| 1 |
/* macptopbm.c - read a MacPaint file and produce a portable bitmap |
| 2 |
** |
| 3 |
** Copyright (C) 1988 by Jef Poskanzer. |
| 4 |
** Some code of ReadMacPaintFile() is based on the work of |
| 5 |
** Patrick J. Naughton. (C) 1987, All Rights Reserved. |
| 6 |
** |
| 7 |
** Permission to use, copy, modify, and distribute this software and its |
| 8 |
** documentation for any purpose and without fee is hereby granted, provided |
| 9 |
** that the above copyright notice appear in all copies and that both that |
| 10 |
** copyright notice and this permission notice appear in supporting |
| 11 |
** documentation. This software is provided "as is" without express or |
| 12 |
** implied warranty. |
| 13 |
|
| 14 |
|
| 15 |
** Apr 2015 afu |
| 16 |
** Changed code style (ANSI-style function definitions, etc.) |
| 17 |
** Added automatic detection of MacBinary header. |
| 18 |
** Added diagnostics for corruptions. |
| 19 |
** Replaced byte-wise operations with bit-wise ones. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "pbm.h" |
| 23 |
#include "pm_c_util.h" |
| 24 |
#include "macp.h" |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
static bool |
| 29 |
validateMacPaintVersion( const unsigned char * const rBuff, |
| 30 |
const int offset ) { |
| 31 |
/*--------------------------------------------------------------------------- |
| 32 |
Macpaint (or PNTG) files have two headers. |
| 33 |
The 512 byte MacPaint header is mandatory. |
| 34 |
The newer 128 byte MacBinary header is optional. If it exists, it comes |
| 35 |
before the MacPaint header. |
| 36 |
|
| 37 |
Here we examine the first four bytes of the MacPaint header to get |
| 38 |
the version number. |
| 39 |
|
| 40 |
Valid version numbers are 0, 2, 3. |
| 41 |
We also allow 1. |
| 42 |
-----------------------------------------------------------------------------*/ |
| 43 |
|
| 44 |
bool retval; |
| 45 |
const unsigned char * const vNum = rBuff + offset; |
| 46 |
|
| 47 |
if ( ( ( vNum[0] | vNum[1] | vNum[2] ) != 0x00 ) || vNum[3] > 3 ) |
| 48 |
retval = FALSE; |
| 49 |
else |
| 50 |
retval = TRUE; |
| 51 |
|
| 52 |
pm_message("MacPaint version (at offset %u): %02x %02x %02x %02x (%s)", |
| 53 |
offset, vNum[0], vNum[1], vNum[2], vNum[3], |
| 54 |
retval == TRUE ? "valid" : "not valid" ); |
| 55 |
|
| 56 |
return( retval ); |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
static bool |
| 62 |
scanMacBinaryHeader( const unsigned char * rBuff ) { |
| 63 |
/*---------------------------------------------------------------------------- |
| 64 |
We check byte 0 and 1, and then the MacPaint header version assuming it |
| 65 |
starts at offset 128. |
| 66 |
|
| 67 |
Byte 0: must be 0x00. |
| 68 |
Byte 1: (filename length) must be 1-63. |
| 69 |
|
| 70 |
Other fields that may be of interest: |
| 71 |
|
| 72 |
Bytes 2 through 63: (Internal Filename) |
| 73 |
See Apple Charmap for valid characters. |
| 74 |
Unlike US-Ascii, 8-bit characters (range 0x80 - 0xFF) are valid. |
| 75 |
0x00-0x1F and 0x7F are control characters. 0x00 appears in some files. |
| 76 |
Colon ':' (0x3a) should be avoided in Mac environments but in practice |
| 77 |
does appear. |
| 78 |
|
| 79 |
Bytes 65 through 68: (File Type) |
| 80 |
Four Ascii characters. Should be "PNTG". |
| 81 |
|
| 82 |
Bytes 82 to 85: (SizeOfDataFork) |
| 83 |
uint32 value. It seems this is file size (in bytes) / 256 + N, N <= 4. |
| 84 |
|
| 85 |
Bytes 100 through 124: |
| 86 |
Should be all zero if the header is MacBinary I. |
| 87 |
Defined and used in MacBinary II. |
| 88 |
|
| 89 |
Bytes 124,125: CRC |
| 90 |
(MacBinary II only) CRC value of bytes 0 through 123. |
| 91 |
|
| 92 |
All multi-byte values are big-endian. |
| 93 |
|
| 94 |
Reference: |
| 95 |
http://www.fileformat.info/format/macpaint/egff.htm |
| 96 |
Fully describes the fields. However, the detection method described |
| 97 |
does not work very well. |
| 98 |
|
| 99 |
Also see: |
| 100 |
http://fileformats.archiveteam.org/wiki/MacPaint |
| 101 |
-----------------------------------------------------------------------------*/ |
| 102 |
bool foundMacBinaryHeader; |
| 103 |
|
| 104 |
/* Examine byte 0. It should be 0x00. Note that the first |
| 105 |
byte of a valid MacPaint header should also be 0x00. |
| 106 |
*/ |
| 107 |
if ( rBuff[0] != 0x00 ) { |
| 108 |
foundMacBinaryHeader = FALSE; |
| 109 |
} |
| 110 |
|
| 111 |
/* Examine byte 1, the length of the filename. |
| 112 |
It should be in the range 1 - 63. |
| 113 |
*/ |
| 114 |
else if( rBuff[1] == 0 || rBuff[1] > 63 ) { |
| 115 |
foundMacBinaryHeader = FALSE; |
| 116 |
} |
| 117 |
|
| 118 |
/* Check the MacPaint header version starting at offset 128. */ |
| 119 |
else if ( validateMacPaintVersion ( rBuff, MACBIN_HEAD_LEN ) == FALSE) { |
| 120 |
foundMacBinaryHeader = FALSE; |
| 121 |
} |
| 122 |
else |
| 123 |
foundMacBinaryHeader = TRUE; |
| 124 |
|
| 125 |
if( foundMacBinaryHeader == TRUE) |
| 126 |
pm_message("Input file contains a MacBinary header " |
| 127 |
"followed by a MacPaint header."); |
| 128 |
else |
| 129 |
pm_message("Input file does not start with a MacBinary header."); |
| 130 |
|
| 131 |
return ( foundMacBinaryHeader ); |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
static void |
| 137 |
skipHeader( FILE * const ifP ) { |
| 138 |
/*-------------------------------------------------------------------------- |
| 139 |
Determine whether the MacBinary header exists. |
| 140 |
If it does, read off the initial 640 (=128 + 512) bytes of the file. |
| 141 |
If it doesn't, read off 512 bytes. |
| 142 |
|
| 143 |
In the latter case we check the MacHeader version number, but just issue |
| 144 |
a warning if the value is invalid. This is for backward comaptibility. |
| 145 |
---------------------------------------------------------------------------*/ |
| 146 |
unsigned int re; |
| 147 |
const unsigned int buffsize = MAX( MACBIN_HEAD_LEN, MACP_HEAD_LEN ); |
| 148 |
unsigned char * const rBuff = malloc(buffsize); |
| 149 |
|
| 150 |
if( rBuff == NULL ) |
| 151 |
pm_error("Out of memory."); |
| 152 |
|
| 153 |
/* Read 512 bytes. |
| 154 |
See if MacBinary header exists in the first 128 bytes and |
| 155 |
the next 4 bytes signal the start of a MacPaint header. */ |
| 156 |
re = fread ( rBuff, MACP_HEAD_LEN, 1, ifP); |
| 157 |
if (re < 1) |
| 158 |
pm_error("EOF/error while reading header."); |
| 159 |
|
| 160 |
if ( scanMacBinaryHeader( rBuff ) == TRUE ) { |
| 161 |
/* MacBinary header found. Read another 128 bytes to complete the |
| 162 |
MacPaint header, but don't conduct any further analysis. */ |
| 163 |
re = fread ( rBuff, MACBIN_HEAD_LEN, 1, ifP); |
| 164 |
if (re < 1) |
| 165 |
pm_error("EOF/error while reading MacPaint header."); |
| 166 |
|
| 167 |
} else { |
| 168 |
/* MacBinary header not found. We assume file starts with |
| 169 |
MacPaint header. Check MacPaint version but dismiss error. */ |
| 170 |
if (validateMacPaintVersion( rBuff, 0 ) == TRUE) |
| 171 |
pm_message("Input file starts with valid MacPaint header."); |
| 172 |
else |
| 173 |
pm_message(" - Ignoring invalid version number."); |
| 174 |
} |
| 175 |
free( rBuff ); |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
static void |
| 181 |
skipExtraBytes( FILE * const ifP, |
| 182 |
int const extraskip) { |
| 183 |
/*-------------------------------------------------------------------------- |
| 184 |
This function exists for backward compatibility. Its purpose is to |
| 185 |
manually delete the MacBinary header. |
| 186 |
|
| 187 |
We check the MacHeader version number, but just issue a warning if the |
| 188 |
value is invalid. |
| 189 |
---------------------------------------------------------------------------*/ |
| 190 |
unsigned int re; |
| 191 |
unsigned char * const rBuff = malloc(MAX (extraskip, MACP_HEAD_LEN)); |
| 192 |
|
| 193 |
if( rBuff == NULL ) |
| 194 |
pm_error("Out of memory."); |
| 195 |
|
| 196 |
re = fread ( rBuff, 1, extraskip, ifP); |
| 197 |
if (re < extraskip) |
| 198 |
pm_error("EOF/error while reading off initial %u bytes" |
| 199 |
"specified by -extraskip.", extraskip); |
| 200 |
re = fread ( rBuff, MACP_HEAD_LEN, 1, ifP); |
| 201 |
if (re < 1) |
| 202 |
pm_error("EOF/error while reading MacPaint header."); |
| 203 |
|
| 204 |
/* Check the MacPaint version number. Dismiss error. */ |
| 205 |
if (validateMacPaintVersion( rBuff, 0 ) == TRUE) |
| 206 |
pm_message("Input file starts with valid MacPaint header."); |
| 207 |
else |
| 208 |
pm_message(" - Ignoring invalid version number."); |
| 209 |
|
| 210 |
free( rBuff ); |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
static unsigned char |
| 216 |
readChar( FILE * const ifP ) { |
| 217 |
|
| 218 |
int const ch = getc( ifP ); |
| 219 |
|
| 220 |
if (ch ==EOF) |
| 221 |
pm_error("EOF encountered while unpacking image data."); |
| 222 |
|
| 223 |
/* else */ |
| 224 |
return ((unsigned char) ch); |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
static void |
| 230 |
ReadMacPaintFile( FILE * const ifP, |
| 231 |
int * outOfSyncP, |
| 232 |
int * pixelCntP ) { |
| 233 |
/*--------------------------------------------------------------------------- |
| 234 |
Unpack image data. Compression method is called "Packbits". |
| 235 |
This run-length encoding scheme has also been adopted by |
| 236 |
Postscript and TIFF. See source: converter/other/pnmtops.c |
| 237 |
|
| 238 |
Unpacked raster array is raw PBM. No conversion is required. |
| 239 |
|
| 240 |
One source says flag byte should not be 0xFF (255), but we don't reject |
| 241 |
the value, for in practice, it is widely used. |
| 242 |
|
| 243 |
Sequences should never cross row borders. |
| 244 |
Violations of this rule are recorded in outOfSync. |
| 245 |
|
| 246 |
Note that pixelCnt counts bytes, not bits, so it is the number of pixels |
| 247 |
multiplied by 8. This counter exists to detect corruptions. |
| 248 |
---------------------------------------------------------------------------*/ |
| 249 |
int pixelCnt = 0; /* Initial value */ |
| 250 |
int outOfSync = 0; /* Initial value */ |
| 251 |
unsigned int flag; /* Read from input */ |
| 252 |
unsigned int i; |
| 253 |
unsigned char * const bitrow = pbm_allocrow_packed(MACP_COLS); |
| 254 |
|
| 255 |
while ( pixelCnt < MACP_BYTES ) { |
| 256 |
flag = (unsigned int) readChar( ifP ); /* Flag (count) byte */ |
| 257 |
if ( flag < 0x80 ) { |
| 258 |
/* Unpack next (flag + 1) chars as is */ |
| 259 |
for ( i = 0; i <= flag; i++ ) |
| 260 |
if( pixelCnt < MACP_BYTES) { |
| 261 |
int const colChar = pixelCnt % MACP_COLCHARS; |
| 262 |
pixelCnt++; |
| 263 |
bitrow[colChar] = readChar( ifP ); |
| 264 |
if (colChar == MACP_COLCHARS-1) |
| 265 |
pbm_writepbmrow_packed( stdout, bitrow, MACP_COLS, 0 ); |
| 266 |
if (colChar == 0 && i > 0 ) |
| 267 |
outOfSync++; |
| 268 |
} |
| 269 |
} |
| 270 |
else { |
| 271 |
/* Repeat next char (2's complement of flagCnt) times */ |
| 272 |
unsigned int const flagCnt = 256 - flag; |
| 273 |
unsigned char const ch = readChar( ifP ); |
| 274 |
for ( i = 0; i <= flagCnt; i++ ) |
| 275 |
if( pixelCnt < MACP_BYTES) { |
| 276 |
int const colChar = pixelCnt % MACP_COLCHARS; |
| 277 |
pixelCnt++; |
| 278 |
bitrow[colChar] = ch; |
| 279 |
if (colChar == MACP_COLCHARS-1) |
| 280 |
pbm_writepbmrow_packed( stdout, bitrow, MACP_COLS, 0 ); |
| 281 |
if (colChar == 0 && i > 0 ) |
| 282 |
outOfSync++; |
| 283 |
} |
| 284 |
} |
| 285 |
} |
| 286 |
pbm_freerow_packed ( bitrow ); |
| 287 |
*outOfSyncP = outOfSync; |
| 288 |
*pixelCntP = pixelCnt; |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
int |
| 294 |
main( int argc, char * argv[]) { |
| 295 |
|
| 296 |
FILE * ifp; |
| 297 |
int argn, extraskip; |
| 298 |
const char * const usage = "[-extraskip N] [macpfile]"; |
| 299 |
int outOfSync; |
| 300 |
int pixelCnt; |
| 301 |
|
| 302 |
pbm_init( &argc, argv ); |
| 303 |
|
| 304 |
argn = 1; /* initial value */ |
| 305 |
extraskip = 0; /* initial value */ |
| 306 |
|
| 307 |
/* Check for flags. */ |
| 308 |
if ( argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0' ) { |
| 309 |
if ( pm_keymatch( argv[argn], "-extraskip", 2 ) ) { |
| 310 |
argn++; |
| 311 |
if ( argn == argc || sscanf( argv[argn], "%d", &extraskip ) != 1 ) |
| 312 |
pm_usage( usage ); |
| 313 |
} |
| 314 |
else |
| 315 |
pm_usage( usage ); |
| 316 |
argn++; |
| 317 |
} |
| 318 |
|
| 319 |
if ( argn < argc ) { |
| 320 |
ifp = pm_openr( argv[argn] ); |
| 321 |
argn++; |
| 322 |
} |
| 323 |
else |
| 324 |
ifp = stdin; |
| 325 |
|
| 326 |
if ( argn != argc ) |
| 327 |
pm_usage( usage ); |
| 328 |
|
| 329 |
if ( extraskip > 256 * 1024 ) |
| 330 |
pm_error("-extraskip value too large"); |
| 331 |
else if ( extraskip > 0 ) |
| 332 |
skipExtraBytes( ifp, extraskip); |
| 333 |
else |
| 334 |
skipHeader( ifp ); |
| 335 |
|
| 336 |
pbm_writepbminit( stdout, MACP_COLS, MACP_ROWS, 0 ); |
| 337 |
|
| 338 |
ReadMacPaintFile( ifp, &outOfSync, &pixelCnt ); |
| 339 |
/* We may not be at EOF. |
| 340 |
Macpaint files often have extra bytes after image data. */ |
| 341 |
pm_close( ifp ); |
| 342 |
|
| 343 |
if ( pixelCnt == 0 ) |
| 344 |
pm_error("No image data."); |
| 345 |
|
| 346 |
else if ( pixelCnt < MACP_BYTES ) |
| 347 |
pm_error("Compressed image data terminated prematurely."); |
| 348 |
|
| 349 |
else if ( outOfSync > 0 ) |
| 350 |
pm_message("Warning: Corrupt image data. %d rows misaligned.", |
| 351 |
outOfSync); |
| 352 |
|
| 353 |
pm_close( stdout ); |
| 354 |
exit( 0 ); |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
|