textures, overlay images, libimago
[demo_prior] / libs / imago2 / src / file_ppm.c
1 /*
2 libimago - a multi-format image file input/output library.
3 Copyright (C) 2010-2017 John Tsiombikas <nuclear@member.fsf.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published
7 by the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /* -- Portable Pixmap (PPM) module (also supports PGM) -- */
20
21 #include <stdlib.h>
22 #include <string.h>
23 #include <ctype.h>
24 #include "imago2.h"
25 #include "ftype_module.h"
26 #include "byteord.h"
27
28 static int check(struct img_io *io);
29 static int read(struct img_pixmap *img, struct img_io *io);
30 static int write(struct img_pixmap *img, struct img_io *io);
31
32 int img_register_ppm(void)
33 {
34         static struct ftype_module mod = {".ppm:.pgm:.pnm", check, read, write};
35         return img_register_module(&mod);
36 }
37
38
39 static int check(struct img_io *io)
40 {
41         char id[2];
42         int res = -1;
43         long pos = io->seek(0, SEEK_CUR, io->uptr);
44
45         if(io->read(id, 2, io->uptr) < 2) {
46                 io->seek(pos, SEEK_SET, io->uptr);
47                 return -1;
48         }
49
50         if(id[0] == 'P' && (id[1] == '6' || id[1] == '3' || id[1] == '5')) {
51                 res = 0;
52         }
53         io->seek(pos, SEEK_SET, io->uptr);
54         return res;
55 }
56
57 static int iofgetc(struct img_io *io)
58 {
59         char c;
60         return io->read(&c, 1, io->uptr) < 1 ? -1 : c;
61 }
62
63 static char *iofgets(char *buf, int size, struct img_io *io)
64 {
65         int c;
66         char *ptr = buf;
67
68         while(--size > 0 && (c = iofgetc(io)) != -1) {
69                 *ptr++ = c;
70                 if(c == '\n') break;
71         }
72         *ptr = 0;
73
74         return ptr == buf ? 0 : buf;
75 }
76
77 static int read(struct img_pixmap *img, struct img_io *io)
78 {
79         char buf[256];
80         int xsz, ysz, maxval, got_hdrlines = 1;
81         int i, greyscale, numval, valsize, fbsize, text;
82         enum img_fmt fmt;
83
84         if(!iofgets(buf, sizeof buf, io)) {
85                 return -1;
86         }
87         if(!(buf[0] == 'P' && (buf[1] == '6' || buf[1] == '3' || buf[1] == '5'))) {
88                 return -1;
89         }
90         greyscale = buf[1] == '5' ? 1 : 0;
91         text = buf[1] == '3' ? 1 : 0;
92
93         while(got_hdrlines < 3 && iofgets(buf, sizeof buf, io)) {
94                 if(buf[0] == '#') continue;
95
96                 switch(got_hdrlines) {
97                 case 1:
98                         if(sscanf(buf, "%d %d\n", &xsz, &ysz) < 2) {
99                                 return -1;
100                         }
101                         break;
102
103                 case 2:
104                         if(sscanf(buf, "%d\n", &maxval) < 1) {
105                                 return -1;
106                         }
107                 default:
108                         break;
109                 }
110                 got_hdrlines++;
111         }
112
113         if(xsz < 1 || ysz < 1 || maxval <= 0 || maxval > 65535) {
114                 return -1;
115         }
116
117         valsize = maxval < 256 ? 1 : 2;
118         numval = xsz * ysz * (greyscale ? 1 : 3);
119         fbsize = numval * valsize;
120
121         if(valsize > 1) {
122                 fmt = greyscale ? IMG_FMT_GREYF : IMG_FMT_RGBF;
123         } else {
124                 fmt = greyscale ? IMG_FMT_GREY8 : IMG_FMT_RGB24;
125         }
126
127         if(img_set_pixels(img, xsz, ysz, fmt, 0) == -1) {
128                 return -1;
129         }
130
131         if(!text) {
132                 if(io->read(img->pixels, fbsize, io->uptr) < (unsigned int)fbsize) {
133                         return -1;
134                 }
135                 if(maxval == 255) {
136                         return 0;       /* we're done, no conversion necessary */
137                 }
138
139                 if(maxval < 256) {
140                         unsigned char *ptr = img->pixels;
141                         for(i=0; i<numval; i++) {
142                                 unsigned char c = *ptr * 255 / maxval;
143                                 *ptr++ = c;
144                         }
145                 } else {
146                         /* we allocated a floating point framebuffer, and dropped the 16bit pixels
147                          * into it. To convert it in-place we'll iterate backwards from the end, since
148                          * otherwise each 32bit floating point value we store, would overwrite the next
149                          * pixel.
150                          */
151                         uint16_t *src = (uint16_t*)img->pixels + numval;
152                         float *dest = (float*)img->pixels + numval;
153
154                         for(i=0; i<numval; i++) {
155                                 uint16_t val = *--src;
156 #ifdef IMAGO_LITTLE_ENDIAN
157                                 val = (val >> 8) | (val << 8);
158 #endif
159                                 *--dest = (float)val / (float)maxval;
160                         }
161                 }
162         } else {
163                 char *pptr = img->pixels;
164                 int c = iofgetc(io);
165
166                 for(i=0; i<numval; i++) {
167                         char *valptr = buf;
168
169                         while(c != -1 && isspace(c)) {
170                                 c = iofgetc(io);
171                         }
172
173                         while(c != -1 && !isspace(c) && valptr - buf < sizeof buf - 1) {
174                                 *valptr++ = c;
175                                 c = iofgetc(io);
176                         }
177                         if(c == -1) break;
178                         *valptr = 0;
179
180                         *pptr++ = atoi(buf) * 255 / maxval;
181                 }
182         }
183         return 0;
184 }
185
186 static int write(struct img_pixmap *img, struct img_io *io)
187 {
188         int i, sz, nval, res = -1;
189         char buf[256];
190         float *fptr, maxfval;
191         struct img_pixmap tmpimg;
192         static const char *fmt = "P%d\n#written by libimago2\n%d %d\n%d\n";
193         int greyscale = img_is_greyscale(img);
194
195         nval = greyscale ? 1 : 3;
196
197         img_init(&tmpimg);
198
199         switch(img->fmt) {
200         case IMG_FMT_RGBA32:
201                 if(img_copy(&tmpimg, img) == -1) {
202                         goto done;
203                 }
204                 if(img_convert(&tmpimg, IMG_FMT_RGB24) == -1) {
205                         goto done;
206                 }
207                 img = &tmpimg;
208
209         case IMG_FMT_RGB24:
210         case IMG_FMT_GREY8:
211                 sprintf(buf, fmt, greyscale ? 5 : 6, img->width, img->height, 255);
212                 if(io->write(buf, strlen(buf), io->uptr) < strlen(buf)) {
213                         goto done;
214                 }
215                 sz = img->width * img->height * nval;
216                 if(io->write(img->pixels, sz, io->uptr) < (unsigned int)sz) {
217                         goto done;
218                 }
219                 res = 0;
220                 break;
221
222         case IMG_FMT_RGBAF:
223                 if(img_copy(&tmpimg, img) == -1) {
224                         goto done;
225                 }
226                 if(img_convert(&tmpimg, IMG_FMT_RGBF) == -1) {
227                         goto done;
228                 }
229                 img = &tmpimg;
230
231         case IMG_FMT_RGBF:
232         case IMG_FMT_GREYF:
233                 sprintf(buf, fmt, greyscale ? 5 : 6, img->width, img->height, 65535);
234                 if(io->write(buf, strlen(buf), io->uptr) < strlen(buf)) {
235                         goto done;
236                 }
237                 fptr = img->pixels;
238                 maxfval = 0;
239                 for(i=0; i<img->width * img->height * nval; i++) {
240                         float val = *fptr++;
241                         if(val > maxfval) maxfval = val;
242                 }
243                 fptr = img->pixels;
244                 for(i=0; i<img->width * img->height * nval; i++) {
245                         uint16_t val = (uint16_t)(*fptr++ / maxfval * 65535.0);
246                         img_write_uint16_be(io, val);
247                 }
248                 res = 0;
249                 break;
250
251         default:
252                 break;
253         }
254
255 done:
256         img_destroy(&tmpimg);
257         return res;
258 }