9 #include <magick/api.h>
11 /* load given image to memory. return short RGB values */
12 unsigned short *load_img(int *width, int *height, const char *filename, int index)
15 ImageInfo *imageinfo = NULL;
16 ExceptionInfo exception;
17 unsigned short *img = NULL;
19 MagickCoreGenesis(NULL, MagickFalse);
20 // InitializeMagick(NULL);
21 imageinfo = CloneImageInfo(0);
22 GetExceptionInfo(&exception);
24 sprintf(imageinfo->filename, filename, index);
26 image = ReadImage(imageinfo, &exception);
28 // printf("failed to read image '%s' via *magick\n", filename);
32 *width = image->columns;
33 *height = image->rows;
35 img = (unsigned short *)malloc((*width) * (*height) * 3 * 2);
37 printf("%s:failed to allocate image data\n", __func__);
41 ExportImagePixels(image, 0, 0, *width, *height, "RGB", ShortPixel, img, NULL);
42 // DispatchImage(image, 0, 0, *width, *height, "RGB", ShortPixel, img, NULL);
49 DestroyImageInfo(imageinfo);
57 /* save given image */
58 int save_img(unsigned short *img, int width, int height, int alpha, const char *filename, int index)
62 ImageInfo *imageinfo = NULL;
63 ExceptionInfo exception;
65 MagickCoreGenesis(NULL, MagickFalse);
66 // InitializeMagick(NULL);
67 imageinfo = CloneImageInfo(0);
68 GetExceptionInfo(&exception);
70 imageinfo->quality = 100;
72 image=ConstituteImage(width, height, (alpha)?"RGBA":"RGB", ShortPixel, img, &exception);
74 printf("%s:failed to prepare to write image\n", __func__);
78 /* store as 16 bit, if lib and format supports it */
79 image->depth = save_depth;
81 sprintf(image->filename, filename, index); /* ACHTUNG: nicht imageinfo!!! */
82 if (!WriteImage(imageinfo, image)) {
83 printf("%s:failed to write image\n", __func__);
94 DestroyImageInfo(imageinfo);
103 /* load given image to memory. return short RGB values */
104 unsigned short *load_img(int *width, int *height, const char *filename, int index)
107 unsigned short *img = NULL;
111 sprintf(line, filename, index);
112 // printf("reading image: %s\n", line);
113 fp = fopen(line, "r");
115 // printf("failed to read ppm image '%s'\n", filename);
119 if (!fgets(line, sizeof(line), fp)) {
120 printf("%s:failed to read image depth\n", __func__);
123 line[sizeof(line)-1] = '\0';
124 if (line[0]) line[strlen(line)-1] = '\0';
127 if (!!strcmp(line, "P6")) {
128 printf("%s:expecting image depth 'P6'\n", __func__);
132 if (!fgets(line, sizeof(line), fp)) {
133 printf("%s:failed to read image size\n", __func__);
136 line[sizeof(line)-1] = '\0';
137 if (line[0]) line[strlen(line)-1] = '\0';
140 sscanf(line, "%d %d", width, height);
141 // printf("Image size: w=%d h=%d\n", *width, *height);
143 if (!fgets(line, sizeof(line), fp)) {
144 printf("%s:failed to read line '255' or '65535'\n", __func__);
147 line[sizeof(line)-1] = '\0';
148 if (line[0]) line[strlen(line)-1] = '\0';
151 if (!strcmp(line, "255")) {
154 if (!strcmp(line, "65535")) {
157 printf("%s:expecting line '255' or '65535'\n", __func__);
161 img = (unsigned short *)malloc((*width) * (*height) * 3 * 2);
163 printf("%s:failed to allocate image data\n", __func__);
166 if (fread(img, (*width) * (*height) * 3 * words, 1, fp) != 1) {
167 printf("%s:failed to read image data\n", __func__);
171 /* char to short (255 -> 65535) */
173 unsigned char *from = (unsigned char *)img, c;
174 for (i = (*width) * (*height) * 3 - 1; i >= 0; i--) {
176 img[i] = (c << 8) | c;
179 /* correct byte order */
181 unsigned char *from = (unsigned char *)img;
182 for (i = 0; i < (*width) * (*height) * 3; i++) {
183 v = ((*from++) << 8);
196 /* save given image */
197 int save_img(unsigned short *img, int width, int height, int alpha, const char *filename, int index)
207 printf("%s:cannot save alpha component with PPM support only\n", __func__);
212 sprintf(line, filename, index);
213 // printf("writing image: %s\n", line);
214 fp = fopen(line, "w");
216 printf("%s:failed to write image\n", __func__);
219 fprintf(fp, "P6\n%d %d\n65535\n", width, height);
221 /* correct byte order, write and restore byte order */
222 to = (unsigned char *)img;
223 for (i = 0; i < width * height * 3; i++) {
225 if (i/100*i == i) { printf("%04x ", v); }
229 rc = fwrite(img, width * height * 3 * 2, 1, fp);
230 to = (unsigned char *)img;
231 for (i = 0; i < width * height * 3; i++) {
237 printf("%s:failed to write image data\n", __func__);
251 int save_img_array(double *array, int width, int height, int alpha, const char *filename, int index)
254 unsigned short *img = NULL;
259 printf("%s:warning, cannot save alpha component with PPM support only\n", __func__);
263 components = (alpha) ? 4 : 3;
265 img = (unsigned short *)malloc(width * height * components * 2);
267 printf("%s:failed to allocate image data\n", __func__);
271 array2img_short(array, width, height, img, width, height, alpha);
273 save_img(img, width, height, alpha, filename, index);
284 /* convert an image to a three dimensional array of double
285 * the size is: width, height, 3
287 void img2array_short(unsigned short *img, int iw, int ih, double *array, int aw, int ah)
295 for (y = 0; y < ih; y++) {
296 for (x = 0; x < iw; x++) {
297 r = img[(x+iw*y)*3] / 65535.0F;
298 g = img[(x+iw*y)*3+1] / 65535.0F;
299 b = img[(x+iw*y)*3+2] / 65535.0F;
301 array[x+aw*y+channel] = g;
302 array[x+aw*y+channel+channel] = b;
307 /* convert a three dimensional array of double to an image
308 * the size is: width, height, 3
310 void array2img_short(double *array, int aw, int ah, unsigned short *img, int iw, int ih, int alpha)
313 int channel, components;
317 components = (alpha) ? 4 : 3;
319 for (y = 0; y < ih; y++) {
320 for (x = 0; x < iw; x++) {
322 c = (r * 65535.0F + 0.5F);
327 img[(x+iw*y)*components] = c;
328 g = array[x+aw*y+channel];
329 c = (g * 65535.0F + 0.5F);
334 img[(x+iw*y)*components+1] = c;
335 b = array[x+aw*y+channel+channel];
336 c = (b * 65535.0F + 0.5F);
341 img[(x+iw*y)*components+2] = c;
343 a = array[x+aw*y+channel+channel+channel];
344 c = (a * 65535.0F + 0.5F);
349 img[(x+iw*y)*components+3] = c;
356 * scale down image in img_buffer by calculating average
358 void scale_img(unsigned short *img, int width, int height, int scale)
360 int w, h, i, j, x, y;
369 for (i = 0; i < h; i++) {
370 for (j = 0; j < w; j++) {
372 for (y = 0; y < scale; y++) {
373 for (x = 0; x < scale; x++) {
374 r += img[((i*scale+y) * width + j*scale+x) * 3 + 0];
375 g += img[((i*scale+y) * width + j*scale+x) * 3 + 1];
376 b += img[((i*scale+y) * width + j*scale+x) * 3 + 2];
379 img[(i * w + j)*3 + 0] = r / scale / scale;
380 img[(i * w + j)*3 + 1] = g / scale / scale;
381 img[(i * w + j)*3 + 2] = b / scale / scale;