dbf-halloween2015
view libs/imago/file_ppm.c @ 3:c37fe5d8a4ed
windows port
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 01 Nov 2015 06:04:28 +0200 |
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 readbuf(struct img_pixmap *img, struct img_io *io);
27 /*static int writebuf(struct img_pixmap *img, struct img_io *io);*/
29 int img_register_ppm(void)
30 {
31 static struct ftype_module mod = {".ppm", check, readbuf, 0};
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 readbuf(struct img_pixmap *img, struct img_io *io)
76 {
77 char buf[256];
78 int xsz = 0, ysz = 0, maxval = 0, 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((int)io->read(img->pixels, xsz * ysz * 3, io->uptr) < xsz * ysz * 3) {
116 return -1;
117 }
118 return 0;
119 }
121 /*
122 static int writebuf(struct img_pixmap *img, struct img_io *io)
123 {
124 int sz;
125 char buf[256];
126 struct img_pixmap tmpimg;
128 img_init(&tmpimg);
130 if(img->fmt != IMG_FMT_RGB24) {
131 if(img_copy(&tmpimg, img) == -1) {
132 return -1;
133 }
134 if(img_convert(&tmpimg, IMG_FMT_RGB24) == -1) {
135 return -1;
136 }
137 img = &tmpimg;
138 }
140 sprintf(buf, "P6\n#written by libimago2\n%d %d\n255\n", img->width, img->height);
141 if(io->write(buf, strlen(buf), io->uptr) < strlen(buf)) {
142 img_destroy(&tmpimg);
143 return -1;
144 }
146 sz = img->width * img->height * 3;
147 if((int)io->write(img->pixels, sz, io->uptr) < sz) {
148 img_destroy(&tmpimg);
149 return -1;
150 }
152 img_destroy(&tmpimg);
153 return 0;
154 }
155 */