rev |
line source |
nuclear@1
|
1 /*
|
nuclear@1
|
2 libimago - a multi-format image file input/output library.
|
nuclear@1
|
3 Copyright (C) 2010 John Tsiombikas <nuclear@member.fsf.org>
|
nuclear@1
|
4
|
nuclear@1
|
5 This program is free software: you can redistribute it and/or modify
|
nuclear@1
|
6 it under the terms of the GNU Lesser General Public License as published
|
nuclear@1
|
7 by the Free Software Foundation, either version 3 of the License, or
|
nuclear@1
|
8 (at your option) any later version.
|
nuclear@1
|
9
|
nuclear@1
|
10 This program is distributed in the hope that it will be useful,
|
nuclear@1
|
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
nuclear@1
|
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
nuclear@1
|
13 GNU Lesser General Public License for more details.
|
nuclear@1
|
14
|
nuclear@1
|
15 You should have received a copy of the GNU Lesser General Public License
|
nuclear@1
|
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
|
nuclear@1
|
17 */
|
nuclear@1
|
18
|
nuclear@1
|
19 /* -- Portable Pixmap (PPM) module -- */
|
nuclear@1
|
20
|
nuclear@1
|
21 #include <string.h>
|
nuclear@1
|
22 #include "imago2.h"
|
nuclear@1
|
23 #include "ftype_module.h"
|
nuclear@1
|
24
|
nuclear@1
|
25 static int check(struct img_io *io);
|
nuclear@1
|
26 static int readbuf(struct img_pixmap *img, struct img_io *io);
|
nuclear@1
|
27 /*static int writebuf(struct img_pixmap *img, struct img_io *io);*/
|
nuclear@1
|
28
|
nuclear@1
|
29 int img_register_ppm(void)
|
nuclear@1
|
30 {
|
nuclear@1
|
31 static struct ftype_module mod = {".ppm", check, readbuf, 0};
|
nuclear@1
|
32 return img_register_module(&mod);
|
nuclear@1
|
33 }
|
nuclear@1
|
34
|
nuclear@1
|
35
|
nuclear@1
|
36 static int check(struct img_io *io)
|
nuclear@1
|
37 {
|
nuclear@1
|
38 char id[2];
|
nuclear@1
|
39 int res = -1;
|
nuclear@1
|
40 long pos = io->seek(0, SEEK_CUR, io->uptr);
|
nuclear@1
|
41
|
nuclear@1
|
42 if(io->read(id, 2, io->uptr) < 2) {
|
nuclear@1
|
43 io->seek(pos, SEEK_SET, io->uptr);
|
nuclear@1
|
44 return -1;
|
nuclear@1
|
45 }
|
nuclear@1
|
46
|
nuclear@1
|
47 if(id[0] == 'P' && (id[1] == '6' || id[1] == '3')) {
|
nuclear@1
|
48 res = 0;
|
nuclear@1
|
49 }
|
nuclear@1
|
50 io->seek(pos, SEEK_SET, io->uptr);
|
nuclear@1
|
51 return res;
|
nuclear@1
|
52 }
|
nuclear@1
|
53
|
nuclear@1
|
54 static int iofgetc(struct img_io *io)
|
nuclear@1
|
55 {
|
nuclear@1
|
56 char c;
|
nuclear@1
|
57 return io->read(&c, 1, io->uptr) < 1 ? -1 : c;
|
nuclear@1
|
58 }
|
nuclear@1
|
59
|
nuclear@1
|
60 static char *iofgets(char *buf, int size, struct img_io *io)
|
nuclear@1
|
61 {
|
nuclear@1
|
62 int c;
|
nuclear@1
|
63 char *ptr = buf;
|
nuclear@1
|
64
|
nuclear@1
|
65 while(--size > 0 && (c = iofgetc(io)) != -1) {
|
nuclear@1
|
66 *ptr++ = c;
|
nuclear@1
|
67 if(c == '\n') break;
|
nuclear@1
|
68 }
|
nuclear@1
|
69 *ptr = 0;
|
nuclear@1
|
70
|
nuclear@1
|
71 return ptr == buf ? 0 : buf;
|
nuclear@1
|
72 }
|
nuclear@1
|
73
|
nuclear@1
|
74 /* TODO: implement P3 reading */
|
nuclear@1
|
75 static int readbuf(struct img_pixmap *img, struct img_io *io)
|
nuclear@1
|
76 {
|
nuclear@1
|
77 char buf[256];
|
nuclear@1
|
78 int xsz = 0, ysz = 0, maxval = 0, got_hdrlines = 1;
|
nuclear@1
|
79
|
nuclear@1
|
80 if(!iofgets(buf, sizeof buf, io)) {
|
nuclear@1
|
81 return -1;
|
nuclear@1
|
82 }
|
nuclear@1
|
83 if(!(buf[0] == 'P' && (buf[1] == '6' || buf[1] == '3'))) {
|
nuclear@1
|
84 return -1;
|
nuclear@1
|
85 }
|
nuclear@1
|
86
|
nuclear@1
|
87 while(got_hdrlines < 3 && iofgets(buf, sizeof buf, io)) {
|
nuclear@1
|
88 if(buf[0] == '#') continue;
|
nuclear@1
|
89
|
nuclear@1
|
90 switch(got_hdrlines) {
|
nuclear@1
|
91 case 1:
|
nuclear@1
|
92 if(sscanf(buf, "%d %d\n", &xsz, &ysz) < 2) {
|
nuclear@1
|
93 return -1;
|
nuclear@1
|
94 }
|
nuclear@1
|
95 break;
|
nuclear@1
|
96
|
nuclear@1
|
97 case 2:
|
nuclear@1
|
98 if(sscanf(buf, "%d\n", &maxval) < 1) {
|
nuclear@1
|
99 return -1;
|
nuclear@1
|
100 }
|
nuclear@1
|
101 default:
|
nuclear@1
|
102 break;
|
nuclear@1
|
103 }
|
nuclear@1
|
104 got_hdrlines++;
|
nuclear@1
|
105 }
|
nuclear@1
|
106
|
nuclear@1
|
107 if(xsz < 1 || ysz < 1 || maxval != 255) {
|
nuclear@1
|
108 return -1;
|
nuclear@1
|
109 }
|
nuclear@1
|
110
|
nuclear@1
|
111 if(img_set_pixels(img, xsz, ysz, IMG_FMT_RGB24, 0) == -1) {
|
nuclear@1
|
112 return -1;
|
nuclear@1
|
113 }
|
nuclear@1
|
114
|
nuclear@1
|
115 if((int)io->read(img->pixels, xsz * ysz * 3, io->uptr) < xsz * ysz * 3) {
|
nuclear@1
|
116 return -1;
|
nuclear@1
|
117 }
|
nuclear@1
|
118 return 0;
|
nuclear@1
|
119 }
|
nuclear@1
|
120
|
nuclear@1
|
121 /*
|
nuclear@1
|
122 static int writebuf(struct img_pixmap *img, struct img_io *io)
|
nuclear@1
|
123 {
|
nuclear@1
|
124 int sz;
|
nuclear@1
|
125 char buf[256];
|
nuclear@1
|
126 struct img_pixmap tmpimg;
|
nuclear@1
|
127
|
nuclear@1
|
128 img_init(&tmpimg);
|
nuclear@1
|
129
|
nuclear@1
|
130 if(img->fmt != IMG_FMT_RGB24) {
|
nuclear@1
|
131 if(img_copy(&tmpimg, img) == -1) {
|
nuclear@1
|
132 return -1;
|
nuclear@1
|
133 }
|
nuclear@1
|
134 if(img_convert(&tmpimg, IMG_FMT_RGB24) == -1) {
|
nuclear@1
|
135 return -1;
|
nuclear@1
|
136 }
|
nuclear@1
|
137 img = &tmpimg;
|
nuclear@1
|
138 }
|
nuclear@1
|
139
|
nuclear@1
|
140 sprintf(buf, "P6\n#written by libimago2\n%d %d\n255\n", img->width, img->height);
|
nuclear@1
|
141 if(io->write(buf, strlen(buf), io->uptr) < strlen(buf)) {
|
nuclear@1
|
142 img_destroy(&tmpimg);
|
nuclear@1
|
143 return -1;
|
nuclear@1
|
144 }
|
nuclear@1
|
145
|
nuclear@1
|
146 sz = img->width * img->height * 3;
|
nuclear@1
|
147 if((int)io->write(img->pixels, sz, io->uptr) < sz) {
|
nuclear@1
|
148 img_destroy(&tmpimg);
|
nuclear@1
|
149 return -1;
|
nuclear@1
|
150 }
|
nuclear@1
|
151
|
nuclear@1
|
152 img_destroy(&tmpimg);
|
nuclear@1
|
153 return 0;
|
nuclear@1
|
154 }
|
nuclear@1
|
155 */
|