-
Notifications
You must be signed in to change notification settings - Fork 0
/
tc_image.c
executable file
·451 lines (386 loc) · 10.2 KB
/
tc_image.c
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/*
* \file tc_image.c
* \brief A lightweight image class with I/O to PGM format.
* \author David Ray Thompson \r\n
*
* Copyright 2012, by the California Institute of Technology. ALL RIGHTS
* RESERVED. United States Government Sponsorship acknowledged. Any
* commercial use must be negotiated with the Office of Technology
* Transfer at the California Institute of Technology.
*/
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <string.h>
#include "tc_image.h"
#ifndef TC_IMAGE_C
#define TC_IMAGE_C
/** Translate unsigned int to pixel. */
pixel_t uchar_to_pixel(const unsigned char c)
{
return (pixel_t) c;
}
/** Translate int to pixel. */
pixel_t int_to_pixel(const int i)
{
return (pixel_t) i;
}
/** Translate pixel to unsigned char. */
unsigned char pixel_to_uchar(const pixel_t p)
{
return (unsigned char) (p);
}
/** log a message */
void tc_write_log(const char *msg)
{
fprintf(stderr, "%li| %s", time(NULL),msg);
}
/** get a pixel */
pixel_t tc_get(tc_image* img,
const int row,
const int col,
const int chan)
{
register int rowstride = img->cols * img->chans;
register int colstride = img->chans;
const int bandstride = 1;
return img->data[row*rowstride + col*colstride + chan*bandstride];
}
/** set a pixel */
void tc_set(tc_image* img,
const int row,
const int col,
const int chan,
const pixel_t val)
{
register int rowstride = img->cols * img->chans;
register int colstride = img->chans;
const int bandstride = 1;
img->data[row*rowstride + col*colstride + chan*bandstride] = val;
}
/** Allocate an image. */
int tc_alloc_image(
tc_image **image,
const int rows,
const int cols,
const int chans)
{
tc_image *img = NULL;
if (rows < 1 || cols < 1 || chans < 1)
{
tc_write_log("tc_alloc_img: invalid params.\r\n");
return ERR;
}
/* allocate base img struct */
img = (tc_image*) malloc(sizeof(tc_image));
if (img == NULL)
{
tc_write_log("tc_alloc_img: No memory for img.\r\n");
return ERR;
}
img->data = (pixel_t*) malloc(sizeof(pixel_t) * rows * cols * chans);
if (img->data == NULL)
{
tc_write_log("tc_alloc_img: No memory for img data.\r\n");
free(img);
return ERR;
}
img->rows = rows;
img->cols = cols;
img->chans = chans;
*image = img;
return OK;
}
/*! Free an image. */
int tc_free_image(tc_image *image)
{
if (image == NULL)
{
tc_write_log("tc_free_image: NULL pointer.\r\n");
return ERR;
}
if (image->data == NULL)
{
if (image->rows < 1 ||
image->cols < 1 ||
image->chans < 1)
{
return OK;
}
else
{
tc_write_log("tc_free_image: NULL data.\r\n");
free(image);
return ERR;
}
}
free(image->data);
free(image);
return OK;
}
/*
*! Read from a binary or ascii pgm image file.
* Thanks to Shelley Research Group for base code.
*/
int tc_read_image( tc_image **img, const char *filename)
{
FILE * file;
int r, b, c;
int rows, cols, chans, max;
int ch_int;
char type='0';
char *buf = (char *) malloc(MAX_STRING * sizeof(char));
tc_image *image;
if (!strstr(filename, ".pgm") &&
!strstr(filename, ".PGM") &&
!strstr(filename, ".ppm") &&
!strstr(filename, ".PPM"))
{
tc_write_log("tc_read_image: Don't recognize the file suffix.\r\n");
free(buf);
return ERR;
}
if ((file = fopen(filename, "rb")) == NULL)
{
snprintf(buf, MAX_STRING,
"tc_read_image: Can't open file %s for reading.\r\n", filename);
tc_write_log(buf);
free(buf);
return ERR;
}
/* read header */
char imgtype = (char) getc(file);
char* bandcode = malloc(sizeof(char)*2);
bandcode[0] = (char) getc(file);
bandcode[1] = '\0';
type = atoi(bandcode);
free(bandcode);
char eol = (char) getc(file);
if (eol != '\n' && eol != ' ')
{
tc_write_log("read_image: bad header format.\r\n");
free(buf);
return ERR;
}
switch (imgtype)
{
case 'P': /* pgm image */
switch (type)
{
case 1:
case 4:
tc_write_log("tc_read_image: Don't support bitmaps\r\n");
return ERR;
break;
case 2:
case 5:
chans = 1;
break;
case 3:
case 6:
chans = 3;
break;
default:
tc_write_log("read_image: bad header format.\r\n");
return ERR;
break;
}
break;
case 'H': /* custom format */
chans = type;
type = 0;
printf("read_image: custom format, %d channels.\r\n", chans);
break;
default:
tc_write_log("read_image: bad header format.\r\n");
free(buf);
return ERR;
}
/* read comment block */
while (getc(file) == '#') while (getc(file) != '\n');
/* read columns and rows, and max value */
fseek(file, -1, SEEK_CUR);
fscanf(file,"%d %d %d", &cols, &rows, &max);
/* Consume the final newline */
fgetc(file);
if (tc_alloc_image(&image, rows, cols, chans) == ERR)
{
fclose(file);
free(buf);
return ERR;
}
for (r=0; r < rows; r++)
{
for (c=0; c < cols; c++)
{
for (b=0; b < chans; b++)
{
switch (type)
{
case 2:
case 3:
/* ASCII */
if (fscanf(file,"%d", &ch_int) != 1)
{
tc_write_log("tc_read_image: Syntax error.\r\n");
tc_free_image(image);
img = NULL;
return ERR;
}
tc_set(image, r, c, b, int_to_pixel(ch_int));
break;
default:
/* binary */
tc_set(image, r, c, b, uchar_to_pixel(getc(file)));
break;
}
}
}
}
*img = image;
fclose(file);
free(buf);
return OK;
}
/*! Write a binary grayscale pgm. */
int tc_write_image(tc_image *img, const char *filename)
{
int r, b, c;
FILE * file;
if ((file = fopen(filename, "wb")) == NULL)
{
tc_write_log("tc_write_image: Can't open file for writing.\r\n");
return ERR;
}
if (!strstr(filename, ".pgm") &&
!strstr(filename, ".PGM") &&
!strstr(filename, ".PPM") &&
!strstr(filename, ".ppm"))
{
tc_write_log("tc_write_image: Don't recognize image type.");
fclose(file);
return ERR;
}
switch(img->chans)
{
case 1:
fprintf(file, "P5\n");
fprintf(file, "%d %d\n", img->cols, img->rows);
fprintf(file, "255\n");
break;
case 3:
fprintf(file, "P6\n");
fprintf(file, "%d %d\n", img->cols, img->rows);
fprintf(file, "255\n");
break;
default:
tc_write_log("tc_write_image: writing special pgm format");
/* our convention for channels > 3 */
fprintf(file, "H%i\n",img->chans);
fprintf(file, "%d %d\n", img->cols, img->rows);
fprintf(file, "255\n");
break;
}
for (r=0; r < img->rows; r++)
{
for (c=0; c < img->cols; c++)
{
for (b=0; b < img->chans; b++)
{
putc(pixel_to_uchar(tc_get(img, r, c, b)), file);
}
}
}
fclose(file);
return OK;
}
/*! Allocate a new image and fill it with a copy of src's data. */
int tc_clone_image(tc_image **dst, tc_image *src)
{
if (src == NULL || dst == NULL)
{
tc_write_log("clone_image: NULL image.\r\n");
return ERR;
}
if (src->rows < 1 || src->cols < 1 || src->chans < 1)
{
tc_write_log("clone_image: Bad dimensions.\r\n");
return ERR;
}
if (tc_alloc_image(dst, src->rows, src->cols, src->chans) == ERR)
{
return ERR;
}
return tc_copy_image(*dst, src);
}
/*! Allocate a new image and fill it with a cropped portion of
* the data in copy of src data. */
int tc_crop_image(tc_image **dst, tc_image *src, const int top,
const int left, const int height, const int width)
{
int b,c,r;
pixel_t x;
if (src == NULL || dst == NULL)
{
tc_write_log("crop_image: NULL image.\r\n");
return ERR;
}
if (src->rows < 1 || src->cols < 1 || src->chans < 1)
{
tc_write_log("clone_image: Bad dimensions.\r\n");
return ERR;
}
if ((top < 0) || (top >= src->rows) ||
(left < 0) || (left >= src->cols) ||
(height <= 0) || ((top+height) > src->rows) ||
(width <= 0) || ((left+width) > src->cols))
{
tc_write_log("crop_image: Bad subwindow dimensions.\r\n");
return ERR;
}
if (tc_alloc_image(dst, height, width, src->chans) == ERR)
{
tc_write_log("crop_image: Out of memory.\r\n");
return ERR;
}
for (b=0; b<(*dst)->chans; b++)
{
for (r=0; r<(*dst)->rows; r++)
{
for (c=0; c<(*dst)->cols; c++)
{
x = tc_get(src, top+r, left+c, b);
tc_set((*dst), r, c, b, x);
}
}
}
return OK;
}
/*! Copy src to dst (preallocated). */
int tc_copy_image(tc_image *dst, tc_image *src)
{
long int sz;
if (src == NULL || dst == NULL)
{
tc_write_log("tc_copy_image: NULL image.\r\n");
return ERR;
}
sz = sizeof(pixel_t) * src->rows * src->cols * src->chans;
if (src->rows != dst->rows ||
src->cols != dst->cols ||
src->chans != dst->chans)
{
tc_write_log("tc_copy_image: dimensions do not match.\r\n");
return ERR;
}
if (src->rows < 1 ||
src->cols < 1 ||
src->chans < 1)
{
tc_write_log("tc_copy_image: illegal image size.\r\n");
return ERR;
}
memcpy(dst->data, src->data, sz);
return OK;
}
#endif