eqemu

annotate libs/libimago/src/file_ppm.c @ 10:819c7ebb1bec

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