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