| 1 |
/* $Id: pnmrawtopcropwhite.c,v 3.10 2011/03/14 20:14:00 krisrose Exp $ |
| 2 |
** |
| 3 |
** Crop off top white lines from raw PBM file (efficiently :) |
| 4 |
** Copyright (c) 1991-1998 Kristoffer H. Rose <[email protected]> |
| 5 |
** |
| 6 |
** This file is part of the Xy-pic package for graphs and diagrams in TeX. |
| 7 |
** Copyright (c) 1991-2011 Kristoffer H. Rose <[email protected]> |
| 8 |
** |
| 9 |
** The Xy-pic package is free software; you can redistribute it and/or modify |
| 10 |
** it under the terms of the GNU General Public License as published by the |
| 11 |
** Free Software Foundation; either version 2 of the License, or (at your |
| 12 |
** option) any later version. |
| 13 |
** |
| 14 |
** The Xy-pic package is distributed in the hope that it will be useful, but |
| 15 |
** WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 16 |
** or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 17 |
** for more details. |
| 18 |
** |
| 19 |
** You should have received a copy of the GNU General Public License along |
| 20 |
** with this package; if not, see http://www.gnu.org/licenses/. |
| 21 |
*/ |
| 22 |
static const char Id[] = |
| 23 |
"@(#)$Id: pnmrawtopcropwhite.c,v 3.10 2011/03/14 20:14:00 krisrose Exp $\n"; |
| 24 |
|
| 25 |
|
| 26 |
#include <stdio.h> |
| 27 |
#include <string.h> |
| 28 |
#include <stdlib.h> |
| 29 |
|
| 30 |
/* Globals */ |
| 31 |
|
| 32 |
#define PBMRAWMAGIC "P4" /* Portable BitMap magic cookie */ |
| 33 |
#define PGMRAWMAGIC "P5" /* Portable GrayMap magic cookie */ |
| 34 |
#define PPMRAWMAGIC "P6" /* Portable PixMap magic cookie */ |
| 35 |
#define MAGICSIZE 2 |
| 36 |
|
| 37 |
static char *me; /* program name */ |
| 38 |
|
| 39 |
/* Subroutines */ |
| 40 |
|
| 41 |
void oops(void) { |
| 42 |
fprintf(stderr, "Usage: %s\n", me); |
| 43 |
exit(1); |
| 44 |
} |
| 45 |
|
| 46 |
void panic(const char *mess) { |
| 47 |
fprintf(stderr, "Error: %s\nUsage: %s\n", mess, me); |
| 48 |
exit(2); |
| 49 |
} |
| 50 |
|
| 51 |
void skipcomment(void) { |
| 52 |
int c; |
| 53 |
while(1) { |
| 54 |
switch (c = getchar()) { |
| 55 |
case EOF: panic("premature EOF on input"); break; |
| 56 |
case '\n': return; |
| 57 |
default: break; |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
void skipwhite(void) { |
| 63 |
int c; |
| 64 |
while(1) { |
| 65 |
switch (c = getc(stdin)) { |
| 66 |
case EOF: panic("premature EOF on input"); break; |
| 67 |
case ' ': case '\t': case '\r': case '\n': break; |
| 68 |
case '#': skipcomment(); break; |
| 69 |
default: ungetc(c, stdin); return; |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
int nextint(const char *what) { |
| 75 |
int i; |
| 76 |
skipwhite(); |
| 77 |
if (!scanf("%d", &i)) panic(what); |
| 78 |
return i; |
| 79 |
} |
| 80 |
|
| 81 |
/* Program */ |
| 82 |
|
| 83 |
int main (int argc, char *argv[]) { |
| 84 |
|
| 85 |
char magic[MAGICSIZE]; |
| 86 |
enum { PBM, PGM, PPM } which; |
| 87 |
int width, height, newheight, pixelmax; |
| 88 |
|
| 89 |
int linelength; |
| 90 |
char *line, *whiteline; |
| 91 |
|
| 92 |
/* establish identity and proper usage */ |
| 93 |
me = argv[0]; |
| 94 |
if (argc != 1) oops(); |
| 95 |
|
| 96 |
/* magic */ |
| 97 |
if (!fread(magic, 1, MAGICSIZE, stdin)) |
| 98 |
panic("no magic (nothing on standard input)"); |
| 99 |
|
| 100 |
if (!memcmp(magic, PBMRAWMAGIC, MAGICSIZE)) |
| 101 |
which = PBM; |
| 102 |
else if (!memcmp(magic, PGMRAWMAGIC, MAGICSIZE)) |
| 103 |
which = PGM; |
| 104 |
else if (!memcmp(magic, PPMRAWMAGIC, MAGICSIZE)) |
| 105 |
which = PPM; |
| 106 |
else |
| 107 |
panic("bad magic (standard input not a raw anymap)"); |
| 108 |
|
| 109 |
/* width and height */ |
| 110 |
width = nextint("pixelwidth integer"); |
| 111 |
height = nextint("pixelheight integer"); |
| 112 |
switch (which) { |
| 113 |
case PBM: pixelmax = 0; break; /* hack (used by memset below) */ |
| 114 |
case PGM: case PPM: |
| 115 |
pixelmax = nextint("pixelmax integer"); |
| 116 |
if (pixelmax > 255) panic("pixelmax larger than 255"); |
| 117 |
} |
| 118 |
if (getchar() != '\n') |
| 119 |
panic("something not a simple newline after width height [pixelmax]"); |
| 120 |
|
| 121 |
/* allocate pixel line */ |
| 122 |
switch (which) { |
| 123 |
case PBM: linelength = (width + 7) / 8; break; |
| 124 |
case PGM: linelength = width; break; |
| 125 |
case PPM: linelength = width * 3; break; |
| 126 |
} |
| 127 |
line = (char*)malloc(linelength); |
| 128 |
if (!line) panic("could not allocate pixel line"); |
| 129 |
whiteline = (char*)malloc(linelength); |
| 130 |
if (!whiteline) panic("could not allocate white pixel line"); |
| 131 |
memset(whiteline, pixelmax, linelength); |
| 132 |
|
| 133 |
/* skip white pixel lines */ |
| 134 |
newheight = height; |
| 135 |
while (fread(line, linelength, 1, stdin) |
| 136 |
&& !memcmp(line, whiteline, linelength)) { |
| 137 |
newheight--; |
| 138 |
if (newheight <= 0) panic("entire image blank"); |
| 139 |
} |
| 140 |
|
| 141 |
/* output revised header */ |
| 142 |
if (newheight < height) |
| 143 |
fprintf(stderr,"%s: cropping %d rows off the top\n", me, height-newheight); |
| 144 |
printf("%.*s\n%d %d\n", MAGICSIZE, magic, width, newheight); |
| 145 |
switch (which) { |
| 146 |
case PBM: break; |
| 147 |
case PGM: case PPM: |
| 148 |
printf("%d\n", pixelmax); break; |
| 149 |
} |
| 150 |
|
| 151 |
/* output remainder of image */ |
| 152 |
do { |
| 153 |
if (!fwrite(line, linelength, 1, stdout)) panic("could not write"); |
| 154 |
} while (fread(line, linelength, 1, stdin)); |
| 155 |
if (!feof(stdin)) panic("input not exhausted"); |
| 156 |
|
| 157 |
return(0); |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
/* |
| 162 |
** $Log: pnmrawtopcropwhite.c,v $ |
| 163 |
** Revision 3.10 2011/03/14 20:14:00 krisrose |
| 164 |
** Preparing for release 3.8.6. |
| 165 |
** |
| 166 |
** Revision 3.9 2010/06/10 18:45:49 krisrose |
| 167 |
** Reference to GPL by URL. |
| 168 |
** |
| 169 |
** Revision 3.8 2010/04/16 06:06:51 krisrose |
| 170 |
** Preparing for a new release... |
| 171 |
** |
| 172 |
** Revision 3.7 1999/02/16 15:12:50 krisrose |
| 173 |
** Interim release (Y&Y fonts now free). |
| 174 |
** |
| 175 |
** Revision 1.3 1997/05/01 20:59:51 krisrose |
| 176 |
** Works for all three raw formats now. |
| 177 |
** |
| 178 |
** Revision 1.2 1997/05/01 17:34:58 krisrose |
| 179 |
** Extended to raw PGM and PPM in addition to PBM. |
| 180 |
** |
| 181 |
** Revision 1.1 1997/04/30 21:30:31 krisrose |
| 182 |
** Initial revision |
| 183 |
*/ |