2 libimago - a multi-format image file input/output library.
3 Copyright (C) 2010-2017 John Tsiombikas <nuclear@member.fsf.org>
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.
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.
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/>.
19 /* -- Portable Pixmap (PPM) module (also supports PGM) -- */
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);
32 int img_register_ppm(void)
34 static struct ftype_module mod = {".ppm:.pgm:.pnm", check, read, write};
35 return img_register_module(&mod);
39 static int check(struct img_io *io)
43 long pos = io->seek(0, SEEK_CUR, io->uptr);
45 if(io->read(id, 2, io->uptr) < 2) {
46 io->seek(pos, SEEK_SET, io->uptr);
50 if(id[0] == 'P' && (id[1] == '6' || id[1] == '3' || id[1] == '5')) {
53 io->seek(pos, SEEK_SET, io->uptr);
57 static int iofgetc(struct img_io *io)
60 return io->read(&c, 1, io->uptr) < 1 ? -1 : c;
63 static char *iofgets(char *buf, int size, struct img_io *io)
68 while(--size > 0 && (c = iofgetc(io)) != -1) {
74 return ptr == buf ? 0 : buf;
77 static int read(struct img_pixmap *img, struct img_io *io)
80 int xsz, ysz, maxval, got_hdrlines = 1;
81 int i, greyscale, numval, valsize, fbsize, text;
84 if(!iofgets(buf, sizeof buf, io)) {
87 if(!(buf[0] == 'P' && (buf[1] == '6' || buf[1] == '3' || buf[1] == '5'))) {
90 greyscale = buf[1] == '5' ? 1 : 0;
91 text = buf[1] == '3' ? 1 : 0;
93 while(got_hdrlines < 3 && iofgets(buf, sizeof buf, io)) {
94 if(buf[0] == '#') continue;
96 switch(got_hdrlines) {
98 if(sscanf(buf, "%d %d\n", &xsz, &ysz) < 2) {
104 if(sscanf(buf, "%d\n", &maxval) < 1) {
113 if(xsz < 1 || ysz < 1 || maxval <= 0 || maxval > 65535) {
117 valsize = maxval < 256 ? 1 : 2;
118 numval = xsz * ysz * (greyscale ? 1 : 3);
119 fbsize = numval * valsize;
122 fmt = greyscale ? IMG_FMT_GREYF : IMG_FMT_RGBF;
124 fmt = greyscale ? IMG_FMT_GREY8 : IMG_FMT_RGB24;
127 if(img_set_pixels(img, xsz, ysz, fmt, 0) == -1) {
132 if(io->read(img->pixels, fbsize, io->uptr) < (unsigned int)fbsize) {
136 return 0; /* we're done, no conversion necessary */
140 unsigned char *ptr = img->pixels;
141 for(i=0; i<numval; i++) {
142 unsigned char c = *ptr * 255 / maxval;
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
151 uint16_t *src = (uint16_t*)img->pixels + numval;
152 float *dest = (float*)img->pixels + numval;
154 for(i=0; i<numval; i++) {
155 uint16_t val = *--src;
156 #ifdef IMAGO_LITTLE_ENDIAN
157 val = (val >> 8) | (val << 8);
159 *--dest = (float)val / (float)maxval;
163 char *pptr = img->pixels;
166 for(i=0; i<numval; i++) {
169 while(c != -1 && isspace(c)) {
173 while(c != -1 && !isspace(c) && valptr - buf < sizeof buf - 1) {
180 *pptr++ = atoi(buf) * 255 / maxval;
186 static int write(struct img_pixmap *img, struct img_io *io)
188 int i, sz, nval, res = -1;
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);
195 nval = greyscale ? 1 : 3;
201 if(img_copy(&tmpimg, img) == -1) {
204 if(img_convert(&tmpimg, IMG_FMT_RGB24) == -1) {
211 sprintf(buf, fmt, greyscale ? 5 : 6, img->width, img->height, 255);
212 if(io->write(buf, strlen(buf), io->uptr) < strlen(buf)) {
215 sz = img->width * img->height * nval;
216 if(io->write(img->pixels, sz, io->uptr) < (unsigned int)sz) {
223 if(img_copy(&tmpimg, img) == -1) {
226 if(img_convert(&tmpimg, IMG_FMT_RGBF) == -1) {
233 sprintf(buf, fmt, greyscale ? 5 : 6, img->width, img->height, 65535);
234 if(io->write(buf, strlen(buf), io->uptr) < strlen(buf)) {
239 for(i=0; i<img->width * img->height * nval; i++) {
241 if(val > maxfval) maxfval = val;
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);
256 img_destroy(&tmpimg);