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