diff options
author | mutantturkey <mutantturke@gmail.com> | 2012-07-02 08:35:39 -0400 |
---|---|---|
committer | mutantturkey <mutantturke@gmail.com> | 2012-07-02 08:35:39 -0400 |
commit | ba2bfacec6f2349e9c3b57608674cfb057591ee5 (patch) | |
tree | 424e8bc21d10ddfd6164cb37642344e95d035a7e /fly-tools | |
parent | 446461ce562f982ee3a768036083091b9b2e30e6 (diff) |
code cleanups, all values will be positive - so don't check - should benefit performance
Diffstat (limited to 'fly-tools')
-rw-r--r-- | fly-tools/derive-background/pixel.c | 109 |
1 files changed, 52 insertions, 57 deletions
diff --git a/fly-tools/derive-background/pixel.c b/fly-tools/derive-background/pixel.c index 605cfea..2330678 100644 --- a/fly-tools/derive-background/pixel.c +++ b/fly-tools/derive-background/pixel.c @@ -5,40 +5,45 @@ #include <stdlib.h> #include <string.h> -#define round(x)((x)>=0?(int)((x)+0.5):(int)((x)-0.5)) +//#define round(x)((x)>=0?(int)((x)+0.5):(int)((x)-0.5)) +#define round(x)((int)((x)+0.5)) #define ThrowWandException(wand) { char *description; ExceptionType severity; description=MagickGetException(wand,&severity); (void) fprintf(stderr,"%s %s %lu %s\n",GetMagickModule(),description); description=(char *) MagickRelinquishMemory(description); exit(-1); } -int -findmax (uint8_t *p, int n) + +int findmax (uint8_t *p, int n) { - int mx = 0, v = p[0]; - for (int i = 1; i < n; i++) { - if (p[i] > v) { - v = p[i]; - mx = i; - } - } - return mx; + int mx = 0, v = p[0]; + for (int i = 1; i < n; i++) { + if (p[i] > v) { + v = p[i]; + mx = i; + } + } + return mx; } int main(int argc, char **argv ) { - MagickWand *magick_wand; - MagickWand *first_wand; - PixelIterator* iterator; - PixelWand **pixels; + MagickWand *magick_wand, *first_wand, *output_wand; + PixelIterator *input_iterator, *output_iterator; + PixelWand **pixels, *p_out; double green, red, blue; + int i,j, k, x, y; int image = 0; - unsigned long height, width; int nImages = 0; + unsigned long height, width; + char filename[256]; char rgb[128]; char *temp; + FILE *input_file; + uint8_t ****array; + uint8_t ***output; // open the first image and get the height and width first_wand = NewMagickWand(); @@ -63,19 +68,24 @@ int main(int argc, char **argv ) { fclose(count); } - printf("number of imagges: %d \n", nImages); + printf("number of images: %d \n", nImages); - // initialize the storage array. + // initialize the storage arrays + output = calloc(height, sizeof(output[0])); + for(i = 0; i < height; i++) { + output[i] = calloc(width, sizeof(output[0][0])); + for(j = 0; j < width; j++) { + output[i][j] = calloc(3, sizeof(output[0][0][0])); + } + } + array = calloc(nImages, sizeof(array[0])); - for(i = 0; i < nImages; i++) - { + for(i = 0; i < nImages; i++) { array[i] = calloc(height, sizeof(array[0][0])); - for(j = 0; j < (long) height; j++) - { + for(j = 0; j < (long) height; j++) { array[i][j] = calloc(width, sizeof(array[0][0][0])); - for(k = 0; k < width; k++) - { + for(k = 0; k < width; k++){ array[i][j][k] = calloc(3, sizeof(array[0][0][0][0])); } } @@ -84,7 +94,7 @@ int main(int argc, char **argv ) { MagickWandGenesis(); // store each pixel in the storage array. array[nImage][height][width][ { R, G, B} ] - FILE *input_file = fopen ( argv[1], "r" ); + input_file = fopen ( argv[1], "r" ); if ( input_file != NULL ) { while ((fgets(filename, sizeof(filename), input_file)) != NULL ) { temp = strchr(filename, '\n'); @@ -93,11 +103,11 @@ int main(int argc, char **argv ) { magick_wand = NewMagickWand(); MagickReadImage(magick_wand, filename); - iterator = NewPixelIterator(magick_wand); + input_iterator = NewPixelIterator(magick_wand); printf("Image number:%d Filename: %s \n", image, filename); for (i=0; i<height; i++) { - pixels = PixelGetNextIteratorRow(iterator, &width); + pixels = PixelGetNextIteratorRow(input_iterator, &width); for (j=0; j<width ; j++) { green = PixelGetGreen(pixels[j]); @@ -109,11 +119,10 @@ int main(int argc, char **argv ) { array[image][i][j][2] = round(blue*255); // printf("array: (%d, %d,%d,%d,%d,%d) \n", image, i, j, array[image][i][j][0], array[image][i][j][1], array[image][i][j][2]); } - PixelSyncIterator(iterator); + PixelSyncIterator(input_iterator); } - - PixelSyncIterator(iterator); - iterator=DestroyPixelIterator(iterator); + PixelSyncIterator(input_iterator); + input_iterator=DestroyPixelIterator(input_iterator); ClearMagickWand(magick_wand); image++; @@ -125,19 +134,12 @@ int main(int argc, char **argv ) { exit(1); } - - // initialize the output array - uint8_t output[200][200][3]; - // calculate histograms for (j = 0; j < height; j++) { for (k = 0; k < width; k++) { - uint8_t histr[256] = { 0 }, histg[256] = { 0 }, histb[256] = { 0 }; - - //printf("height %d width %d image %d %hhu %hhu %hhu \n", j, k, i, array[i][j][k][0], array[i][j][k][1], array[i][j][k][2]); for (i = 0; i < nImages; i++) { histr[array[i][j][k][0]] += 1; histg[array[i][j][k][1]] += 1; @@ -153,36 +155,29 @@ int main(int argc, char **argv ) { } } - MagickWand *m_wand = NULL; - PixelWand *p_wand = NULL; - iterator = NULL; - *pixels = NULL; - - p_wand = NewPixelWand(); - PixelSetColor(p_wand,"white"); - m_wand = NewMagickWand(); - - MagickNewImage(m_wand,width,height,p_wand); + pixels = NewPixelWand(); + p_out = NewPixelWand(); + PixelSetColor(p_out,"white"); - iterator=NewPixelIterator(m_wand); + output_wand = NewMagickWand(); + MagickNewImage(output_wand,width,height,p_out); + output_iterator=NewPixelIterator(output_wand); for(x=0;x<height;x++) { - pixels=PixelGetNextIteratorRow(iterator,&y); + pixels=PixelGetNextIteratorRow(output_iterator,&y); for(y=0;y<width;y++) { - sprintf(rgb, "rgb(%d,%d,%d)", output[x][y][0], output[x][y][1], output[x][y][2]); printf("write (%d,%d), %s \n", x ,y, rgb); - PixelSetColor(pixels[y],rgb); // this segfaults + PixelSetColor(pixels[y],rgb); } - - PixelSyncIterator(iterator); + PixelSyncIterator(output_iterator); } - iterator=DestroyPixelIterator(iterator); - MagickWriteImage(m_wand,"output.png"); - DestroyMagickWand(m_wand); + output_iterator=DestroyPixelIterator(output_iterator); + MagickWriteImage(output_wand,"output.png"); + DestroyMagickWand(output_wand); MagickWandTerminus(); |