Facebook
From Colorant Pelican, 3 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 54
  1. /**
  2.  * Names: Luke Whipple and Colton Johnson and David Dubchakov
  3.  *
  4.  * Some image manioulation functions for an array of pixels
  5.  */
  6.  
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <math.h>
  10. #define STB_IMAGE_IMPLEMENTATION
  11. #include "stb_image.h"
  12.  
  13. #define STB_IMAGE_WRITE_IMPLEMENTATION
  14. #include "stb_image_write.h"
  15.  
  16. #include "imageUtils.h"
  17.  
  18. Pixel **loadImage(const char *filePath, int *height, int *width) {
  19.   int x,y,n;
  20.   unsigned char *data = stbi_load(filePath, &x, &y, &n, 4); //4 = force RGBA channels
  21.   *height = y;
  22.   *width = x;
  23.  
  24.   //contiguous allocation:
  25.   Pixel **image = (Pixel **)malloc(sizeof(Pixel *) * y);
  26.   image[0] = (Pixel *)malloc(sizeof(Pixel) * (y * x));
  27.   for(int i=1; i<y; i++) {
  28.     image[i] = (*image + (x * i));
  29.   }
  30.  
  31.   int rowIndex = 0;
  32.   int colIndex = 0;
  33.   for(int i=0; i<(x * y * 4); i+=4) { //for each row
  34.       image[rowIndex][colIndex].red   = data[i+0];
  35.       image[rowIndex][colIndex].green = data[i+1];
  36.       image[rowIndex][colIndex].blue  = data[i+2];
  37.       colIndex++;
  38.       if(colIndex == x) {
  39.         //go to next row, reset column
  40.         rowIndex++;
  41.         colIndex=0;
  42.       }
  43.   }
  44.   stbi_image_free(data);
  45.   return image;
  46. }
  47.  
  48. void saveImage(const char *fileName, Pixel **image, int height, int width) {
  49.  
  50.   // Convert height x width Pixel array to single array with
  51.   // 3 (RGB) channels, ignoring the alpha channel and assume 100% opaque
  52.   unsigned char *data = (unsigned char *) malloc(height * width * 3);
  53.   int x = 0;
  54.   for(int i=0; i<height; i++) {
  55.     for(int j=0; j<width; j++) {
  56.       data[x+0] = image[i][j].red;
  57.       data[x+1] = image[i][j].green;
  58.       data[x+2] = image[i][j].blue;
  59.       x+=3;
  60.     }
  61.   }
  62.   //write
  63.   stbi_write_jpg(fileName, width, height, 3, data, 100);
  64.   free(data);
  65.   return;
  66. }
  67.  
  68. Pixel ** copyImage(Pixel ** image, int height, int width) {
  69.   if ((height <= 0) || (width <= 0) || (image == NULL)) {
  70.     return (NULL);
  71.   }
  72.   Pixel ** copy = NULL;
  73.   copy = (Pixel ** ) malloc(sizeof(Pixel) * height);
  74.   for (int k = 0; k < height; k++) {
  75.     copy[k] = (Pixel * ) malloc(sizeof(Pixel) * width);
  76.   }
  77.  
  78.   int i = 0;
  79.   int j = 0;
  80.   for (i = 0; i < height; i++) {
  81.     for (j = 0; j < width; j++) {
  82.       copy[i][j] = image[i][j];
  83.     }
  84.   }
  85.   return (copy);
  86. }
  87.  
  88. void flipHorizontal(Pixel ** image, int height, int width) {
  89.  
  90.   for (int i = 0; i < height; i++) {
  91.     for (int n = 0; n < (width / 2); n++) {
  92.  
  93.       Pixel placeHolder = image[i][n];
  94.  
  95.       image[i][n] = image[i][width - 1 - n];
  96.  
  97.       image[i][width - 1 - n] = placeHolder;
  98.     }
  99.   }
  100. }
  101.  
  102. /**
  103.      *  for (i = 0; i < (NUM_ELEMENTS / 2); ++i) {
  104.       tempVal = userVals[i];                        // Temp for swap
  105.       userVals[i] = userVals[NUM_ELEMENTS - 1 - i]; // First part of swap
  106.       userVals[NUM_ELEMENTS - 1 - i] = tempVal;     // Second complete
  107.    }
  108. */
  109.  
  110. //TODO: implement
  111.  
  112. void flipVertical(Pixel ** image, int height, int width) {
  113.  
  114.   for (int i = 0; i < width; i++) {
  115.     for (int n = 0; n < (height / 2); n++) {
  116.  
  117.       Pixel placeHolder = image[n][i];
  118.  
  119.       image[n][i] = image[height - 1 - n][i];
  120.  
  121.       image[height - 1 - n][i] = placeHolder;
  122.     }
  123.   }
  124. }
  125.  
  126. Pixel ** rotateClockwise(Pixel ** image, int height, int width) {
  127.  
  128.   int placeHolder = height;
  129.   height = width;
  130.   width = placeHolder;
  131.  
  132.   Pixel ** rotate = NULL;
  133.   rotate = (Pixel ** ) malloc(sizeof(Pixel) * height);
  134.   for (int k = 0; k < height; k++) {
  135.     rotate[k] = (Pixel * ) malloc(sizeof(Pixel) * width);
  136.   }
  137.  
  138.   for (int i = 0; i < height; i++) {
  139.     for (int j = 0; j < width; j++) {
  140.       rotate[i][j] = image[j][i];
  141.     }
  142.  
  143.     //TODO: implement
  144.   }
  145.  
  146.   flipHorizontal(rotate, height, width);
  147.  
  148.   placeHolder = height;
  149.   height = width;
  150.   width = placeHolder;
  151.  
  152.   return (rotate);
  153. }