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 184 185 186 187 188 189 190 191 192
|
# include "bitmapConfig.h"
# include <stdlib.h>
# include <stdio.h>
# include <ctype.h>
# include <string.h>
# include "bmintern.h"
# include <sioStdio.h>
# include <appDebugon.h>
/************************************************************************/
/* */
/* Read a WBMP file. */
/* */
/************************************************************************/
static int bmWbmpGetNumber( unsigned int * pNum,
SimpleInputStream * sis )
{
unsigned int num= 0;
unsigned int kontinue= 0x80;
while( kontinue )
{
int c= sioInGetCharacter( sis );
if ( c == EOF )
{ XDEB(c); return -1; }
num= 128* num+ ( c & 0x7f );
kontinue= c & 0x80;
}
*pNum= num; return 0;
}
static int bmWbmpReadWbmp( BitmapDescription * bd,
unsigned char ** pBuffer,
SimpleInputStream * sis )
{
unsigned char * buffer= (unsigned char *)0;
int done;
unsigned int typeField;
int fixHeaderField;
if ( bmWbmpGetNumber( &typeField, sis ) )
{ LDEB(1); return -1; }
if ( typeField != 0 )
{ LDEB(typeField); return -1; }
fixHeaderField= sioInGetCharacter( sis );
if ( fixHeaderField & 0x80 )
{ XDEB(fixHeaderField); return -1; }
if ( bmWbmpGetNumber( &(bd->bdPixelsWide), sis ) )
{ LDEB(1); return -1; }
if ( bmWbmpGetNumber( &(bd->bdPixelsHigh), sis ) )
{ LDEB(1); return -1; }
bd->bdXResolution= bd->bdYResolution= 1;
bd->bdUnit= BMunPIXEL;
bd->bdColorEncoding= BMcoWHITEBLACK;
bd->bdSamplesPerPixel= 1;
bd->bdBitsPerSample= 1;
bd->bdBitsPerPixel= 1;
bmCalculateSizes( bd );
buffer= (unsigned char *)malloc( bd->bdBufferLength );
if ( ! buffer )
{ XDEB(buffer); return -1; }
done= sioInReadBytes( sis, buffer, bd->bdBufferLength );
if ( done != bd->bdBufferLength )
{ LLDEB(done,bd->bdBufferLength); free( buffer ); return -1; }
*pBuffer= buffer;
return 0;
}
int bmReadWbmpFile( const char * filename,
unsigned char ** pBuffer,
BitmapDescription * bd,
int * pPrivateFormat,
double * pCompressionFactor )
{
SimpleInputStream * sis;
sis= sioInStdioOpen( filename );
if ( ! sis )
{ SXDEB(filename,sis); return -1; }
if ( bmWbmpReadWbmp( bd, pBuffer, sis ) )
{ SDEB(filename); sioInClose( sis ); return -1; }
*pPrivateFormat= 0;
sioInClose( sis );
return 0;
}
/************************************************************************/
/* */
/* Write a PGM file. */
/* */
/************************************************************************/
int bmCanWriteWbmpFile( const BitmapDescription * bd,
int privateFormat,
double compressionFactor )
{
if ( bd->bdColorEncoding != BMcoBLACKWHITE &&
bd->bdColorEncoding != BMcoWHITEBLACK )
{ return -1; }
if ( bd->bdBitsPerPixel > 1 )
{ return -1; }
if ( privateFormat != 0 )
{ LDEB(privateFormat); return -1; }
return 0;
}
static void bmWbmpPutNumber( int num,
SimpleOutputStream * sos )
{
unsigned int kontinue= 0x00;
unsigned char bytes[5];
int n= 0;
for (;;)
{
if ( n >= sizeof(bytes) )
{ LLDEB(n,sizeof(bytes)); break; }
bytes[n]= num & 0x7f;
num /= 128;
bytes[n++] |= kontinue;
if ( num == 0 )
{ break; }
kontinue= 0x80;
}
n--;
while( n >= 0 )
{
sioOutPutCharacter( bytes[n], sos );
n--;
}
return;
}
int bmWriteWbmpFile( const char * filename,
const unsigned char * buffer,
const BitmapDescription * bd,
int privateFormat,
double compressionFactor )
{
int done;
SimpleOutputStream * sos;
sos= sioOutStdioOpen( filename );
if ( ! sos )
{ SXDEB(filename,sos); return -1; }
if ( privateFormat != 0 )
{ LDEB(privateFormat); return -1; }
bmWbmpPutNumber( privateFormat, sos );
sioOutPutCharacter( 0, sos );
bmWbmpPutNumber( bd->bdPixelsWide, sos );
bmWbmpPutNumber( bd->bdPixelsHigh, sos );
done= sioOutWriteBytes( sos, buffer, bd->bdBufferLength );
if ( done != bd->bdBufferLength )
{ LLDEB(done,bd->bdBufferLength); sioOutClose( sos ); return -1; }
sioOutClose( sos );
return 0;
}
|