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