initial import
[dosrtxon] / libs / imago / src / fileppm.c
1 /*
2 libimago - a multi-format image file input/output library.
3 Copyright (C) 2010 John Tsiombikas <nuclear@member.fsf.org>
4
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.
9
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.
14
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 */
18
19 /* -- Portable Pixmap (PPM) module -- */
20
21 #include <string.h>
22 #include "imago2.h"
23 #include "ftmodule.h"
24
25 static int check(struct img_io *io);
26 static int read(struct img_pixmap *img, struct img_io *io);
27 static int write(struct img_pixmap *img, struct img_io *io);
28
29 int img_register_ppm(void)
30 {
31         static struct ftype_module mod = {".ppm", check, read, write};
32         return img_register_module(&mod);
33 }
34
35
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);
41
42         if(io->read(id, 2, io->uptr) < 2) {
43                 io->seek(pos, SEEK_SET, io->uptr);
44                 return -1;
45         }
46
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 }
53
54 static int iofgetc(struct img_io *io)
55 {
56         char c;
57         return io->read(&c, 1, io->uptr) < 1 ? -1 : c;
58 }
59
60 static char *iofgets(char *buf, int size, struct img_io *io)
61 {
62         int c;
63         char *ptr = buf;
64
65         while(--size > 0 && (c = iofgetc(io)) != -1) {
66                 *ptr++ = c;
67                 if(c == '\n') break;
68         }
69         *ptr = 0;
70
71         return ptr == buf ? 0 : buf;
72 }
73
74 /* TODO: implement P3 reading */
75 static int read(struct img_pixmap *img, struct img_io *io)
76 {
77         char buf[256];
78         int xsz, ysz, maxval, got_hdrlines = 1;
79
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         }
86
87         while(got_hdrlines < 3 && iofgets(buf, sizeof buf, io)) {
88                 if(buf[0] == '#') continue;
89
90                 switch(got_hdrlines) {
91                 case 1:
92                         if(sscanf(buf, "%d %d\n", &xsz, &ysz) < 2) {
93                                 return -1;
94                         }
95                         break;
96
97                 case 2:
98                         if(sscanf(buf, "%d\n", &maxval) < 1) {
99                                 return -1;
100                         }
101                 default:
102                         break;
103                 }
104                 got_hdrlines++;
105         }
106
107         if(xsz < 1 || ysz < 1 || maxval != 255) {
108                 return -1;
109         }
110
111         if(img_set_pixels(img, xsz, ysz, IMG_FMT_RGB24, 0) == -1) {
112                 return -1;
113         }
114
115         if(io->read(img->pixels, xsz * ysz * 3, io->uptr) < (unsigned int)(xsz * ysz * 3)) {
116                 return -1;
117         }
118         return 0;
119 }
120
121 static int write(struct img_pixmap *img, struct img_io *io)
122 {
123         int sz;
124         char buf[256];
125         struct img_pixmap tmpimg;
126
127         img_init(&tmpimg);
128
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         }
138
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         }
144
145         sz = img->width * img->height * 3;
146         if(io->write(img->pixels, sz, io->uptr) < (unsigned int)sz) {
147                 img_destroy(&tmpimg);
148                 return -1;
149         }
150
151         img_destroy(&tmpimg);
152         return 0;
153 }