Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 27.36 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <pthread.h>
  4.  
  5. //problem assumptions
  6. #define BMP_HEADER_SIZE_BYTES 14
  7. #define BMP_DIB_HEADER_SIZE_BYTES 40
  8. #define MAXIMUM_IMAGE_SIZE 256
  9.  
  10. #define BYTE_PAD(n) (n%4)
  11.  
  12.  
  13. struct BMP_Header {
  14.     char signature[2];      //ID field
  15.     int size;       //Size of the BMP file
  16.     short reserved1;        //Application specific
  17.     short reserved2;        //Application specific
  18.     int offset_pixel_array;  //Offset where the pixel array (bitmap data) can be found
  19. };
  20.  
  21.  
  22. struct DIB_Header {
  23.     int size; // Number of bytes in DIB Header
  24.     int width; // Width of bitmap in pixels
  25.     int height; // Height
  26.     short numColorPlanes;
  27.     short numBitsPerPixel;
  28.     int BI_RGB;
  29.     int sizeOfRawBitmapData;
  30.     int printResHoriz;
  31.     int printResVert;
  32.     int numColorsInPalette;
  33.     int numImportantColors;
  34. };
  35.  
  36. struct Pixel {
  37.     unsigned char r;
  38.     unsigned char g;
  39.     unsigned char b;
  40. };
  41.  
  42.  
  43. struct PixelRow {
  44.     struct Pixel *pixels;
  45.     char *pad;
  46. };
  47.  
  48.  
  49. struct boxBlurArgs {
  50.     int height;
  51.     int width;
  52.     int i;
  53.     struct PixelRow * pixelRows;
  54.     struct PixelRow * blurredPixelRows;
  55. };
  56.  
  57. void print_usage(char **argv) {
  58.     fprintf(stderr, "Usage: ./%s <input_bitmap_file> <output_bitmap_file>\n", argv[0]);
  59.     exit(-1);
  60. }
  61.  
  62. void * computeBoxBlur(void *blurArgs);
  63.  
  64. int main(int argc, char *argv[]) {
  65.     if (argc != 3) {
  66.         print_usage(argv);
  67.     }
  68.    
  69.     FILE* infile = fopen(argv[1], "rb");
  70.     if (!infile) {
  71.         fprintf(stderr, "No filename or bad filename specified\n");
  72.         print_usage(argv);
  73.     }
  74.    
  75.     struct BMP_Header bmpHeader;
  76.     struct DIB_Header dibHeader;
  77.  
  78.     //read bitmap file header (14 bytes)
  79.     fread(&bmpHeader.signature, sizeof(char)*2, 1, infile);
  80.     fread(&bmpHeader.size, sizeof(int), 1, infile);
  81.     fread(&bmpHeader.reserved1, sizeof(short), 1, infile);
  82.     fread(&bmpHeader.reserved2, sizeof(short), 1, infile);
  83.     fread(&bmpHeader.offset_pixel_array, sizeof(int), 1, infile);
  84.  
  85.     fread(&dibHeader.size, sizeof(int), 1, infile);
  86.     fread(&dibHeader.width, sizeof(int), 1, infile);
  87.     fread(&dibHeader.height, sizeof(int), 1, infile);
  88.     fread(&dibHeader.numColorPlanes, sizeof(short), 1, infile);
  89.     fread(&dibHeader.numBitsPerPixel, sizeof(short), 1, infile);
  90.     fread(&dibHeader.BI_RGB, sizeof(int), 1, infile);
  91.     fread(&dibHeader.sizeOfRawBitmapData, sizeof(int), 1, infile);
  92.     fread(&dibHeader.printResHoriz, sizeof(int), 1, infile);
  93.     fread(&dibHeader.printResVert, sizeof(int), 1, infile);
  94.     fread(&dibHeader.numColorsInPalette, sizeof(int), 1, infile);
  95.     fread(&dibHeader.numImportantColors, sizeof(int), 1, infile);
  96.  
  97.     printf("signature: %c%c\n", bmpHeader.signature[0], bmpHeader.signature[1]);
  98.     printf("size: %d\n", bmpHeader.size);
  99.     printf("reserved1: %d\n", bmpHeader.reserved1);
  100.     printf("reserved2: %d\n", bmpHeader.reserved2);
  101.     printf("offset_pixel_array: %d\n", bmpHeader.offset_pixel_array);
  102.  
  103.     printf("dibHeader.size: %d\n", dibHeader.size);
  104.     printf("dibHeader.width: %d\n", dibHeader.width);
  105.     printf("dibHeader.height: %d\n", dibHeader.height);
  106.     printf("dibHeader.numColorPlanes: %d\n", dibHeader.numColorPlanes);
  107.     printf("dibHeader.numBitsPerPixel: %d\n", dibHeader.numBitsPerPixel);
  108.     printf("dibHeader.BI_RGB: %d\n", dibHeader.BI_RGB);
  109.     printf("dibHeader.sizeOfRawBitmapData: %d\n", dibHeader.sizeOfRawBitmapData);
  110.     printf("dibHeader.printResHoriz: %d\n", dibHeader.printResHoriz);
  111.     printf("dibHeader.printResVert: %d\n", dibHeader.printResVert);
  112.     printf("dibHeader.numColorsInPalette: %d\n", dibHeader.numColorsInPalette);
  113.     printf("dibHeader.numImportantColors: %d\n", dibHeader.numImportantColors);
  114.  
  115.     int bytePad = BYTE_PAD(dibHeader.width);
  116.  
  117.     // Malloc all rows of pixels for original image
  118.     struct PixelRow *pixelRows = malloc( (sizeof(struct Pixel *) + sizeof(char *)) * dibHeader.height );
  119.     if (!pixelRows) {
  120.         fprintf(stderr, "Failed to malloc pixelRows, quitting!\n");
  121.         exit(-1);
  122.     }
  123.  
  124.     // Malloc all rows of pixels for new image
  125.     struct PixelRow *blurredPixelRows = malloc( (sizeof(struct Pixel *) + sizeof(char *)) * dibHeader.height );
  126.     if (!blurredPixelRows) {
  127.         fprintf(stderr, "Failed to malloc blurredPixelRows, quitting!\n");
  128.         exit(-1);
  129.     }
  130.  
  131.  
  132.     // Malloc structures to hold pixel data per row (including possible byte pad)
  133.     for (int i = 0; i < dibHeader.height; i++) {
  134.         pixelRows[i].pixels = malloc( (sizeof(struct Pixel) * dibHeader.width) );
  135.         blurredPixelRows[i].pixels = malloc( (sizeof(struct Pixel) * dibHeader.width) );
  136.         if (! pixelRows[i].pixels ) {
  137.             fprintf(stderr, "Failed to malloc pixels for pixelRows[%d], quitting!\n", i);
  138.             exit(-1);
  139.         }
  140.        
  141.         if (! blurredPixelRows[i].pixels ) {
  142.             fprintf(stderr, "Failed to malloc pixels for blurredPixelRows[%d], quitting!\n", i);
  143.             exit(-1);
  144.         }
  145.  
  146.         if (bytePad > 0) {
  147.             pixelRows[i].pad = malloc( sizeof(char) * bytePad );
  148.             blurredPixelRows[i].pad = malloc( sizeof(char) * bytePad );
  149.             if (! pixelRows[i].pad ) {
  150.                 fprintf(stderr, "Failed to malloc pad for pixelRows[%d], quitting!\n", i);
  151.                 exit(-1);
  152.             }
  153.             if (! blurredPixelRows[i].pad ) {
  154.                 fprintf(stderr, "Failed to malloc pad for blurredPixelRows[%d], quitting!\n", i);
  155.                 exit(-1);
  156.             }
  157.         }
  158.         else {
  159.             pixelRows[i].pad = NULL;
  160.             blurredPixelRows[i].pad = NULL;
  161.         }
  162.     }
  163.  
  164.     // Read in the pixel data
  165.     for (int i = 0; i < dibHeader.height; i++) {
  166.         size_t pixelRowSize = ((sizeof(unsigned char) * dibHeader.width) * 3) + bytePad;
  167.        
  168.         unsigned char *pixelRow = malloc( pixelRowSize );
  169.        
  170.         if (pixelRow == NULL) {
  171.             fprintf(stderr, "Failed to malloc pixelRow, quitting!\n");
  172.             exit(-1);
  173.         }
  174.  
  175.         fread(pixelRow, sizeof(unsigned char), (dibHeader.width * 3) + bytePad, infile);
  176.        
  177.         int k = 0;
  178.  
  179.         for (int j = 0; (j < pixelRowSize - bytePad) && (k < dibHeader.width) ; j+=3) {
  180.             unsigned char b = pixelRow[j];
  181.             unsigned char g = pixelRow[j+1];
  182.             unsigned char r = pixelRow[j+2];
  183.  
  184.             pixelRows[i].pixels[k].r = r;
  185.             pixelRows[i].pixels[k].g = g;
  186.             pixelRows[i].pixels[k].b = b;
  187.            
  188.             k++;
  189.         }
  190.  
  191.         for (int j = 0; j < bytePad; j++) {
  192.             char v = pixelRow[ pixelRowSize - bytePad + j ];
  193.             pixelRows[i].pad[j] = v;
  194.         }
  195.         free(pixelRow);
  196.     }
  197.     fclose(infile);
  198.  
  199.     FILE *outfile = fopen(argv[2], "wb");
  200.     if (!outfile) {
  201.         fprintf(stderr, "Failed to open outfile: %s\n", argv[2]);
  202.         exit(-1);
  203.     }
  204.    
  205.     fwrite(&bmpHeader.signature, sizeof(char)*2, 1, outfile);
  206.     fwrite(&bmpHeader.size, sizeof(int), 1, outfile);
  207.     fwrite(&bmpHeader.reserved1, sizeof(short), 1, outfile);
  208.     fwrite(&bmpHeader.reserved2, sizeof(short), 1, outfile);
  209.     fwrite(&bmpHeader.offset_pixel_array, sizeof(int), 1, outfile);
  210.  
  211.     fwrite(&dibHeader.size, sizeof(int), 1, outfile);
  212.     fwrite(&dibHeader.width, sizeof(int), 1, outfile);
  213.     fwrite(&dibHeader.height, sizeof(int), 1, outfile);
  214.     fwrite(&dibHeader.numColorPlanes, sizeof(short), 1, outfile);
  215.     fwrite(&dibHeader.numBitsPerPixel, sizeof(short), 1, outfile);
  216.     fwrite(&dibHeader.BI_RGB, sizeof(int), 1, outfile);
  217.     fwrite(&dibHeader.sizeOfRawBitmapData, sizeof(int), 1, outfile);
  218.     fwrite(&dibHeader.printResHoriz, sizeof(int), 1, outfile);
  219.     fwrite(&dibHeader.printResVert, sizeof(int), 1, outfile);
  220.     fwrite(&dibHeader.numColorsInPalette, sizeof(int), 1, outfile);
  221.     fwrite(&dibHeader.numImportantColors, sizeof(int), 1, outfile);
  222.  
  223.     // compute box-blur with pthreads
  224.     for (int i = 0; i < dibHeader.height; i+=4) {
  225.         pthread_t exampleThread[4];
  226.         struct boxBlurArgs b[4];
  227.         for (int j = 0; j < 4; j++) {
  228.             b[j].height = dibHeader.height;
  229.             b[j].width = dibHeader.width;
  230.             b[j].i = i+j;
  231.             b[j].pixelRows = pixelRows;
  232.             b[j].blurredPixelRows = blurredPixelRows;
  233.  
  234.             if ( pthread_create( &exampleThread[j], NULL, computeBoxBlur, &b[j])) {
  235.                 fprintf(stderr, "Error creating thread %d on row %d\n", j, i);
  236.                 exit(-1);
  237.             }
  238.         }
  239.  
  240.         for (int j = 0; j < 4; j++) {
  241.             if ( pthread_join( exampleThread[j], NULL ) ) {
  242.                 fprintf(stderr, "Error joining thread %d\n", j);
  243.                 exit(-1);
  244.             }
  245.         }
  246.     }
  247.    
  248.     // write out file
  249.     for (int i = 0; i < dibHeader.height; i++) {
  250.         char *pad = pixelRows[i].pad;
  251.         for (int j = 0; j < dibHeader.width; j++) {
  252.             char r = blurredPixelRows[i].pixels[j].r;
  253.             char g = blurredPixelRows[i].pixels[j].g;
  254.             char b = blurredPixelRows[i].pixels[j].b;
  255.             fputc(b, outfile);
  256.             fputc(g, outfile);
  257.             fputc(r, outfile);
  258.         }
  259.         if (pad) {
  260.             for (int j = 0; j < bytePad; j++) {
  261.                 fputc(pad[j], outfile);
  262.             }
  263.         }
  264.     }
  265.  
  266.     fclose(outfile);
  267.  
  268.     // end of program cleanup
  269.     for (int i = 0; i < dibHeader.height; i++) {
  270.         free(pixelRows[i].pixels);
  271.         free(pixelRows[i].pad);
  272.     }
  273.     free(pixelRows);
  274.     return 0;
  275. }
  276.  
  277.  
  278. void * computeBoxBlur(void *blurArgs) {
  279.     struct boxBlurArgs *a = blurArgs;
  280.     int height = a->height;
  281.     int width = a->width;
  282.     int i = a->i;
  283.     struct PixelRow * pixelRows = a->pixelRows;
  284.     struct PixelRow * blurredPixelRows = a->blurredPixelRows;
  285.  
  286.     if (i < height) {
  287.        
  288.         for (int j = 0; j < width; j++) {
  289.  
  290.             int x = i;
  291.             int y = j;
  292.  
  293.             if (i == 0) {
  294.                
  295.                 if (j == 0) {
  296.                     unsigned char r = pixelRows[x].pixels[y].r;
  297.                     unsigned char g = pixelRows[x].pixels[y].g;
  298.                     unsigned char b = pixelRows[x].pixels[y].b;
  299.  
  300.                     x = i+1;
  301.                     y = j;
  302.                     unsigned char r_u = pixelRows[x].pixels[y].r;
  303.                     unsigned char g_u = pixelRows[x].pixels[y].g;
  304.                     unsigned char b_u = pixelRows[x].pixels[y].b;
  305.  
  306.                     x = i+1;
  307.                     y = j+1;
  308.                     unsigned char r_ur = pixelRows[x].pixels[y].r;
  309.                     unsigned char g_ur = pixelRows[x].pixels[y].g;
  310.                     unsigned char b_ur = pixelRows[x].pixels[y].b;
  311.  
  312.                     x = i;
  313.                     y = j+1;
  314.                     unsigned char r_r = pixelRows[x].pixels[y].r;
  315.                     unsigned char g_r = pixelRows[x].pixels[y].g;
  316.                     unsigned char b_r = pixelRows[x].pixels[y].b;
  317.  
  318.                     // compute new pixel
  319.                     unsigned char r_new = (r + r_u + r_ur + r_r) / 4;
  320.                     unsigned char g_new = (g + g_u + g_ur + g_r) / 4;
  321.                     unsigned char b_new = (b + b_u + b_ur + b_r) / 4;
  322.  
  323.                     // store in new array blurredPixelRows
  324.  
  325.                     blurredPixelRows[i].pixels[j].r = r_new;
  326.                     blurredPixelRows[i].pixels[j].g = g_new;
  327.                     blurredPixelRows[i].pixels[j].b = b_new;
  328.                 }
  329.                 else if ( j == width - 1 ) {
  330.                     unsigned char r = pixelRows[x].pixels[y].r;
  331.                     unsigned char g = pixelRows[x].pixels[y].g;
  332.                     unsigned char b = pixelRows[x].pixels[y].b;
  333.  
  334.                     x = i+1;
  335.                     y = j;
  336.                     unsigned char r_u = pixelRows[x].pixels[y].r;
  337.                     unsigned char g_u = pixelRows[x].pixels[y].g;
  338.                     unsigned char b_u = pixelRows[x].pixels[y].b;
  339.  
  340.                     x = i+1;
  341.                     y = j-1;
  342.                     unsigned char r_ul = pixelRows[x].pixels[y].r;
  343.                     unsigned char g_ul = pixelRows[x].pixels[y].g;
  344.                     unsigned char b_ul = pixelRows[x].pixels[y].b;
  345.  
  346.                     x = i;
  347.                     y = j-1;
  348.                     unsigned char r_l = pixelRows[x].pixels[y].r;
  349.                     unsigned char g_l = pixelRows[x].pixels[y].g;
  350.                     unsigned char b_l = pixelRows[x].pixels[y].b;
  351.  
  352.                     // compute new pixel
  353.                     unsigned char r_new = (r + r_u + r_ul + r_l) / 4;
  354.                     unsigned char g_new = (g + g_u + g_ul + g_l) / 4;
  355.                     unsigned char b_new = (b + b_u + b_ul + b_l) / 4;
  356.  
  357.                     // store in new array blurredPixelRows
  358.  
  359.                     blurredPixelRows[i].pixels[j].r = r_new;
  360.                     blurredPixelRows[i].pixels[j].g = g_new;
  361.                     blurredPixelRows[i].pixels[j].b = b_new;
  362.                 }
  363.                 else {
  364.  
  365.                     unsigned char r = pixelRows[x].pixels[y].r;
  366.                     unsigned char g = pixelRows[x].pixels[y].g;
  367.                     unsigned char b = pixelRows[x].pixels[y].b;
  368.  
  369.                     x = i+1;
  370.                     y = j;
  371.                     unsigned char r_u = pixelRows[x].pixels[y].r;
  372.                     unsigned char g_u = pixelRows[x].pixels[y].g;
  373.                     unsigned char b_u = pixelRows[x].pixels[y].b;
  374.  
  375.                     x = i+1;
  376.                     y = j-1;
  377.                     unsigned char r_ul = pixelRows[x].pixels[y].r;
  378.                     unsigned char g_ul = pixelRows[x].pixels[y].g;
  379.                     unsigned char b_ul = pixelRows[x].pixels[y].b;
  380.  
  381.                     x = i+1;
  382.                     y = j+1;
  383.                     unsigned char r_ur = pixelRows[x].pixels[y].r;
  384.                     unsigned char g_ur = pixelRows[x].pixels[y].g;
  385.                     unsigned char b_ur = pixelRows[x].pixels[y].b;
  386.  
  387.                     x = i;
  388.                     y = j+1;
  389.                     unsigned char r_r = pixelRows[x].pixels[y].r;
  390.                     unsigned char g_r = pixelRows[x].pixels[y].g;
  391.                     unsigned char b_r = pixelRows[x].pixels[y].b;
  392.  
  393.                     x = i;
  394.                     y = j-1;
  395.                     unsigned char r_l = pixelRows[x].pixels[y].r;
  396.                     unsigned char g_l = pixelRows[x].pixels[y].g;
  397.                     unsigned char b_l = pixelRows[x].pixels[y].b;
  398.  
  399.                     // compute new pixel
  400.                     unsigned char r_new = (r + r_u + r_ul + r_l + r_ur + r_r) / 6;
  401.                     unsigned char g_new = (g + g_u + g_ul + g_l + g_ur + g_r) / 6;
  402.                     unsigned char b_new = (b + b_u + b_ul + b_l + b_ur + b_r) / 6;
  403.  
  404.                     // store in new array blurredPixelRows
  405.  
  406.                     blurredPixelRows[i].pixels[j].r = r_new;
  407.                     blurredPixelRows[i].pixels[j].g = g_new;
  408.                     blurredPixelRows[i].pixels[j].b = b_new;
  409.                 }  
  410.             }
  411.             else if ( i != 0 && i != (height - 1)) {
  412.                 if (j == 0) {
  413.                     unsigned char r = pixelRows[x].pixels[y].r;
  414.                     unsigned char g = pixelRows[x].pixels[y].g;
  415.                     unsigned char b = pixelRows[x].pixels[y].b;
  416.  
  417.                     x = i+1;
  418.                     y = j;
  419.                     unsigned char r_u = pixelRows[x].pixels[y].r;
  420.                     unsigned char g_u = pixelRows[x].pixels[y].g;
  421.                     unsigned char b_u = pixelRows[x].pixels[y].b;
  422.  
  423.                     x = i+1;
  424.                     y = j+1;
  425.                     unsigned char r_ur = pixelRows[x].pixels[y].r;
  426.                     unsigned char g_ur = pixelRows[x].pixels[y].g;
  427.                     unsigned char b_ur = pixelRows[x].pixels[y].b;
  428.  
  429.                     x = i;
  430.                     y = j+1;
  431.                     unsigned char r_r = pixelRows[x].pixels[y].r;
  432.                     unsigned char g_r = pixelRows[x].pixels[y].g;
  433.                     unsigned char b_r = pixelRows[x].pixels[y].b;
  434.                    
  435.                     x = i-1;
  436.                     y = j+1;
  437.                     unsigned char r_dr = pixelRows[x].pixels[y].r;
  438.                     unsigned char g_dr = pixelRows[x].pixels[y].g;
  439.                     unsigned char b_dr = pixelRows[x].pixels[y].b;
  440.                    
  441.                     x = i-1;
  442.                     y = j;
  443.                     unsigned char r_d = pixelRows[x].pixels[y].r;
  444.                     unsigned char g_d = pixelRows[x].pixels[y].g;
  445.                     unsigned char b_d = pixelRows[x].pixels[y].b;
  446.  
  447.                     // compute new pixel
  448.                     unsigned char r_new = (r + r_u + r_ur + r_r + r_dr + r_d) / 6;
  449.                     unsigned char g_new = (g + g_u + g_ur + g_r + g_dr + g_d) / 6;
  450.                     unsigned char b_new = (b + b_u + b_ur + b_r + b_dr + b_d) / 6;
  451.  
  452.                     // store in new array blurredPixelRows
  453.  
  454.                     blurredPixelRows[i].pixels[j].r = r_new;
  455.                     blurredPixelRows[i].pixels[j].g = g_new;
  456.                     blurredPixelRows[i].pixels[j].b = b_new;
  457.                 }
  458.                 else if ( j == width - 1 ) {
  459.                     unsigned char r = pixelRows[x].pixels[y].r;
  460.                     unsigned char g = pixelRows[x].pixels[y].g;
  461.                     unsigned char b = pixelRows[x].pixels[y].b;
  462.  
  463.                     y = i+1;
  464.                     y = j;
  465.                     unsigned char r_u = pixelRows[x].pixels[y].r;
  466.                     unsigned char g_u = pixelRows[x].pixels[y].g;
  467.                     unsigned char b_u = pixelRows[x].pixels[y].b;
  468.  
  469.                     y = i+1;
  470.                     y = j-1;
  471.                     unsigned char r_ul = pixelRows[x].pixels[y].r;
  472.                     unsigned char g_ul = pixelRows[x].pixels[y].g;
  473.                     unsigned char b_ul = pixelRows[x].pixels[y].b;
  474.  
  475.                     x = i;
  476.                     y = j-1;
  477.                     unsigned char r_l = pixelRows[x].pixels[y].r;
  478.                     unsigned char g_l = pixelRows[x].pixels[y].g;
  479.                     unsigned char b_l = pixelRows[x].pixels[y].b;
  480.                    
  481.                     x = i-1;
  482.                     y = j-1;
  483.                     unsigned char r_dl = pixelRows[x].pixels[y].r;
  484.                     unsigned char g_dl = pixelRows[x].pixels[y].g;
  485.                     unsigned char b_dl = pixelRows[x].pixels[y].b;
  486.                    
  487.                     x = i-1;
  488.                     y = j;
  489.                     unsigned char r_d = pixelRows[x].pixels[y].r;
  490.                     unsigned char g_d = pixelRows[x].pixels[y].g;
  491.                     unsigned char b_d = pixelRows[x].pixels[y].b;
  492.  
  493.                     // compute new pixel
  494.                     unsigned char r_new = (r + r_u + r_ul + r_l+ r_dl + r_d) / 6;
  495.                     unsigned char g_new = (g + g_u + g_ul + g_l + g_dl + g_d) / 6;
  496.                     unsigned char b_new = (b + b_u + b_ul + b_l+ b_dl + b_d) / 6;
  497.  
  498.                     // store in new array blurredPixelRows
  499.  
  500.                     blurredPixelRows[i].pixels[j].r = r_new;
  501.                     blurredPixelRows[i].pixels[j].g = g_new;
  502.                     blurredPixelRows[i].pixels[j].b = b_new;
  503.                 }
  504.                 else {
  505.                     unsigned char r = pixelRows[x].pixels[y].r;
  506.                     unsigned char g = pixelRows[x].pixels[y].g;
  507.                     unsigned char b = pixelRows[x].pixels[y].b;
  508.  
  509.                     x = i+1;
  510.                     y = j;
  511.                     unsigned char r_u = pixelRows[x].pixels[y].r;
  512.                     unsigned char g_u = pixelRows[x].pixels[y].g;
  513.                     unsigned char b_u = pixelRows[x].pixels[y].b;
  514.  
  515.                     x = i+1;
  516.                     y = j-1;
  517.                     unsigned char r_ul = pixelRows[x].pixels[y].r;
  518.                     unsigned char g_ul = pixelRows[x].pixels[y].g;
  519.                     unsigned char b_ul = pixelRows[x].pixels[y].b;
  520.  
  521.                     x = i+1;
  522.                     y = j+1;
  523.                     unsigned char r_ur = pixelRows[x].pixels[y].r;
  524.                     unsigned char g_ur = pixelRows[x].pixels[y].g;
  525.                     unsigned char b_ur = pixelRows[x].pixels[y].b;
  526.  
  527.                     x = i;
  528.                     y = j+1;
  529.                     unsigned char r_r = pixelRows[x].pixels[y].r;
  530.                     unsigned char g_r = pixelRows[x].pixels[y].g;
  531.                     unsigned char b_r = pixelRows[x].pixels[y].b;
  532.  
  533.                     x = i;
  534.                     y = j-1;
  535.                     unsigned char r_l = pixelRows[x].pixels[y].r;
  536.                     unsigned char g_l = pixelRows[x].pixels[y].g;
  537.                     unsigned char b_l = pixelRows[x].pixels[y].b;
  538.                    
  539.                     x = i-1;
  540.                     y = j;
  541.                     unsigned char r_d = pixelRows[x].pixels[y].r;
  542.                     unsigned char g_d = pixelRows[x].pixels[y].g;
  543.                     unsigned char b_d = pixelRows[x].pixels[y].b;
  544.  
  545.                     x = i-1;
  546.                     y = j-1;
  547.                     unsigned char r_dl = pixelRows[x].pixels[y].r;
  548.                     unsigned char g_dl = pixelRows[x].pixels[y].g;
  549.                     unsigned char b_dl = pixelRows[x].pixels[y].b;
  550.  
  551.                     x = i-1;
  552.                     y = j+1;
  553.                     unsigned char r_dr = pixelRows[x].pixels[y].r;
  554.                     unsigned char g_dr = pixelRows[x].pixels[y].g;
  555.                     unsigned char b_dr = pixelRows[x].pixels[y].b;
  556.  
  557.                     // compute new pixel
  558.                     unsigned char r_new = (r + r_u + r_ul + r_l + r_ur + r_r + r_d + r_dr + r_dl) / 9;
  559.                     unsigned char g_new = (g + g_u + g_ul + g_l + g_ur + g_r + g_d + g_dr + g_dl) / 9;
  560.                     unsigned char b_new = (b + b_u + b_ul + b_l + b_ur + b_r + b_d + b_dr + b_dl) / 9;
  561.  
  562.                     // store in new array blurredPixelRows
  563.  
  564.                     blurredPixelRows[i].pixels[j].r = r_new;
  565.                     blurredPixelRows[i].pixels[j].g = g_new;
  566.                     blurredPixelRows[i].pixels[j].b = b_new;
  567.                 }  
  568.             }
  569.             else if (i == (height - 1)) {
  570.                 if (j == 0) {
  571.                     unsigned char r = pixelRows[x].pixels[y].r;
  572.                     unsigned char g = pixelRows[x].pixels[y].g;
  573.                     unsigned char b = pixelRows[x].pixels[y].b;
  574.  
  575.                     x = i;
  576.                     y = j+1;
  577.                     unsigned char r_r = pixelRows[x].pixels[y].r;
  578.                     unsigned char g_r = pixelRows[x].pixels[y].g;
  579.                     unsigned char b_r = pixelRows[x].pixels[y].b;
  580.  
  581.                     x = i-1;
  582.                     y = j+1;
  583.                     unsigned char r_dr = pixelRows[x].pixels[y].r;
  584.                     unsigned char g_dr = pixelRows[x].pixels[y].g;
  585.                     unsigned char b_dr = pixelRows[x].pixels[y].b;
  586.  
  587.                     x = i-1;
  588.                     y = j;
  589.                     unsigned char r_d = pixelRows[x].pixels[y].r;
  590.                     unsigned char g_d = pixelRows[x].pixels[y].g;
  591.                     unsigned char b_d = pixelRows[x].pixels[y].b;
  592.                    
  593.                     // compute new pixel
  594.                     unsigned char r_new = (r + r_r + r_dr + r_d) / 4;
  595.                     unsigned char g_new = (g + + g_r + g_dr + g_d) / 4;
  596.                     unsigned char b_new = (b + + b_r + b_dr + b_d) / 4;
  597.  
  598.                     // store in new array blurredPixelRows
  599.  
  600.                     blurredPixelRows[i].pixels[j].r = r_new;
  601.                     blurredPixelRows[i].pixels[j].g = g_new;
  602.                     blurredPixelRows[i].pixels[j].b = b_new;
  603.                 }
  604.                 else if ( j == width - 1 ) {
  605.                     unsigned char r = pixelRows[x].pixels[y].r;
  606.                     unsigned char g = pixelRows[x].pixels[y].g;
  607.                     unsigned char b = pixelRows[x].pixels[y].b;
  608.  
  609.                     x = i-1;
  610.                     y = j;
  611.                     unsigned char r_l = pixelRows[x].pixels[y].r;
  612.                     unsigned char g_l = pixelRows[x].pixels[y].g;
  613.                     unsigned char b_l = pixelRows[x].pixels[y].b;
  614.  
  615.                     x = i-1;
  616.                     y = j-1;
  617.                     unsigned char r_dl = pixelRows[x].pixels[y].r;
  618.                     unsigned char g_dl = pixelRows[x].pixels[y].g;
  619.                     unsigned char b_dl = pixelRows[x].pixels[y].b;
  620.  
  621.                     x = i;
  622.                     y = j-1;
  623.                     unsigned char r_d = pixelRows[x].pixels[y].r;
  624.                     unsigned char g_d = pixelRows[x].pixels[y].g;
  625.                     unsigned char b_d = pixelRows[x].pixels[y].b;
  626.                    
  627.                     // compute new pixel
  628.                     unsigned char r_new = (r + r_l+ r_dl + r_d) / 4;
  629.                     unsigned char g_new = (g + g_l + g_dl + g_d) / 4;
  630.                     unsigned char b_new = (b + b_l+ b_dl + b_d) / 4;
  631.  
  632.                     // store in new array blurredPixelRows
  633.  
  634.                     blurredPixelRows[i].pixels[j].r = r_new;
  635.                     blurredPixelRows[i].pixels[j].g = g_new;
  636.                     blurredPixelRows[i].pixels[j].b = b_new;
  637.                 }
  638.                 else {
  639.                     unsigned char r = pixelRows[x].pixels[y].r;
  640.                     unsigned char g = pixelRows[x].pixels[y].g;
  641.                     unsigned char b = pixelRows[x].pixels[y].b;
  642.  
  643.                     x = i;
  644.                     y = j-1;
  645.                     unsigned char r_l = pixelRows[x].pixels[y].r;
  646.                     unsigned char g_l = pixelRows[x].pixels[y].g;
  647.                     unsigned char b_l = pixelRows[x].pixels[y].b;
  648.  
  649.                     x = i;
  650.                     y = j+1;
  651.                     unsigned char r_r = pixelRows[x].pixels[y].r;
  652.                     unsigned char g_r = pixelRows[x].pixels[y].g;
  653.                     unsigned char b_r = pixelRows[x].pixels[y].b;
  654.  
  655.                     x = i - 1;
  656.                     y = j+1;
  657.                     unsigned char r_dr = pixelRows[x].pixels[y].r;
  658.                     unsigned char g_dr = pixelRows[x].pixels[y].g;
  659.                     unsigned char b_dr = pixelRows[x].pixels[y].b;
  660.  
  661.                     x = i - 1;
  662.                     y = j;
  663.                     unsigned char r_d = pixelRows[x].pixels[y].r;
  664.                     unsigned char g_d = pixelRows[x].pixels[y].g;
  665.                     unsigned char b_d = pixelRows[x].pixels[y].b;
  666.  
  667.                     x = i - 1;
  668.                     y = j - 1;
  669.                     unsigned char r_dl = pixelRows[x].pixels[y].r;
  670.                     unsigned char g_dl = pixelRows[x].pixels[y].g;
  671.                     unsigned char b_dl = pixelRows[x].pixels[y].b;
  672.                    
  673.                     // compute new pixel
  674.                     unsigned char r_new = (r + r_l + r_r + r_d + r_dr + r_dl) / 6;
  675.                     unsigned char g_new = (g + g_l + g_r + g_d + g_dr + g_dl) / 6;
  676.                     unsigned char b_new = (b + b_l + b_r + b_d + b_dr + b_dl) / 6;
  677.  
  678.                     // store in new array blurredPixelRows
  679.                     blurredPixelRows[i].pixels[j].r = r_new;
  680.                     blurredPixelRows[i].pixels[j].g = g_new;
  681.                     blurredPixelRows[i].pixels[j].b = b_new;
  682.                 }  
  683.             }
  684.         }
  685.     }
  686.  
  687.     return NULL;
  688. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement