aboutsummaryrefslogtreecommitdiffstats
path: root/src/libimage/img.c
blob: 42f2c43e6dcbd8eaf70be76818e8e2b8ca12a605 (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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "img.h"

int save_depth = 16;

#ifdef HAVE_MAGICK
#include <MagickCore/MagickCore.h>

/* load given image to memory. return short RGB values */
unsigned short *load_img(int *width, int *height, const char *filename, int index)
{
	Image *image = NULL;
	ImageInfo *imageinfo = NULL;
	ExceptionInfo *exception;
	unsigned short *img = NULL;

	MagickCoreGenesis(NULL, MagickFalse);
//	InitializeMagick(NULL);
	imageinfo = CloneImageInfo(0);
	exception = AcquireExceptionInfo();

	sprintf(imageinfo->filename, filename, index);

	image = ReadImage(imageinfo, exception);
	if (!image) {
//		printf("failed to read image '%s' via *magick\n", filename);
		goto exit;
	}

	*width = image->columns;
	*height = image->rows;

	img = (unsigned short *)malloc((*width) * (*height) * 3 * 2);
	if (!img) {
		printf("%s:failed to allocate image data\n", __func__);
		goto exit;
	}

	ExportImagePixels(image, 0, 0, *width, *height, "RGB", ShortPixel, img, NULL);
//	DispatchImage(image, 0, 0, *width, *height, "RGB", ShortPixel, img, NULL);

exit:
	if (image)
		DestroyImage(image);

	if (imageinfo)
		DestroyImageInfo(imageinfo);

	if (exception)
		DestroyExceptionInfo(exception);

	MagickCoreTerminus();
//	DestroyMagick();

	return img;
}

/* save given image */
int save_img(unsigned short *img, int width, int height, int alpha, const char *filename, int index)
{
	int rc = -1;
	Image *image = NULL;
	ImageInfo *imageinfo = NULL;
	ExceptionInfo *exception;

	MagickCoreGenesis(NULL, MagickFalse);
//	InitializeMagick(NULL);
	imageinfo = CloneImageInfo(0);
	exception = AcquireExceptionInfo();

	imageinfo->quality = 100;
	if (strlen(filename) >= 4 && !strcmp(filename + strlen(filename) - 4, ".png"))
		imageinfo->quality = 1;

	image=ConstituteImage(width, height, (alpha)?"RGBA":"RGB", ShortPixel, img, exception);
	if (!image) {
		printf("%s:failed to prepare to write image\n", __func__);
		goto exit;
	}

	/* store as 16 bit, if lib and format supports it */
	image->depth = save_depth;

	sprintf(image->filename, filename, index); /* ACHTUNG: nicht imageinfo!!! */
	if (!WriteImage(imageinfo, image, exception)) {
		printf("%s:failed to write image\n", __func__);
		goto exit;
	}

	rc = 0;

exit:
	if (image)
		DestroyImage(image);

	if (imageinfo)
		DestroyImageInfo(imageinfo);

	if (exception)
		DestroyExceptionInfo(exception);

	MagickCoreTerminus();
//	DestroyMagick();

	return rc;
}
#else

/* load given image to memory. return short RGB values */
unsigned short *load_img(int *width, int *height, const char *filename, int index)
{
	FILE *fp = NULL;
	unsigned short *img = NULL;
	char line[256];
	int words, i;

	sprintf(line, filename, index);
//	printf("reading image: %s\n", line);
	fp = fopen(line, "r");
	if (!fp) {
//		printf("failed to read ppm image '%s'\n", filename);
		goto exit;
	}
again1:
	if (!fgets(line, sizeof(line), fp)) {
		printf("%s:failed to read image depth\n", __func__);
		goto exit;
	}
	line[sizeof(line)-1] = '\0';
	if (line[0]) line[strlen(line)-1] = '\0';
	if (line[0] == '#')
		goto again1;
	if (!!strcmp(line, "P6")) {
		printf("%s:expecting image depth 'P6'\n", __func__);
		goto exit;
	}
again2:
	if (!fgets(line, sizeof(line), fp)) {
		printf("%s:failed to read image size\n", __func__);
		goto exit;
	}
	line[sizeof(line)-1] = '\0';
	if (line[0]) line[strlen(line)-1] = '\0';
	if (line[0] == '#')
		goto again2;
	sscanf(line, "%d %d", width, height);
//	printf("Image size: w=%d h=%d\n", *width, *height);
again3:
	if (!fgets(line, sizeof(line), fp)) {
		printf("%s:failed to read line '255' or '65535'\n", __func__);
		goto exit;
	}
	line[sizeof(line)-1] = '\0';
	if (line[0]) line[strlen(line)-1] = '\0';
	if (line[0] == '#')
		goto again3;
	if (!strcmp(line, "255")) {
		words = 1;
	} else
	if (!strcmp(line, "65535")) {
		words = 2;
	} else {
		printf("%s:expecting line '255' or '65535'\n", __func__);
		goto exit;
	}

	img = (unsigned short *)malloc((*width) * (*height) * 3 * 2);
	if (!img) {
		printf("%s:failed to allocate image data\n", __func__);
		goto exit;
	}
	if (fread(img, (*width) * (*height) * 3 * words, 1, fp) != 1) {
		printf("%s:failed to read image data\n", __func__);
		goto exit;
	}

	/* char to short (255 -> 65535) */
	if (words == 1) {
		unsigned char *from = (unsigned char *)img, c;
		for (i = (*width) * (*height) * 3 - 1; i >= 0; i--) {
			c = from[i];
			img[i] = (c << 8) | c;
		}
	} else {
		/* correct byte order */
		unsigned short v;
		unsigned char *from = (unsigned char *)img;
		for (i = 0; i < (*width) * (*height) * 3; i++) {
			v = ((*from++) << 8);
			v |= (*from++);
			img[i] = v;
		}
	}

exit:
	if (fp)
		fclose(fp);

	return img;
}

/* save given image */
int save_img(unsigned short *img, int width, int height, int alpha, const char *filename, int index)
{
	FILE *fp = NULL;
	int rc = -1;
	char line[256];
	int i;
	unsigned short v;
	unsigned char *to;

	if (alpha) {
		printf("%s:cannot save alpha component with PPM support only\n", __func__);
		alpha = 0;
		goto exit;
	}

	sprintf(line, filename, index);
//	printf("writing image: %s\n", line);
	fp = fopen(line, "w");
	if (!fp) {
		printf("%s:failed to write image\n", __func__);
		goto exit;
	}
	fprintf(fp, "P6\n%d %d\n65535\n", width, height);

	/* correct byte order, write and restore byte order */
	to = (unsigned char *)img;
	for (i = 0; i < width * height * 3; i++) {
		v = img[i];
		if (i/100*i == i) { printf("%04x ", v); }
		(*to++) = v >> 8;
		(*to++) = v;
	}
	rc = fwrite(img, width * height * 3 * 2, 1, fp);
	to = (unsigned char *)img;
	for (i = 0; i < width * height * 3; i++) {
		v = (*to++) << 8;
		v |= (*to++);
		img[i] = v;
	}
	if (rc != 1) {
		printf("%s:failed to write image data\n", __func__);
		goto exit;
	}

	rc = 0;

exit:
	if (fp)
		fclose(fp);

	return rc;
}
#endif

int save_img_array(double *array, int width, int height, int alpha, const char *filename, int index)
{
	int rc = -1;
	unsigned short *img = NULL;
	int components;

#ifndef HAVE_MAGICK
	if (alpha) {
		printf("%s:warning, cannot save alpha component with PPM support only\n", __func__);
		alpha = 0;
	}
#endif
	components = (alpha) ? 4 : 3;

	img = (unsigned short *)malloc(width * height * components * 2);
	if (!img) {
		printf("%s:failed to allocate image data\n", __func__);
		goto exit;
	}

	array2img_short(array, width, height, img, width, height, alpha);

	save_img(img, width, height, alpha, filename, index);

	rc = 0;

exit:
	if (img)
		free(img);

	return rc;
}

/* convert an image to a three dimensional array of double
 * the size is: width, height, 3
 */
void img2array_short(unsigned short *img, int iw, int ih, double *array, int aw, int ah)
{
	int x, y;
	int channel;
	double r, g, b;

	channel = aw * ah;

	for (y = 0; y < ih; y++) {
		for (x = 0; x < iw; x++) {
			r = img[(x+iw*y)*3] / 65535.0F;
			g = img[(x+iw*y)*3+1] / 65535.0F;
			b = img[(x+iw*y)*3+2] / 65535.0F;
			array[x+aw*y] = r;
			array[x+aw*y+channel] = g;
			array[x+aw*y+channel+channel] = b;
		}
	}
}

/* convert a three dimensional array of double to an image
 * the size is: width, height, 3
 */
void array2img_short(double *array, int aw, int ah, unsigned short *img, int iw, int ih, int alpha)
{
	int x, y, c;
	int channel, components;
	double r, g, b, a;

	channel = aw * ah;
	components = (alpha) ? 4 : 3;

	for (y = 0; y < ih; y++) {
		for (x = 0; x < iw; x++) {
			r = array[x+aw*y];
			c = (r * 65535.0F + 0.5F);
			if (c < 0)
				c = 0;
			else if (c > 65535)
				c = 65535;
			img[(x+iw*y)*components] = c;
			g = array[x+aw*y+channel];
			c = (g * 65535.0F + 0.5F);
			if (c < 0)
				c = 0;
			else if (c > 65535)
				c = 65535;
			img[(x+iw*y)*components+1] = c;
			b = array[x+aw*y+channel+channel];
			c = (b * 65535.0F + 0.5F);
			if (c < 0)
				c = 0;
			else if (c > 65535)
				c = 65535;
			img[(x+iw*y)*components+2] = c;
			if (alpha) {
				a = array[x+aw*y+channel+channel+channel];
				c = (a * 65535.0F + 0.5F);
				if (c < 0)
					c = 0;
				else if (c > 65535)
					c = 65535;
				img[(x+iw*y)*components+3] = c;
			}
		}
	}
}

/*
 * scale down image in img_buffer by calculating average
 */
void scale_img(unsigned short *img, int width, int height, int scale)
{
	int w, h, i, j, x, y;
	int r, g, b;

	if (scale == 1)
		return;

	w = width / scale;
	h = height / scale;

	for (i = 0; i < h; i++) {
		for (j = 0; j < w; j++) {
			r = g = b = 0;
			for (y = 0; y < scale; y++) {
				for (x = 0; x < scale; x++) {
					r += img[((i*scale+y) * width + j*scale+x) * 3 + 0];
					g += img[((i*scale+y) * width + j*scale+x) * 3 + 1];
					b += img[((i*scale+y) * width + j*scale+x) * 3 + 2];
				}
			}
			img[(i * w + j)*3 + 0] = r / scale / scale;
			img[(i * w + j)*3 + 1] = g / scale / scale;
			img[(i * w + j)*3 + 2] = b / scale / scale;
		}
	}
}