vrshoot

annotate libs/imago/file_ppm.c @ 2:334d17aed7de

visual studio project files
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 02 Feb 2014 18:36:38 +0200
parents
children
rev   line source
nuclear@0 1 /*
nuclear@0 2 libimago - a multi-format image file input/output library.
nuclear@0 3 Copyright (C) 2010 John Tsiombikas <nuclear@member.fsf.org>
nuclear@0 4
nuclear@0 5 This program is free software: you can redistribute it and/or modify
nuclear@0 6 it under the terms of the GNU Lesser General Public License as published
nuclear@0 7 by the Free Software Foundation, either version 3 of the License, or
nuclear@0 8 (at your option) any later version.
nuclear@0 9
nuclear@0 10 This program is distributed in the hope that it will be useful,
nuclear@0 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
nuclear@0 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
nuclear@0 13 GNU Lesser General Public License for more details.
nuclear@0 14
nuclear@0 15 You should have received a copy of the GNU Lesser General Public License
nuclear@0 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
nuclear@0 17 */
nuclear@0 18
nuclear@0 19 /* -- Portable Pixmap (PPM) module -- */
nuclear@0 20
nuclear@0 21 #include <string.h>
nuclear@0 22 #include "imago2.h"
nuclear@0 23 #include "ftype_module.h"
nuclear@0 24
nuclear@0 25 static int check(struct img_io *io);
nuclear@0 26 static int read(struct img_pixmap *img, struct img_io *io);
nuclear@0 27 static int write(struct img_pixmap *img, struct img_io *io);
nuclear@0 28
nuclear@0 29 int img_register_ppm(void)
nuclear@0 30 {
nuclear@0 31 static struct ftype_module mod = {".ppm", check, read, write};
nuclear@0 32 return img_register_module(&mod);
nuclear@0 33 }
nuclear@0 34
nuclear@0 35
nuclear@0 36 static int check(struct img_io *io)
nuclear@0 37 {
nuclear@0 38 char id[2];
nuclear@0 39 int res = -1;
nuclear@0 40 long pos = io->seek(0, SEEK_CUR, io->uptr);
nuclear@0 41
nuclear@0 42 if(io->read(id, 2, io->uptr) < 2) {
nuclear@0 43 io->seek(pos, SEEK_SET, io->uptr);
nuclear@0 44 return -1;
nuclear@0 45 }
nuclear@0 46
nuclear@0 47 if(id[0] == 'P' && (id[1] == '6' || id[1] == '3')) {
nuclear@0 48 res = 0;
nuclear@0 49 }
nuclear@0 50 io->seek(pos, SEEK_SET, io->uptr);
nuclear@0 51 return res;
nuclear@0 52 }
nuclear@0 53
nuclear@0 54 static int iofgetc(struct img_io *io)
nuclear@0 55 {
nuclear@0 56 char c;
nuclear@0 57 return io->read(&c, 1, io->uptr) < 1 ? -1 : c;
nuclear@0 58 }
nuclear@0 59
nuclear@0 60 static char *iofgets(char *buf, int size, struct img_io *io)
nuclear@0 61 {
nuclear@0 62 int c;
nuclear@0 63 char *ptr = buf;
nuclear@0 64
nuclear@0 65 while(--size > 0 && (c = iofgetc(io)) != -1) {
nuclear@0 66 *ptr++ = c;
nuclear@0 67 if(c == '\n') break;
nuclear@0 68 }
nuclear@0 69 *ptr = 0;
nuclear@0 70
nuclear@0 71 return ptr == buf ? 0 : buf;
nuclear@0 72 }
nuclear@0 73
nuclear@0 74 /* TODO: implement P3 reading */
nuclear@0 75 static int read(struct img_pixmap *img, struct img_io *io)
nuclear@0 76 {
nuclear@0 77 char buf[256];
nuclear@0 78 int xsz = 0, ysz = 0, maxval = 0, got_hdrlines = 1;
nuclear@0 79
nuclear@0 80 if(!iofgets(buf, sizeof buf, io)) {
nuclear@0 81 return -1;
nuclear@0 82 }
nuclear@0 83 if(!(buf[0] == 'P' && (buf[1] == '6' || buf[1] == '3'))) {
nuclear@0 84 return -1;
nuclear@0 85 }
nuclear@0 86
nuclear@0 87 while(got_hdrlines < 3 && iofgets(buf, sizeof buf, io)) {
nuclear@0 88 if(buf[0] == '#') continue;
nuclear@0 89
nuclear@0 90 switch(got_hdrlines) {
nuclear@0 91 case 1:
nuclear@0 92 if(sscanf(buf, "%d %d\n", &xsz, &ysz) < 2) {
nuclear@0 93 return -1;
nuclear@0 94 }
nuclear@0 95 break;
nuclear@0 96
nuclear@0 97 case 2:
nuclear@0 98 if(sscanf(buf, "%d\n", &maxval) < 1) {
nuclear@0 99 return -1;
nuclear@0 100 }
nuclear@0 101 default:
nuclear@0 102 break;
nuclear@0 103 }
nuclear@0 104 got_hdrlines++;
nuclear@0 105 }
nuclear@0 106
nuclear@0 107 if(xsz < 1 || ysz < 1 || maxval != 255) {
nuclear@0 108 return -1;
nuclear@0 109 }
nuclear@0 110
nuclear@0 111 if(img_set_pixels(img, xsz, ysz, IMG_FMT_RGB24, 0) == -1) {
nuclear@0 112 return -1;
nuclear@0 113 }
nuclear@0 114
nuclear@0 115 if((int)io->read(img->pixels, xsz * ysz * 3, io->uptr) < xsz * ysz * 3) {
nuclear@0 116 return -1;
nuclear@0 117 }
nuclear@0 118 return 0;
nuclear@0 119 }
nuclear@0 120
nuclear@0 121 static int write(struct img_pixmap *img, struct img_io *io)
nuclear@0 122 {
nuclear@0 123 int sz;
nuclear@0 124 char buf[256];
nuclear@0 125 struct img_pixmap tmpimg;
nuclear@0 126
nuclear@0 127 img_init(&tmpimg);
nuclear@0 128
nuclear@0 129 if(img->fmt != IMG_FMT_RGB24) {
nuclear@0 130 if(img_copy(&tmpimg, img) == -1) {
nuclear@0 131 return -1;
nuclear@0 132 }
nuclear@0 133 if(img_convert(&tmpimg, IMG_FMT_RGB24) == -1) {
nuclear@0 134 return -1;
nuclear@0 135 }
nuclear@0 136 img = &tmpimg;
nuclear@0 137 }
nuclear@0 138
nuclear@0 139 sprintf(buf, "P6\n#written by libimago2\n%d %d\n255\n", img->width, img->height);
nuclear@0 140 if(io->write(buf, strlen(buf), io->uptr) < strlen(buf)) {
nuclear@0 141 img_destroy(&tmpimg);
nuclear@0 142 return -1;
nuclear@0 143 }
nuclear@0 144
nuclear@0 145 sz = img->width * img->height * 3;
nuclear@0 146 if((int)io->write(img->pixels, sz, io->uptr) < sz) {
nuclear@0 147 img_destroy(&tmpimg);
nuclear@0 148 return -1;
nuclear@0 149 }
nuclear@0 150
nuclear@0 151 img_destroy(&tmpimg);
nuclear@0 152 return 0;
nuclear@0 153 }