1 /* pnmrw.h - header file for PBM/PGM/PPM read/write library
2 **
3 ** Copyright (C) 1988, 1989, 1991 by Jef Poskanzer.
4 **
5 ** Permission to use, copy, modify, and distribute this software and its
6 ** documentation for any purpose and without fee is hereby granted, provided
7 ** that the above copyright notice appear in all copies and that both that
8 ** copyright notice and this permission notice appear in supporting
9 ** documentation.  This software is provided "as is" without express or
10 ** implied warranty.
11 */
12 
13 /* $Id: libpnmrw.h,v 1.21 2005/03/20 20:15:34 demailly Exp $ */
14 
15 #ifndef _PNMRW_H_
16 #define _PNMRW_H_
17 
18 /* CONFIGURE: On some systems, malloc.h doesn't declare these, so we have
19 ** to do it.  On other systems, for example HP/UX, it declares them
20 ** incompatibly.  And some systems, for example Dynix, don't have a
21 ** malloc.h at all.  A sad situation.  If you have compilation problems
22 ** that point here, feel free to tweak or remove these declarations.
23 #include <malloc.h>
24 */
25 
26 /* End of configurable definitions. */
27 
28 
29 /* Types. */
30 
31 typedef unsigned char bit;
32 #define PBM_WHITE 0
33 #define PBM_BLACK 1
34 #define PBM_FORMAT_TYPE(f) \
35 	((f) == PBM_FORMAT || (f) == RPBM_FORMAT ? PBM_TYPE : -1)
36 
37 typedef unsigned char gray;
38 #define PGM_MAXMAXVAL 255
39 #define PGM_FORMAT_TYPE(f) \
40 	((f) == PGM_FORMAT || (f) == RPGM_FORMAT ? PGM_TYPE : PBM_FORMAT_TYPE(f))
41 
42 typedef gray pixval;
43 #define PPM_MAXMAXVAL PGM_MAXMAXVAL
44 typedef struct
45 {
46     pixval r, g, b;
47 } pixel;
48 #define PPM_GETR(p) ((p).r)
49 #define PPM_GETG(p) ((p).g)
50 #define PPM_GETB(p) ((p).b)
51 #define PPM_ASSIGN(p,red,grn,blu) \
52     do { (p).r = (red); (p).g = (grn); (p).b = (blu); } while (0)
53 #define PPM_EQUAL(p,q) ((p).r == (q).r && (p).g == (q).g && (p).b == (q).b)
54 #define PPM_FORMAT_TYPE(f) \
55     ((f) == PPM_FORMAT || (f) == RPPM_FORMAT ? PPM_TYPE : PGM_FORMAT_TYPE(f))
56 
57 typedef pixel xel;
58 typedef pixval xelval;
59 #define PNM_MAXMAXVAL PPM_MAXMAXVAL
60 #define PNM_GET1(x) PPM_GETB(x)
61 #define PNM_ASSIGN1(x,v) PPM_ASSIGN(x,0,0,v)
62 #define PNM_EQUAL(x,y) PPM_EQUAL(x,y)
63 #define PNM_FORMAT_TYPE(f) PPM_FORMAT_TYPE(f)
64 
65 
66 /* Magic constants. */
67 
68 #define PBM_MAGIC1 'P'
69 #define PBM_MAGIC2 '1'
70 #define RPBM_MAGIC2 '4'
71 #define PBM_FORMAT (PBM_MAGIC1 * 256 + PBM_MAGIC2)
72 #define RPBM_FORMAT (PBM_MAGIC1 * 256 + RPBM_MAGIC2)
73 #define PBM_TYPE PBM_FORMAT
74 
75 #define PGM_MAGIC1 'P'
76 #define PGM_MAGIC2 '2'
77 #define RPGM_MAGIC2 '5'
78 #define PGM_FORMAT (PGM_MAGIC1 * 256 + PGM_MAGIC2)
79 #define RPGM_FORMAT (PGM_MAGIC1 * 256 + RPGM_MAGIC2)
80 #define PGM_TYPE PGM_FORMAT
81 
82 #define PPM_MAGIC1 'P'
83 #define PPM_MAGIC2 '3'
84 #define RPPM_MAGIC2 '6'
85 #define PPM_FORMAT (PPM_MAGIC1 * 256 + PPM_MAGIC2)
86 #define RPPM_FORMAT (PPM_MAGIC1 * 256 + RPPM_MAGIC2)
87 #define PPM_TYPE PPM_FORMAT
88 
89 
90 /* Color scaling macro -- to make writing ppmtowhatever easier. */
91 
92 #define PPM_DEPTH(newp,p,oldmaxval,newmaxval) \
93     PPM_ASSIGN((newp), \
94 	((int) PPM_GETR(p) * (newmaxval) + (oldmaxval) / 2) / (oldmaxval), \
95 	((int) PPM_GETG(p) * (newmaxval) + (oldmaxval) / 2) / (oldmaxval), \
96 	((int) PPM_GETB(p) * (newmaxval) + (oldmaxval) / 2) / (oldmaxval))
97 
98 
99 /* Luminance macro. */
100 
101 #define PPM_LUMIN(p) (0.299*PPM_GETR(p) + 0.587*PPM_GETG(p) + 0.114*PPM_GETB(p))
102 
103 
104 /* Declarations of pnmrw routines. */
105 
106 void pnm_init2 (char* pn);
107 
108 char ** pm_allocarray (int cols, int rows, int size);
109 #define pnm_allocarray(cols, rows) \
110     ((xel**) pm_allocarray(cols, rows, sizeof(xel)))
111 char* pm_allocrow (int cols, int size);
112 #define pnm_allocrow(cols) ((xel*) pm_allocrow(cols, sizeof(xel))
113 void pm_freearray (char** its, int rows);
114 #define pnm_freearray(xels, rows) pm_freearray((char**) xels, rows)
115 void pm_freerow (char* itrow);
116 #define pnm_freerow(xelrow) pm_freerow((char*) xelrow)
117 xel** pnm_readpnm (FILE* file, int* colsP, int* rowsP,
118 		   xelval* maxvalP, int* formatP);
119 int pnm_readpnminit (FILE* file, int* colsP, int* rowsP,
120 		     xelval* maxvalP, int* formatP);
121 int pnm_readpnmrow (FILE* file, xel* xelrow, int cols,
122 		    xelval maxval, int format);
123 int pnm_writepnm (FILE* file, xel** xels, int cols, int rows,
124 		  xelval maxval, int format, int forceplain);
125 int pnm_writepnminit (FILE* file, int cols, int rows,
126 		      xelval maxval, int format, int forceplain);
127 int pnm_writepnmrow (FILE* file, xel* xelrow, int cols,
128 		     xelval maxval, int format, int forceplain);
129 
130 extern xelval pnm_pbmmaxval;
131 /* This is the maxval used when a PNM program reads a PBM file.	 Normally
132 ** it is 1; however, for some programs, a larger value gives better results.
133 */
134 
135 /* File open/close that handles "-" as stdin and checks errors. */
136 
137 FILE* pm_openr (char* name);
138 FILE* pm_openw (char* name);
139 int pm_closer (FILE* f);
140 int pm_closew (FILE* f);
141 
142 
143 /* Colormap stuff. */
144 
145 typedef struct colorhist_item* colorhist_vector;
146 struct colorhist_item
147 {
148     pixel color;
149     int value;
150 };
151 
152 typedef struct colorhist_list_item* colorhist_list;
153 struct colorhist_list_item
154 {
155     struct colorhist_item ch;
156     colorhist_list next;
157 };
158 
159 typedef colorhist_list* colorhash_table;
160 colorhist_vector ppm_computecolorhist (pixel** pixels, int cols, int rows,
161 				       int maxcolors, int* colorsP);
162 /* Returns a colorhist *colorsP long with space allocated for maxcolors. */
163 
164 void ppm_addtocolorhist (colorhist_vector chv, int* colorsP,
165 			 int maxcolors, pixel* colorP, int value, int position);
166 void ppm_freecolorhist (colorhist_vector chv);
167 colorhash_table ppm_computecolorhash (pixel** pixels, int cols, int rows,
168 				      int maxcolors, int* colorsP);
169 int ppm_lookupcolor (colorhash_table cht, pixel* colorP);
170 colorhist_vector ppm_colorhashtocolorhist (colorhash_table cht, int maxcolors);
171 colorhash_table ppm_colorhisttocolorhash (colorhist_vector chv, int colors);
172 int ppm_addtocolorhash (colorhash_table cht, pixel* colorP, int value);
173 /* Returns -1 on failure. */
174 colorhash_table ppm_alloccolorhash (void);
175 void ppm_freecolorhash (colorhash_table cht);
176 
177 #endif /*_PNMRW_H_*/
178