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
|
/* ply-image.c - png file loader
*
* Copyright (C) 2006, 2007 Red Hat, Inc.
* Copyright (C) 2003 University of Southern California
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*
* Some implementation taken from the cairo library.
*
* Written by: Charlie Brej <cbrej@cs.man.ac.uk>
* Kristian Høgsberg <krh@redhat.com>
* Ray Strode <rstrode@redhat.com>
* Carl D. Worth <cworth@cworth.org>
*/
#include "config.h"
#include "ply-image.h"
#include "ply-pixel-buffer.h"
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <math.h>
#include <png.h>
#include <linux/fb.h>
#include "ply-utils.h"
struct _ply_image
{
char *filename;
ply_pixel_buffer_t *buffer;
};
struct bmp_file_header {
uint16_t id;
uint32_t file_size;
uint32_t reserved;
uint32_t bitmap_offset;
} __attribute__((__packed__));
struct bmp_dib_header {
uint32_t dib_header_size;
int32_t width;
int32_t height;
uint16_t planes;
uint16_t bpp;
uint32_t compression;
uint32_t bitmap_size;
uint32_t horz_resolution;
uint32_t vert_resolution;
uint32_t colors_used;
uint32_t colors_important;
} __attribute__((__packed__));
const uint8_t png_header[8] = { 0x89, 'P', 'N', 'G', 0x0d, 0x0a, 0x1a, 0x0a };
ply_image_t *
ply_image_new (const char *filename)
{
ply_image_t *image;
assert (filename != NULL);
image = calloc (1, sizeof(ply_image_t));
image->filename = strdup (filename);
image->buffer = NULL;
return image;
}
void
ply_image_free (ply_image_t *image)
{
if (image == NULL)
return;
assert (image->filename != NULL);
ply_pixel_buffer_free (image->buffer);
free (image->filename);
free (image);
}
static void
transform_to_argb32 (png_struct *png,
png_row_info *row_info,
png_byte *data)
{
unsigned int i;
for (i = 0; i < row_info->rowbytes; i += 4) {
uint8_t red, green, blue, alpha;
uint32_t pixel_value;
red = data[i + 0];
green = data[i + 1];
blue = data[i + 2];
alpha = data[i + 3];
/* pre-multiply the alpha if there's translucency */
if (alpha != 0xff) {
red = (uint8_t) CLAMP (((red / 255.0) * (alpha / 255.0)) * 255.0, 0, 255.0);
green = (uint8_t) CLAMP (((green / 255.0) * (alpha / 255.0)) * 255.0,
0, 255.0);
blue = (uint8_t) CLAMP (((blue / 255.0) * (alpha / 255.0)) * 255.0, 0, 255.0);
}
pixel_value = (alpha << 24) | (red << 16) | (green << 8) | (blue << 0);
memcpy (data + i, &pixel_value, sizeof(uint32_t));
}
}
static bool
ply_image_load_png (ply_image_t *image, FILE *fp)
{
png_struct *png;
png_info *info;
png_uint_32 width, height, row;
int bits_per_pixel, color_type, interlace_method;
png_byte **rows;
uint32_t *bytes;
assert (image != NULL);
assert (fp != NULL);
png = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
assert (png != NULL);
info = png_create_info_struct (png);
assert (info != NULL);
png_init_io (png, fp);
if (setjmp (png_jmpbuf (png)) != 0)
return false;
png_read_info (png, info);
png_get_IHDR (png, info,
&width, &height, &bits_per_pixel,
&color_type, &interlace_method, NULL, NULL);
if (color_type == PNG_COLOR_TYPE_PALETTE)
png_set_palette_to_rgb (png);
if ((color_type == PNG_COLOR_TYPE_GRAY) && (bits_per_pixel < 8))
png_set_expand_gray_1_2_4_to_8 (png);
if (png_get_valid (png, info, PNG_INFO_tRNS))
png_set_tRNS_to_alpha (png);
if (bits_per_pixel == 16)
png_set_strip_16 (png);
if (bits_per_pixel < 8)
png_set_packing (png);
if ((color_type == PNG_COLOR_TYPE_GRAY)
|| (color_type == PNG_COLOR_TYPE_GRAY_ALPHA))
png_set_gray_to_rgb (png);
if (interlace_method != PNG_INTERLACE_NONE)
png_set_interlace_handling (png);
png_set_filler (png, 0xff, PNG_FILLER_AFTER);
png_set_read_user_transform_fn (png, transform_to_argb32);
png_read_update_info (png, info);
rows = malloc (height * sizeof(png_byte *));
image->buffer = ply_pixel_buffer_new (width, height);
bytes = ply_pixel_buffer_get_argb32_data (image->buffer);
for (row = 0; row < height; row++) {
rows[row] = (png_byte *) &bytes[row * width];
}
png_read_image (png, rows);
free (rows);
png_read_end (png, info);
png_destroy_read_struct (&png, &info, NULL);
return true;
}
static bool
ply_image_load_bmp (ply_image_t *image, FILE *fp)
{
uint32_t x, y, src_y, width, height, bmp_pitch, *dst;
struct bmp_file_header file_header;
struct bmp_dib_header dib_header;
uint8_t r, g, b, *buf, *src;
bool ret = false;
assert (image != NULL);
assert (fp != NULL);
if (fread (&file_header, 1, sizeof(struct bmp_file_header), fp) != sizeof(struct bmp_file_header))
return false;
if (fread (&dib_header, 1, sizeof(struct bmp_dib_header), fp) != sizeof(struct bmp_dib_header))
return false;
if (dib_header.dib_header_size != 40 || dib_header.width < 0 ||
dib_header.planes != 1 || dib_header.bpp != 24 ||
dib_header.compression != 0)
return false;
width = dib_header.width;
height = abs (dib_header.height);
bmp_pitch = (3 * width + 3) & ~3;
buf = malloc (bmp_pitch * height);
assert (buf);
if (fseek (fp, file_header.bitmap_offset, SEEK_SET) != 0)
goto out;
if (fread (buf, 1, bmp_pitch * height, fp) != bmp_pitch * height)
goto out;
image->buffer = ply_pixel_buffer_new (width, height);
dst = ply_pixel_buffer_get_argb32_data (image->buffer);
for (y = 0; y < height; y++) {
/* Positive header height means upside down row order */
if (dib_header.height > 0)
src_y = (height - 1) - y;
else
src_y = y;
src = buf + src_y * bmp_pitch;
for (x = 0; x < width; x++) {
b = *src++;
g = *src++;
r = *src++;
*dst++ = (0xff << 24) | (r << 16) | (g << 8) | (b << 0);
}
}
ply_pixel_buffer_set_opaque (image->buffer, true);
ret = true;
out:
free (buf);
return ret;
}
bool
ply_image_load (ply_image_t *image)
{
uint8_t header[16];
bool ret = false;
FILE *fp;
assert (image != NULL);
fp = fopen (image->filename, "re");
if (fp == NULL)
return false;
if (fread (header, 1, 16, fp) != 16)
goto out;
/* Rewind */
if (fseek (fp, 0, SEEK_SET) != 0)
goto out;
if (memcmp (header, png_header, sizeof(png_header)) == 0)
ret = ply_image_load_png (image, fp);
else if (((struct bmp_file_header *)header)->id == 0x4d42 &&
((struct bmp_file_header *)header)->reserved == 0)
ret = ply_image_load_bmp (image, fp);
out:
fclose (fp);
return ret;
}
uint32_t *
ply_image_get_data (ply_image_t *image)
{
assert (image != NULL);
return ply_pixel_buffer_get_argb32_data (image->buffer);
}
long
ply_image_get_width (ply_image_t *image)
{
ply_rectangle_t size;
assert (image != NULL);
ply_pixel_buffer_get_size (image->buffer, &size);
return size.width;
}
long
ply_image_get_height (ply_image_t *image)
{
ply_rectangle_t size;
assert (image != NULL);
ply_pixel_buffer_get_size (image->buffer, &size);
return size.height;
}
ply_image_t *
ply_image_resize (ply_image_t *image,
long width,
long height)
{
ply_image_t *new_image;
new_image = ply_image_new (image->filename);
new_image->buffer = ply_pixel_buffer_resize (image->buffer,
width,
height);
return new_image;
}
ply_image_t *
ply_image_rotate (ply_image_t *image,
long center_x,
long center_y,
double theta_offset)
{
ply_image_t *new_image;
new_image = ply_image_new (image->filename);
new_image->buffer = ply_pixel_buffer_rotate (image->buffer,
center_x,
center_y,
theta_offset);
return new_image;
}
ply_image_t *
ply_image_tile (ply_image_t *image,
long width,
long height)
{
ply_image_t *new_image;
new_image = ply_image_new (image->filename);
new_image->buffer = ply_pixel_buffer_tile (image->buffer,
width,
height);
return new_image;
}
ply_pixel_buffer_t *
ply_image_get_buffer (ply_image_t *image)
{
assert (image != NULL);
return image->buffer;
}
ply_pixel_buffer_t *
ply_image_convert_to_pixel_buffer (ply_image_t *image)
{
ply_pixel_buffer_t *buffer;
assert (image != NULL);
buffer = image->buffer;
image->buffer = NULL;
ply_image_free (image);
return buffer;
}
/* vim: set ts=4 sw=4 expandtab autoindent cindent cino={.5s,(0: */
|