/* ** xvtoppm: A simple-minded filter to convert xv's .xvpics to ppm format. ** ** @(#)$Id: xvtoppm.c,v 5.3 1999/09/11 08:47:34 mark Exp $ ** ** xvtoppm - .xvpics -> .ppm filter ** Copyright (C) 1992-1999 Mark B. Hanson (mbh@panix.com) ** ** This program is free software; you can redistribute it and/or modify ** it under the terms of the GNU General Public License as published by ** the Free Software Foundation; either version 2 of the License, or ** (at your option) any later version. ** ** This program is distributed in the hope that it will be useful, ** but WITHOUT ANY WARRANTY; without even the implied warranty of ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ** GNU General Public License for more details. ** ** You should have received a copy of the GNU General Public License ** along with this program; if not, write to the Free Software ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ** ** to compile: ** cc -O xvtoppm.c -o xvtoppm ** ** to use: ** xvtoppm .xvpics/file.xv > file.ppm */ #include int main(argc, argv) int argc; char *argv[]; { FILE *file; char magic[3]; char cmap[4]; char buffer[256]; int width; int height; int maxval; unsigned char *data; int i; if (argc != 2) { fprintf(stderr, "%s: usage: %s xv-icon-file\n", argv[0], argv[0]); exit(1); } if ((file = fopen(argv[1], "r")) == NULL) { fprintf(stderr, "%s: fopen(%s) failed!\n", argv[0], argv[1]); exit(1); } if (fscanf(file, "%2s %3s\n", magic, cmap) != 2) { fprintf(stderr, "%s: failed to read magic number!\n", argv[0]); exit(1); } if (strncmp(magic, "P7", 2)) { fprintf(stderr, "%s: yikes! `%s' != `P7'!\n", argv[0], magic); exit(1); } do { if (fgets(buffer, 255, file) == NULL) { perror("fgets"); exit(1); } } while (buffer[0] == '#'); if (sscanf(buffer, "%d %d %d\n", &width, &height, &maxval) != 3) { fprintf(stderr, "%s: failed to read file dimensions!\n", argv[0]); exit(1); } if ((data = (unsigned char *)malloc(width * height)) == NULL) { fprintf(stderr, "%s: failed to malloc %d bytes!\n", argv[0], width * height); exit(1); } if (fread((char *)data, sizeof(char), width * height, file) != width * height) { fprintf(stderr, "%s: failed to read %d bytes!\n", argv[0], width * height); exit(1); } fprintf(stdout, "P6\n%d %d\n255\n", width, height); for (i = 0; i < width * height; i++) { fprintf(stdout, "%c%c%c", (data[i] & 0340), (data[i] & 0034) << 3, (data[i] & 0003) << 6); } free(data); if (fclose(file) == EOF) { fprintf(stderr, "%s: fclose(%s) failed!", argv[0], argv[1]); exit(1); } exit(0); } /* main */