7 #include <magick/api.h>
11 /* load given image to memory. return memory pointer, witdth and height
12 * if offset is given, no memory is allocated */
13 int load_img(int offset, unsigned short **buffer, int *width, int *height,
14 const char *filename, int index)
18 ImageInfo *imageinfo = NULL;
19 ExceptionInfo exception;
21 // MagickCoreGenesis(NULL,MagickFalse);
22 InitializeMagick(NULL);
23 imageinfo = CloneImageInfo(0);
24 GetExceptionInfo(&exception);
26 sprintf(imageinfo->filename, filename, index);
28 image = ReadImage(imageinfo, &exception);
30 // printf("failed to read image '%s' via *magick\n", filename);
33 *width = image->columns;
34 *height = image->rows;
37 *buffer = (unsigned short *)malloc((*width) * (*height) * 3 * sizeof(unsigned short));
39 printf("failed to allocate image data\n");
44 // ExportImagePixels(image, 0, 0, *width, *height, "RGB", CharPixel, *buffer, NULL);
45 DispatchImage(image, 0, 0, *width, *height, "RGB", ShortPixel, *buffer, NULL);
54 DestroyImageInfo(imageinfo);
56 // MagickCoreTerminus();
62 /* save given image */
63 int save_img(unsigned short *buffer, int width, int height, const char *filename,
68 ImageInfo *imageinfo = NULL;
69 ExceptionInfo exception;
71 // MagickCoreGenesis(NULL,MagickFalse);
72 InitializeMagick(NULL);
73 imageinfo = CloneImageInfo(0);
74 GetExceptionInfo(&exception);
76 imageinfo->quality = 100;
78 image=ConstituteImage(width, height, "RGB", ShortPixel, buffer, &exception);
80 printf("failed to prepare to write image\n");
84 /* store as 16 bit, if lib and format supports it */
85 image->depth = save_depth;
87 sprintf(image->filename, filename, index); /* ACHTUNG: nicht imageinfo!!! */
88 if (!WriteImage(imageinfo, image)) {
89 printf("failed to write image\n");
100 DestroyImageInfo(imageinfo);
102 // MagickCoreTerminus();
109 /* load given PPM image to memory. return memory pointer, witdth and height
110 * if offset is given, no memory is allocated */
111 int load_img(int offset, unsigned short **buffer, int *width, int *height,
112 const char *filename, int index)
118 sprintf(line, filename, index);
119 // printf("reading image: %s\n", line);
120 fp = fopen(line, "r");
122 // printf("failed to read ppm image '%s'\n", filename);
126 if (!fgets(line, sizeof(line), fp)) {
127 printf("failed to read image depth\n");
131 line[sizeof(line)-1] = '\0';
132 if (line[0]) line[strlen(line)-1] = '\0';
135 if (!!strcmp(line, "P6")) {
136 printf("expecting image depth 'P6'\n");
141 if (!fgets(line, sizeof(line), fp)) {
142 printf("failed to read image size\n");
146 line[sizeof(line)-1] = '\0';
147 if (line[0]) line[strlen(line)-1] = '\0';
150 sscanf(line, "%d %d", width, height);
151 // printf("Image size: w=%d h=%d\n", *width, *height);
153 if (!fgets(line, sizeof(line), fp)) {
154 printf("failed to read line '255' or '65535'\n");
158 line[sizeof(line)-1] = '\0';
159 if (line[0]) line[strlen(line)-1] = '\0';
162 if (!strcmp(line, "255")) {
165 if (!strcmp(line, "65535")) {
168 printf("expecting line '255' or '65535'\n");
173 *buffer = (unsigned short *)malloc((*width) * (*height) * 3 * words * sizeof(unsigned short));
175 printf("failed to allocate image data\n");
181 if (fread((*buffer) + offset, (*width) * (*height) * 3 * words, 1, fp) != 1) {
182 printf("failed to read image data\n");
188 /* convert 8 to 16 bits */
190 for (i = (*width) * (*height) * 3 - 1; i >= 0; i--) {
191 (*buffer)[offset + i] = ((unsigned char *)((*buffer) - offset + (i >> 1)))[0] << 8;
192 (*buffer)[offset + i] = ((unsigned char *)((*buffer) - offset + (i >> 1)))[1] << 8;
195 for (i = 0; i < (*width) * (*height) * 3; i++) {
196 /* correct bit order */
197 (*buffer)[offset + i] = ((unsigned char *)((*buffer) - offset + i))[0] << 8;
198 (*buffer)[offset + i] |= ((unsigned char *)((*buffer) - offset + i))[1];
207 /* save given image */
208 int save_img(unsigned short *buffer, int width, int height, const char *filename,
215 sprintf(line, filename, index);
216 // printf("writing image: %s\n", line);
217 fp = fopen(line, "w");
219 printf("failed to write image\n");
222 fprintf(fp, "P6\n%d %d\n65535\n", width, height);
223 for (i = 0; i < (*width) * (*height) * 3; i++) {
224 /* correct bit order */
225 rc = fputc(((unsigned char *)((*buffer) - offset + i))[0] >> 8, fp);
226 rc = fputc(((unsigned char *)((*buffer) - offset + i))[1], fp);
235 /* convert an image to a three dimensional array of double
236 * the size is: width, height, 3
238 void img2array(unsigned short *img, int iw, int ih, double *array, int aw,
245 for (y = 0; y < ih; y++) {
246 for (x = 0; x < iw; x++) {
247 r = img[(x+iw*y)*3] / 65535.0F;
248 g = img[(x+iw*y)*3+1] / 65535.0F;
249 b = img[(x+iw*y)*3+2] / 65535.0F;
251 array[x+aw*y+plane] = g;
252 array[x+aw*y+plane+plane] = b;
257 /* convert a three dimensional array of double to an image
258 * the size is: width, height, 3
260 void array2img(double *array, int aw, int ah, unsigned short *img, int iw,
267 for (y = 0; y < ih; y++) {
268 for (x = 0; x < iw; x++) {
270 g = array[x+aw*y+plane];
271 b = array[x+aw*y+plane+plane];
272 c = (r * 65535.0F + 0.5F);
278 c = (g * 65535.0F + 0.5F);
283 img[(x+iw*y)*3+1] = c;
284 c = (b * 65535.0F + 0.5F);
289 img[(x+iw*y)*3+2] = c;