istereo2
view libs/imago2/file_ppm.c @ 18:25d821ab1ca2
fixed ios bugs
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 01 Oct 2015 08:36:55 +0300 |
parents | |
children |
line source
1 /*
2 libimago - a multi-format image file input/output library.
3 Copyright (C) 2010 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/>.
17 */
19 /* -- Portable Pixmap (PPM) module -- */
21 #include <string.h>
22 #include "imago2.h"
23 #include "ftype_module.h"
25 static int check(struct img_io *io);
26 static int readppm(struct img_pixmap *img, struct img_io *io);
27 static int writeppm(struct img_pixmap *img, struct img_io *io);
29 int img_register_ppm(void)
30 {
31 static struct ftype_module mod = {".ppm", check, readppm, writeppm};
32 return img_register_module(&mod);
33 }
36 static int check(struct img_io *io)
37 {
38 char id[2];
39 int res = -1;
40 long pos = io->seek(0, SEEK_CUR, io->uptr);
42 if(io->read(id, 2, io->uptr) < 2) {
43 io->seek(pos, SEEK_SET, io->uptr);
44 return -1;
45 }
47 if(id[0] == 'P' && (id[1] == '6' || id[1] == '3')) {
48 res = 0;
49 }
50 io->seek(pos, SEEK_SET, io->uptr);
51 return res;
52 }
54 static int iofgetc(struct img_io *io)
55 {
56 char c;
57 return io->read(&c, 1, io->uptr) < 1 ? -1 : c;
58 }
60 static char *iofgets(char *buf, int size, struct img_io *io)
61 {
62 int c;
63 char *ptr = buf;
65 while(--size > 0 && (c = iofgetc(io)) != -1) {
66 *ptr++ = c;
67 if(c == '\n') break;
68 }
69 *ptr = 0;
71 return ptr == buf ? 0 : buf;
72 }
74 /* TODO: implement P3 reading */
75 static int readppm(struct img_pixmap *img, struct img_io *io)
76 {
77 char buf[256];
78 int xsz, ysz, maxval, got_hdrlines = 1;
80 if(!iofgets(buf, sizeof buf, io)) {
81 return -1;
82 }
83 if(!(buf[0] == 'P' && (buf[1] == '6' || buf[1] == '3'))) {
84 return -1;
85 }
87 while(got_hdrlines < 3 && iofgets(buf, sizeof buf, io)) {
88 if(buf[0] == '#') continue;
90 switch(got_hdrlines) {
91 case 1:
92 if(sscanf(buf, "%d %d\n", &xsz, &ysz) < 2) {
93 return -1;
94 }
95 break;
97 case 2:
98 if(sscanf(buf, "%d\n", &maxval) < 1) {
99 return -1;
100 }
101 default:
102 break;
103 }
104 got_hdrlines++;
105 }
107 if(xsz < 1 || ysz < 1 || maxval != 255) {
108 return -1;
109 }
111 if(img_set_pixels(img, xsz, ysz, IMG_FMT_RGB24, 0) == -1) {
112 return -1;
113 }
115 if(io->read(img->pixels, xsz * ysz * 3, io->uptr) < xsz * ysz * 3) {
116 return -1;
117 }
118 return 0;
119 }
121 static int writeppm(struct img_pixmap *img, struct img_io *io)
122 {
123 int sz;
124 char buf[256];
125 struct img_pixmap tmpimg;
127 img_init(&tmpimg);
129 if(img->fmt != IMG_FMT_RGB24) {
130 if(img_copy(&tmpimg, img) == -1) {
131 return -1;
132 }
133 if(img_convert(&tmpimg, IMG_FMT_RGB24) == -1) {
134 return -1;
135 }
136 img = &tmpimg;
137 }
139 sprintf(buf, "P6\n#written by libimago2\n%d %d\n255\n", img->width, img->height);
140 if(io->write(buf, strlen(buf), io->uptr) < strlen(buf)) {
141 img_destroy(&tmpimg);
142 return -1;
143 }
145 sz = img->width * img->height * 3;
146 if(io->write(img->pixels, sz, io->uptr) < sz) {
147 img_destroy(&tmpimg);
148 return -1;
149 }
151 img_destroy(&tmpimg);
152 return 0;
153 }