istereo2

annotate libs/imago2/file_ppm.c @ 13:ea928c313344

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