1 #   include	"bitmapConfig.h"
2 
3 #   include	<stdlib.h>
4 #   include	<stdio.h>
5 #   include	<ctype.h>
6 
7 #   include	"bmintern.h"
8 #   include	<sioFileio.h>
9 #   include	<appDebugon.h>
10 
11 /************************************************************************/
12 /*									*/
13 /*  Read a WBMP file.							*/
14 /*									*/
15 /************************************************************************/
16 
bmWbmpGetNumber(unsigned int * pNum,SimpleInputStream * sis)17 static int bmWbmpGetNumber(	unsigned int *		pNum,
18 				SimpleInputStream *	sis )
19     {
20     unsigned int	num= 0;
21     unsigned int	kontinue= 0x80;
22 
23     while( kontinue )
24 	{
25 	int	c= sioInGetByte( sis );
26 
27 	if  ( c == EOF )
28 	    { XDEB(c); return -1;	}
29 
30 	num= 128* num+ ( c & 0x7f );
31 	kontinue= c & 0x80;
32 	}
33 
34     *pNum= num; return 0;
35     }
36 
bmWbmpReadWbmp(BitmapDescription * bd,unsigned char ** pBuffer,SimpleInputStream * sis)37 static int bmWbmpReadWbmp(	BitmapDescription *	bd,
38 				unsigned char **	pBuffer,
39 				SimpleInputStream *	sis )
40     {
41     unsigned char *	buffer= (unsigned char *)0;
42     int			done;
43 
44     unsigned int	typeField;
45     int			fixHeaderField;
46 
47     if  ( bmWbmpGetNumber( &typeField, sis ) )
48 	{ LDEB(1); return -1;	}
49     if  ( typeField != 0 )
50 	{ LDEB(typeField); return -1;	}
51 
52     fixHeaderField= sioInGetByte( sis );
53     if  ( fixHeaderField & 0x80 )
54 	{ XDEB(fixHeaderField); return -1; }
55 
56     if  ( bmWbmpGetNumber( &(bd->bdPixelsWide), sis ) )
57 	{ LDEB(1); return -1;	}
58     if  ( bmWbmpGetNumber( &(bd->bdPixelsHigh), sis ) )
59 	{ LDEB(1); return -1;	}
60 
61     bd->bdXResolution= bd->bdYResolution= 1;
62     bd->bdUnit= BMunPIXEL;
63 
64     bd->bdColorEncoding= BMcoWHITEBLACK;
65     bd->bdSamplesPerPixel= 1;
66     bd->bdBitsPerSample= 1;
67     bd->bdBitsPerPixel= 1;
68 
69     bmCalculateSizes( bd );
70 
71     buffer= (unsigned char *)malloc( bd->bdBufferLength );
72     if  ( ! buffer )
73 	{ XDEB(buffer); return -1;	}
74 
75     done= sioInReadBytes( sis, buffer, bd->bdBufferLength );
76     if  ( done != bd->bdBufferLength )
77 	{ LLDEB(done,bd->bdBufferLength); free( buffer ); return -1; }
78 
79     *pBuffer= buffer;
80     return 0;
81     }
82 
bmReadWbmpFile(const MemoryBuffer * filename,unsigned char ** pBuffer,BitmapDescription * bd,int * pPrivateFormat)83 int bmReadWbmpFile(	const MemoryBuffer *	filename,
84 			unsigned char **	pBuffer,
85 			BitmapDescription *	bd,
86 			int *			pPrivateFormat )
87     {
88     SimpleInputStream *		sis;
89 
90     sis= sioInFileioOpen( filename );
91     if  ( ! sis )
92 	{ XDEB(sis); return -1;	}
93 
94     if  ( bmWbmpReadWbmp( bd, pBuffer, sis ) )
95 	{ LDEB(1); sioInClose( sis ); return -1; }
96 
97     *pPrivateFormat= 0;
98 
99     sioInClose( sis );
100 
101     return 0;
102     }
103 
104 /************************************************************************/
105 /*									*/
106 /*  Write a PGM file.							*/
107 /*									*/
108 /************************************************************************/
109 
bmCanWriteWbmpFile(const BitmapDescription * bd,int privateFormat)110 int bmCanWriteWbmpFile(	const BitmapDescription *	bd,
111 			int				privateFormat )
112     {
113     if  ( bd->bdColorEncoding != BMcoBLACKWHITE		&&
114 	  bd->bdColorEncoding != BMcoWHITEBLACK		)
115 	{ return -1;	}
116 
117     if  ( bd->bdBitsPerPixel > 1 )
118 	{ return -1;	}
119 
120     if  ( privateFormat != 0 )
121 	{ LDEB(privateFormat); return -1;	}
122 
123     return 0;
124     }
125 
bmWbmpPutNumber(int num,SimpleOutputStream * sos)126 static int bmWbmpPutNumber(	int			num,
127 				SimpleOutputStream *	sos )
128     {
129     unsigned int	kontinue= 0x00;
130     unsigned char	bytes[5];
131     int			n= 0;
132 
133     for (;;)
134 	{
135 	if  ( n >= sizeof(bytes) )
136 	    { LLDEB(n,sizeof(bytes)); break;	}
137 
138 	bytes[n]= num & 0x7f;
139 	num /= 128;
140 
141 	bytes[n++] |= kontinue;
142 
143 	if  ( num == 0 )
144 	    { break;	}
145 
146 	kontinue= 0x80;
147 	}
148 
149     n--;
150     while( n >= 0 )
151 	{
152 	if  ( sioOutPutByte( bytes[n], sos ) < 0 )
153 	    { return -1;	}
154 
155 	n--;
156 	}
157 
158     return 0;
159     }
160 
bmWriteWbmpFile(const MemoryBuffer * filename,const unsigned char * buffer,const BitmapDescription * bd,int privateFormat)161 int bmWriteWbmpFile(	const MemoryBuffer *		filename,
162 			const unsigned char *		buffer,
163 			const BitmapDescription *	bd,
164 			int				privateFormat )
165     {
166     int				rval= 0;
167     int				done;
168     SimpleOutputStream *	sos= (SimpleOutputStream *)0;
169 
170     sos= sioOutFileioOpen( filename );
171     if  ( ! sos )
172 	{ XDEB(sos); rval= -1; goto ready;	}
173 
174     if  ( privateFormat != 0 )
175 	{ LDEB(privateFormat); rval= -1; goto ready;	}
176 
177     bmWbmpPutNumber( privateFormat, sos );
178 
179     if  ( sioOutPutByte( 0, sos ) < 0 )
180 	{ rval= -1; goto ready;	}
181 
182     bmWbmpPutNumber( bd->bdPixelsWide, sos );
183     bmWbmpPutNumber( bd->bdPixelsHigh, sos );
184 
185     done= sioOutWriteBytes( sos, buffer, bd->bdBufferLength );
186     if  ( done != bd->bdBufferLength )
187 	{ LLDEB(done,bd->bdBufferLength); rval= -1; goto ready; }
188 
189   ready:
190 
191     if  ( sos )
192 	{ sioOutClose( sos );	}
193 
194 
195     return rval;
196     }
197 
198