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