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