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