3dphotoshoot

diff libs/imago/file_ppm.c @ 14:06dc8b9b4f89

added libimago, libjpeg and libpng
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 07 Jun 2015 17:25:49 +0300
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libs/imago/file_ppm.c	Sun Jun 07 17:25:49 2015 +0300
     1.3 @@ -0,0 +1,153 @@
     1.4 +/*
     1.5 +libimago - a multi-format image file input/output library.
     1.6 +Copyright (C) 2010 John Tsiombikas <nuclear@member.fsf.org>
     1.7 +
     1.8 +This program is free software: you can redistribute it and/or modify
     1.9 +it under the terms of the GNU Lesser General Public License as published
    1.10 +by the Free Software Foundation, either version 3 of the License, or
    1.11 +(at your option) any later version.
    1.12 +
    1.13 +This program is distributed in the hope that it will be useful,
    1.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.16 +GNU Lesser General Public License for more details.
    1.17 +
    1.18 +You should have received a copy of the GNU Lesser General Public License
    1.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
    1.20 +*/
    1.21 +
    1.22 +/* -- Portable Pixmap (PPM) module -- */
    1.23 +
    1.24 +#include <string.h>
    1.25 +#include "imago2.h"
    1.26 +#include "ftype_module.h"
    1.27 +
    1.28 +static int check(struct img_io *io);
    1.29 +static int read(struct img_pixmap *img, struct img_io *io);
    1.30 +static int write(struct img_pixmap *img, struct img_io *io);
    1.31 +
    1.32 +int img_register_ppm(void)
    1.33 +{
    1.34 +	static struct ftype_module mod = {".ppm", check, read, write};
    1.35 +	return img_register_module(&mod);
    1.36 +}
    1.37 +
    1.38 +
    1.39 +static int check(struct img_io *io)
    1.40 +{
    1.41 +	char id[2];
    1.42 +	int res = -1;
    1.43 +	long pos = io->seek(0, SEEK_CUR, io->uptr);
    1.44 +
    1.45 +	if(io->read(id, 2, io->uptr) < 2) {
    1.46 +		io->seek(pos, SEEK_SET, io->uptr);
    1.47 +		return -1;
    1.48 +	}
    1.49 +
    1.50 +	if(id[0] == 'P' && (id[1] == '6' || id[1] == '3')) {
    1.51 +		res = 0;
    1.52 +	}
    1.53 +	io->seek(pos, SEEK_SET, io->uptr);
    1.54 +	return res;
    1.55 +}
    1.56 +
    1.57 +static int iofgetc(struct img_io *io)
    1.58 +{
    1.59 +	char c;
    1.60 +	return io->read(&c, 1, io->uptr) < 1 ? -1 : c;
    1.61 +}
    1.62 +
    1.63 +static char *iofgets(char *buf, int size, struct img_io *io)
    1.64 +{
    1.65 +	int c;
    1.66 +	char *ptr = buf;
    1.67 +
    1.68 +	while(--size > 0 && (c = iofgetc(io)) != -1) {
    1.69 +		*ptr++ = c;
    1.70 +		if(c == '\n') break;
    1.71 +	}
    1.72 +	*ptr = 0;
    1.73 +
    1.74 +	return ptr == buf ? 0 : buf;
    1.75 +}
    1.76 +
    1.77 +/* TODO: implement P3 reading */
    1.78 +static int read(struct img_pixmap *img, struct img_io *io)
    1.79 +{
    1.80 +	char buf[256];
    1.81 +	int xsz, ysz, maxval, got_hdrlines = 1;
    1.82 +
    1.83 +	if(!iofgets(buf, sizeof buf, io)) {
    1.84 +		return -1;
    1.85 +	}
    1.86 +	if(!(buf[0] == 'P' && (buf[1] == '6' || buf[1] == '3'))) {
    1.87 +		return -1;
    1.88 +	}
    1.89 +
    1.90 +	while(got_hdrlines < 3 && iofgets(buf, sizeof buf, io)) {
    1.91 +		if(buf[0] == '#') continue;
    1.92 +
    1.93 +		switch(got_hdrlines) {
    1.94 +		case 1:
    1.95 +			if(sscanf(buf, "%d %d\n", &xsz, &ysz) < 2) {
    1.96 +				return -1;
    1.97 +			}
    1.98 +			break;
    1.99 +
   1.100 +		case 2:
   1.101 +			if(sscanf(buf, "%d\n", &maxval) < 1) {
   1.102 +				return -1;
   1.103 +			}
   1.104 +		default:
   1.105 +			break;
   1.106 +		}
   1.107 +		got_hdrlines++;
   1.108 +	}
   1.109 +
   1.110 +	if(xsz < 1 || ysz < 1 || maxval != 255) {
   1.111 +		return -1;
   1.112 +	}
   1.113 +
   1.114 +	if(img_set_pixels(img, xsz, ysz, IMG_FMT_RGB24, 0) == -1) {
   1.115 +		return -1;
   1.116 +	}
   1.117 +
   1.118 +	if(io->read(img->pixels, xsz * ysz * 3, io->uptr) < (unsigned int)(xsz * ysz * 3)) {
   1.119 +		return -1;
   1.120 +	}
   1.121 +	return 0;
   1.122 +}
   1.123 +
   1.124 +static int write(struct img_pixmap *img, struct img_io *io)
   1.125 +{
   1.126 +	int sz;
   1.127 +	char buf[256];
   1.128 +	struct img_pixmap tmpimg;
   1.129 +
   1.130 +	img_init(&tmpimg);
   1.131 +
   1.132 +	if(img->fmt != IMG_FMT_RGB24) {
   1.133 +		if(img_copy(&tmpimg, img) == -1) {
   1.134 +			return -1;
   1.135 +		}
   1.136 +		if(img_convert(&tmpimg, IMG_FMT_RGB24) == -1) {
   1.137 +			return -1;
   1.138 +		}
   1.139 +		img = &tmpimg;
   1.140 +	}
   1.141 +
   1.142 +	sprintf(buf, "P6\n#written by libimago2\n%d %d\n255\n", img->width, img->height);
   1.143 +	if(io->write(buf, strlen(buf), io->uptr) < strlen(buf)) {
   1.144 +		img_destroy(&tmpimg);
   1.145 +		return -1;
   1.146 +	}
   1.147 +
   1.148 +	sz = img->width * img->height * 3;
   1.149 +	if(io->write(img->pixels, sz, io->uptr) < (unsigned int)sz) {
   1.150 +		img_destroy(&tmpimg);
   1.151 +		return -1;
   1.152 +	}
   1.153 +
   1.154 +	img_destroy(&tmpimg);
   1.155 +	return 0;
   1.156 +}