summaryrefslogtreecommitdiff
path: root/macros/generic/diagrams/xypic/support/pnmrawtopcropwhite.c
blob: ff6ab48a3c04995ce78d65063c541c4afb68eec8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/* $Id: pnmrawtopcropwhite.c,v 3.10 2011/03/14 20:14:00 krisrose Exp $
**
** Crop off top white lines from raw PBM file (efficiently :)
** Copyright (c) 1991-1998	 Kristoffer H. Rose  <krisrose@tug.org>
**
** This file is part of the Xy-pic package for graphs and diagrams in TeX.
** Copyright (c) 1991-2011	 Kristoffer H. Rose  <krisrose@tug.org>
**
** The Xy-pic package 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.
**
** The Xy-pic package 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 package; if not, see http://www.gnu.org/licenses/.
*/
static const char Id[] =
"@(#)$Id: pnmrawtopcropwhite.c,v 3.10 2011/03/14 20:14:00 krisrose Exp $\n";


#include <stdio.h>
#include <string.h>
#include <stdlib.h>

/* Globals */

#define PBMRAWMAGIC "P4"	/* Portable BitMap magic cookie */
#define PGMRAWMAGIC "P5"	/* Portable GrayMap magic cookie */
#define PPMRAWMAGIC "P6"	/* Portable PixMap magic cookie */
#define MAGICSIZE 2

static char *me;		/* program name */

/* Subroutines */

void oops(void) {
  fprintf(stderr, "Usage: %s\n", me);
  exit(1);
}

void panic(const char *mess) {
  fprintf(stderr, "Error: %s\nUsage: %s\n", mess, me);
  exit(2);
}

void skipcomment(void) {
  int c;
  while(1) {
    switch (c = getchar()) {
     case EOF: panic("premature EOF on input"); break;
     case '\n': return;
     default: break;
    }
  }
}

void skipwhite(void) {
  int c;
  while(1) {
    switch (c = getc(stdin)) {
     case EOF: panic("premature EOF on input"); break;
     case ' ': case '\t': case '\r': case '\n': break;
     case '#': skipcomment(); break;
     default: ungetc(c, stdin); return;
    }
  }
}

int nextint(const char *what) {
  int i;
  skipwhite();
  if (!scanf("%d", &i)) panic(what);
  return i;
}

/* Program */

int main (int argc, char *argv[]) {

  char magic[MAGICSIZE];
  enum { PBM, PGM, PPM } which;
  int width, height, newheight, pixelmax;

  int linelength;
  char *line, *whiteline;

  /* establish identity and proper usage */
  me = argv[0];
  if (argc != 1) oops();

  /* magic */
  if (!fread(magic, 1, MAGICSIZE, stdin))
    panic("no magic (nothing on standard input)");

  if (!memcmp(magic, PBMRAWMAGIC, MAGICSIZE))
    which = PBM;
  else if (!memcmp(magic, PGMRAWMAGIC, MAGICSIZE))
    which = PGM;
  else if (!memcmp(magic, PPMRAWMAGIC, MAGICSIZE))
    which = PPM;
  else
    panic("bad magic (standard input not a raw anymap)");

  /* width and height */
  width = nextint("pixelwidth integer");
  height = nextint("pixelheight integer");
  switch (which) {
   case PBM: pixelmax = 0; break; /* hack (used by memset below) */
   case PGM: case PPM:
    pixelmax = nextint("pixelmax integer");
    if (pixelmax > 255) panic("pixelmax larger than 255");
  }
  if (getchar() != '\n')
    panic("something not a simple newline after width height [pixelmax]");

  /* allocate pixel line */
  switch (which) {
   case PBM: linelength = (width + 7) / 8; break;
   case PGM: linelength = width; break;
   case PPM: linelength = width * 3; break;
  }
  line = (char*)malloc(linelength);
  if (!line) panic("could not allocate pixel line");
  whiteline = (char*)malloc(linelength);
  if (!whiteline) panic("could not allocate white pixel line");
  memset(whiteline, pixelmax, linelength);

  /* skip white pixel lines */
  newheight = height;
  while (fread(line, linelength, 1, stdin)
	 && !memcmp(line, whiteline, linelength)) {
    newheight--;
    if (newheight <= 0) panic("entire image blank");
  }

  /* output revised header */
  if (newheight < height)
    fprintf(stderr,"%s: cropping %d rows off the top\n", me, height-newheight);
  printf("%.*s\n%d %d\n", MAGICSIZE, magic, width, newheight);
  switch (which) {
   case PBM: break;
   case PGM: case PPM:
    printf("%d\n", pixelmax); break;
  }

  /* output remainder of image */
  do {
    if (!fwrite(line, linelength, 1, stdout)) panic("could not write");
  } while (fread(line, linelength, 1, stdin));
  if (!feof(stdin)) panic("input not exhausted");
  
  return(0);
}


/*
** $Log: pnmrawtopcropwhite.c,v $
** Revision 3.10  2011/03/14 20:14:00  krisrose
** Preparing for release 3.8.6.
**
** Revision 3.9  2010/06/10 18:45:49  krisrose
** Reference to GPL by URL.
**
** Revision 3.8  2010/04/16 06:06:51  krisrose
** Preparing for a new release...
**
** Revision 3.7  1999/02/16 15:12:50  krisrose
** Interim release (Y&Y fonts now free).
**
** Revision 1.3  1997/05/01 20:59:51  krisrose
** Works for all three raw formats now.
**
** Revision 1.2  1997/05/01 17:34:58  krisrose
** Extended to raw PGM and PPM in addition to PBM.
**
** Revision 1.1  1997/04/30 21:30:31  krisrose
** Initial revision
*/