aboutsummaryrefslogtreecommitdiff
path: root/fly-tools/derive-background/pixel.c
blob: 7fde6aee55d85a10e6e2e14101045344b5d0e3e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include <wand/MagickWand.h>
#include <gsl/gsl_histogram.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define round(x)((x)>=0?(int)((x)+0.5):(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 main(int argc, char **argv ) {


  MagickWand *magick_wand;
  MagickWand *output_wand;
  MagickWand *first_wand;

  double green, red, blue;
  int i,j, k;

  int image = 0;
  int height = 0;
  int width = 0;
  int nImages = 0;

  unsigned long number_wands; 

  char filename[256];
  char *temp;

  uint8_t ****array;

  MagickWandGenesis();

  // open the first image and get the height and width
  first_wand = NewMagickWand();
  if(MagickReadImage(first_wand, "Background.png") == MagickFalse) {
    ThrowWandException(first_wand);
  }
  height =  MagickGetImageHeight(first_wand);
  width =  MagickGetImageWidth(first_wand);

  first_wand =  DestroyMagickWand(first_wand);

  printf("height: %d width:%d \n", height, width);


  // count how many images there are in the input file
  FILE *count = fopen( argv[1], "r");
  if(count != NULL) {  
    while((fgets(filename, sizeof(filename), count)) != NULL) {
      nImages++;
    }
    fclose(count);
  }

  printf("%d \n", nImages);


  // initialize the storage array.
  array = calloc(nImages, sizeof(array[0]));
  for(i = 0; i < nImages; i++)
  {
    array[i] = calloc(height, sizeof(array[0][0]));
    for(j = 0; j < height; j++)
    {
      array[i][j] = calloc(width, sizeof(array[0][0][0]));
      for(k = 0; k < width; k++)
      {
        array[i][j][k] = calloc(3, sizeof(array[0][0][0][0]));
      }
    }
  }

  // store each pixel in the storage array. array[nImage][height][width][ { R, G, B} ] 
  FILE *input_file = fopen ( argv[1], "r" );
  if ( input_file != NULL ) {
    while ((fgets(filename, sizeof(filename), input_file)) != NULL ) {
      temp = strchr(filename, '\n');
      if (temp != NULL) *temp = '\0';

      magick_wand = NewMagickWand();
      MagickReadImage(magick_wand, filename);

      PixelIterator* iterator = NewPixelIterator(magick_wand);

      PixelWand **pixels = PixelGetNextIteratorRow(iterator,&number_wands);
      printf("Image number:%d Filename: %s \n", image, filename);
      for (i=0; pixels != (PixelWand **) NULL; i++) {
        for (j=0; j<number_wands; j++) {

          green = PixelGetGreen(*pixels);
          blue = PixelGetBlue(*pixels);
          red = PixelGetRed(*pixels);

          //    printf("write: %d %d %d \n", round(red*255), round(green*255), round(blue*255));
          array[image][i][j][0] = round(red*255);
          array[image][i][j][1] = round(green*255);
          array[image][i][j][2] = round(blue*255); 
        }
        PixelSyncIterator(iterator);
        pixels=PixelGetNextIteratorRow(iterator,&number_wands);
      }

      PixelSyncIterator(iterator);
      iterator=DestroyPixelIterator(iterator);
      ClearMagickWand(magick_wand);

      image++;
    }
    fclose ( input_file );
  }
  else {
    exit(0);
  }


  // initialize the output array
  int output[height][width][3];

  // calculate histograms

  for (j = 0; j < height; j++) {
    for (k = 0; k < width; k++) {

      gsl_histogram * r = gsl_histogram_alloc (nImages);
      gsl_histogram * g = gsl_histogram_alloc (nImages);
      gsl_histogram * b = gsl_histogram_alloc (nImages);

      gsl_histogram_set_ranges_uniform (r, 0, 255);
      gsl_histogram_set_ranges_uniform (g, 0, 255);
      gsl_histogram_set_ranges_uniform (b, 0, 255);

      for (i = 0; i < nImages; i++) {

        gsl_histogram_increment (r, array[i][j][k][0]);
        gsl_histogram_increment (g, array[i][j][k][1]);
        gsl_histogram_increment (b, array[i][j][k][2]);
       //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]);
      }


      size_t red = gsl_histogram_max_bin(r);
      size_t green = gsl_histogram_max_bin(g);
      size_t blue = gsl_histogram_max_bin(b);
      int red_index = gsl_histogram_get(r, red);
      int green_index = gsl_histogram_get(g, green);
      int blue_index = gsl_histogram_get(b, blue);
      output[j][k][0] = array[red_index - 1 ][j][k][0];
      output[j][k][1] = array[blue_index - 1 ][j][k][1];
      output[j][k][2] = array[green_index - 1 ][j][k][1];

      printf("i (%d,%d) rgb(%d,%d,%d) \n ", j, k, output[j][k][0], output[j][k][1], output[j][k][2]); 

      gsl_histogram_free (r);
      gsl_histogram_free (g);
      gsl_histogram_free (b); 
    }
  }

   MagickWand *m_wand = NULL;
   PixelWand *p_wand = NULL;
   PixelIterator *iterator = NULL;
   PixelWand **pixels = NULL;
   int x,y;
   char rgb[128];

   MagickWandGenesis();

   p_wand = NewPixelWand();
   PixelSetColor(p_wand,"white");
    m_wand = NewMagickWand();
   // Create a 100x100 image with a default of white
   MagickNewImage(m_wand,height,width,p_wand);
   // Get a new pixel iterator 
   iterator=NewPixelIterator(m_wand);
   for(y=0;y<100;y++) {
      // Get the next row of the image as an array of PixelWands
      pixels=PixelGetNextIteratorRow(iterator,&x);
      // Set the row of wands to a simple gray scale gradient
      for(x=0;x<100;x++) {
         sprintf(rgb, "rgb(%d,%d,%d)", array[0][i][j][0], array[0][i][j][1], array[0][i][j][2]);
         PixelSetColor(pixels[x],rgb);
      }
      // Sync writes the pixels back to the m_wand
      PixelSyncIterator(iterator);
   }
   // Clean up
   iterator=DestroyPixelIterator(iterator);
   MagickWriteImage(m_wand,"output.png");
   DestroyMagickWand(m_wand);
    MagickWandTerminus();


  // write the histogram data to an image
 // output_wand = NewMagickWand();
 // PixelWand *p = NewPixelWand();
 // PixelSetColor(p, "white");
 // if(MagickNewImage(output_wand, height, width, p) == MagickFalse) {
 //   ThrowWandException(output_wand);
 // }

 // PixelIterator* iterator = NewPixelIterator(output_wand);
 // PixelWand **pixels = PixelGetNextIteratorRow(iterator,&number_wands);

 // printf("Image number:%d Filename: %s \n", image, filename);
 // for (i=0; i<=height; i++) {
 //   for (j=0; j<width; j++) {

 //     char rgb[24];

 //     //sprintf(rgb, "rgb(0,20,0)");
 //     printf("o (%d,%d) %s \n", i, j, rgb);
 //     if( (PixelSetColor(pixels[j], rgb)) == MagickFalse ) { 
 //       ThrowWandException(output_wand);
 //     }
 //   }
 //   PixelSyncIterator(iterator);
 //   pixels=PixelGetNextIteratorRow(iterator,&number_wands);
 // }


  MagickWandTerminus();

  return 0;
}