nuclear@1: /* nuclear@1: libimago - a multi-format image file input/output library. nuclear@1: Copyright (C) 2010 John Tsiombikas nuclear@1: nuclear@1: This program is free software: you can redistribute it and/or modify nuclear@1: it under the terms of the GNU Lesser General Public License as published nuclear@1: by the Free Software Foundation, either version 3 of the License, or nuclear@1: (at your option) any later version. nuclear@1: nuclear@1: This program is distributed in the hope that it will be useful, nuclear@1: but WITHOUT ANY WARRANTY; without even the implied warranty of nuclear@1: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the nuclear@1: GNU Lesser General Public License for more details. nuclear@1: nuclear@1: You should have received a copy of the GNU Lesser General Public License nuclear@1: along with this program. If not, see . nuclear@1: */ nuclear@1: nuclear@1: /* -- Portable Pixmap (PPM) module -- */ nuclear@1: nuclear@1: #include nuclear@1: #include "imago2.h" nuclear@1: #include "ftype_module.h" nuclear@1: nuclear@1: static int check(struct img_io *io); nuclear@1: static int readbuf(struct img_pixmap *img, struct img_io *io); nuclear@1: /*static int writebuf(struct img_pixmap *img, struct img_io *io);*/ nuclear@1: nuclear@1: int img_register_ppm(void) nuclear@1: { nuclear@1: static struct ftype_module mod = {".ppm", check, readbuf, 0}; nuclear@1: return img_register_module(&mod); nuclear@1: } nuclear@1: nuclear@1: nuclear@1: static int check(struct img_io *io) nuclear@1: { nuclear@1: char id[2]; nuclear@1: int res = -1; nuclear@1: long pos = io->seek(0, SEEK_CUR, io->uptr); nuclear@1: nuclear@1: if(io->read(id, 2, io->uptr) < 2) { nuclear@1: io->seek(pos, SEEK_SET, io->uptr); nuclear@1: return -1; nuclear@1: } nuclear@1: nuclear@1: if(id[0] == 'P' && (id[1] == '6' || id[1] == '3')) { nuclear@1: res = 0; nuclear@1: } nuclear@1: io->seek(pos, SEEK_SET, io->uptr); nuclear@1: return res; nuclear@1: } nuclear@1: nuclear@1: static int iofgetc(struct img_io *io) nuclear@1: { nuclear@1: char c; nuclear@1: return io->read(&c, 1, io->uptr) < 1 ? -1 : c; nuclear@1: } nuclear@1: nuclear@1: static char *iofgets(char *buf, int size, struct img_io *io) nuclear@1: { nuclear@1: int c; nuclear@1: char *ptr = buf; nuclear@1: nuclear@1: while(--size > 0 && (c = iofgetc(io)) != -1) { nuclear@1: *ptr++ = c; nuclear@1: if(c == '\n') break; nuclear@1: } nuclear@1: *ptr = 0; nuclear@1: nuclear@1: return ptr == buf ? 0 : buf; nuclear@1: } nuclear@1: nuclear@1: /* TODO: implement P3 reading */ nuclear@1: static int readbuf(struct img_pixmap *img, struct img_io *io) nuclear@1: { nuclear@1: char buf[256]; nuclear@1: int xsz = 0, ysz = 0, maxval = 0, got_hdrlines = 1; nuclear@1: nuclear@1: if(!iofgets(buf, sizeof buf, io)) { nuclear@1: return -1; nuclear@1: } nuclear@1: if(!(buf[0] == 'P' && (buf[1] == '6' || buf[1] == '3'))) { nuclear@1: return -1; nuclear@1: } nuclear@1: nuclear@1: while(got_hdrlines < 3 && iofgets(buf, sizeof buf, io)) { nuclear@1: if(buf[0] == '#') continue; nuclear@1: nuclear@1: switch(got_hdrlines) { nuclear@1: case 1: nuclear@1: if(sscanf(buf, "%d %d\n", &xsz, &ysz) < 2) { nuclear@1: return -1; nuclear@1: } nuclear@1: break; nuclear@1: nuclear@1: case 2: nuclear@1: if(sscanf(buf, "%d\n", &maxval) < 1) { nuclear@1: return -1; nuclear@1: } nuclear@1: default: nuclear@1: break; nuclear@1: } nuclear@1: got_hdrlines++; nuclear@1: } nuclear@1: nuclear@1: if(xsz < 1 || ysz < 1 || maxval != 255) { nuclear@1: return -1; nuclear@1: } nuclear@1: nuclear@1: if(img_set_pixels(img, xsz, ysz, IMG_FMT_RGB24, 0) == -1) { nuclear@1: return -1; nuclear@1: } nuclear@1: nuclear@1: if((int)io->read(img->pixels, xsz * ysz * 3, io->uptr) < xsz * ysz * 3) { nuclear@1: return -1; nuclear@1: } nuclear@1: return 0; nuclear@1: } nuclear@1: nuclear@1: /* nuclear@1: static int writebuf(struct img_pixmap *img, struct img_io *io) nuclear@1: { nuclear@1: int sz; nuclear@1: char buf[256]; nuclear@1: struct img_pixmap tmpimg; nuclear@1: nuclear@1: img_init(&tmpimg); nuclear@1: nuclear@1: if(img->fmt != IMG_FMT_RGB24) { nuclear@1: if(img_copy(&tmpimg, img) == -1) { nuclear@1: return -1; nuclear@1: } nuclear@1: if(img_convert(&tmpimg, IMG_FMT_RGB24) == -1) { nuclear@1: return -1; nuclear@1: } nuclear@1: img = &tmpimg; nuclear@1: } nuclear@1: nuclear@1: sprintf(buf, "P6\n#written by libimago2\n%d %d\n255\n", img->width, img->height); nuclear@1: if(io->write(buf, strlen(buf), io->uptr) < strlen(buf)) { nuclear@1: img_destroy(&tmpimg); nuclear@1: return -1; nuclear@1: } nuclear@1: nuclear@1: sz = img->width * img->height * 3; nuclear@1: if((int)io->write(img->pixels, sz, io->uptr) < sz) { nuclear@1: img_destroy(&tmpimg); nuclear@1: return -1; nuclear@1: } nuclear@1: nuclear@1: img_destroy(&tmpimg); nuclear@1: return 0; nuclear@1: } nuclear@1: */