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