7 #include <magick/api.h>
9 /* load given PPM image to memory. return memory pointer, witdth and height
10 * if offset is given, no memory is allocated */
11 int load_img(int offset, unsigned char **buffer, int *width, int *height,
12 const char *filename, int index)
16 ImageInfo *imageinfo = NULL;
17 ExceptionInfo exception;
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);
31 *width = image->columns;
32 *height = image->rows;
35 *buffer = (unsigned char *)malloc((*width) * (*height) * 3);
37 printf("failed to allocate image data\n");
42 // ExportImagePixels(image, 0, 0, *width, *height, "RGB", CharPixel, *buffer, NULL);
43 DispatchImage(image, 0, 0, *width, *height, "RGB", CharPixel, *buffer, NULL);
52 DestroyImageInfo(imageinfo);
54 // MagickCoreTerminus();
60 /* save given image */
61 int save_img(unsigned char *buffer, int width, int height, const char *filename,
66 ImageInfo *imageinfo = NULL;
67 ExceptionInfo exception;
69 // MagickCoreGenesis(NULL,MagickFalse);
70 InitializeMagick(NULL);
71 imageinfo = CloneImageInfo(0);
72 GetExceptionInfo(&exception);
74 imageinfo->quality = 100;
76 image=ConstituteImage(width, height, "RGB", CharPixel, buffer, &exception);
78 printf("failed to prepare to write image\n");
81 sprintf(image->filename, filename, index); /* ACHTUNG: nicht imageinfo!!! */
82 if (!WriteImage(imageinfo, image)) {
83 printf("failed to write image\n");
94 DestroyImageInfo(imageinfo);
96 // MagickCoreTerminus();
103 /* load given PPM image to memory. return memory pointer, witdth and height
104 * if offset is given, no memory is allocated */
105 int load_img(int offset, unsigned char **buffer, int *width, int *height,
106 const char *filename, int index)
112 sprintf(line, filename, index);
113 // printf("reading image: %s\n", line);
114 fp = fopen(line, "r");
116 // printf("failed to read ppm image '%s'\n", filename);
120 if (!fgets(line, sizeof(line), fp)) {
121 printf("failed to read image depth\n");
125 line[sizeof(line)-1] = '\0';
126 if (line[0]) line[strlen(line)-1] = '\0';
129 if (!!strcmp(line, "P6")) {
130 printf("expecting image depth 'P6'\n");
135 if (!fgets(line, sizeof(line), fp)) {
136 printf("failed to read image size\n");
140 line[sizeof(line)-1] = '\0';
141 if (line[0]) line[strlen(line)-1] = '\0';
144 sscanf(line, "%d %d", width, height);
145 // printf("Image size: w=%d h=%d\n", *width, *height);
147 if (!fgets(line, sizeof(line), fp)) {
148 printf("failed to read line '255' or '65535'\n");
152 line[sizeof(line)-1] = '\0';
153 if (line[0]) line[strlen(line)-1] = '\0';
156 if (!strcmp(line, "255")) {
159 if (!strcmp(line, "65535")) {
162 printf("expecting line '255' or '65535'\n");
167 *buffer = (unsigned char *)malloc((*width) * (*height) * 3 * words);
169 printf("failed to allocate image data\n");
175 if (fread((*buffer) + offset, (*width) * (*height) * 3 * words, 1, fp) != 1) {
176 printf("failed to read image data\n");
182 /* convert 16 to 8 bits */
184 for (i = 0; i < (*width) * (*height) * 3; i++)
185 (*buffer)[offset + i] = (*buffer)[offset + i*2];
193 /* save given image */
194 int save_img(unsigned char *buffer, int width, int height, const char *filename,
201 sprintf(line, filename, index);
202 // printf("writing image: %s\n", line);
203 fp = fopen(line, "w");
205 printf("failed to write image\n");
208 fprintf(fp, "P6\n%d %d\n255\n", width, height);
209 rc = fwrite(buffer, width * height * 3, 1, fp);
216 /* convert an image to a three dimensional array of double
217 * the size is: width, height, 3
219 void img2array(unsigned char *img, int iw, int ih, double *array, int aw,
226 for (y = 0; y < ih; y++) {
227 for (x = 0; x < iw; x++) {
228 r = img[(x+iw*y)*3] / 255.0F;
229 g = img[(x+iw*y)*3+1] / 255.0F;
230 b = img[(x+iw*y)*3+2] / 255.0F;
232 array[x+aw*y+plane] = g;
233 array[x+aw*y+plane+plane] = b;
238 /* convert a three dimensional array of double to an image
239 * the size is: width, height, 3
241 void array2img(double *array, int aw, int ah, unsigned char *img, int iw,
248 for (y = 0; y < ih; y++) {
249 for (x = 0; x < iw; x++) {
251 g = array[x+aw*y+plane];
252 b = array[x+aw*y+plane+plane];
253 c = (r * 255.0F + 0.5F);
259 c = (g * 255.0F + 0.5F);
264 img[(x+iw*y)*3+1] = c;
265 c = (b * 255.0F + 0.5F);
270 img[(x+iw*y)*3+2] = c;