| 1 |
/* infotopam: A program to convert Amiga Info icon files to PAM files |
| 2 |
* Copyright (C) 2004 Richard Griswold - [email protected] |
| 3 |
* |
| 4 |
* Thanks to the following people on comp.sys.amiga.programmer for tips |
| 5 |
* and pointers on decoding the info file format: |
| 6 |
* |
| 7 |
* Ben Hutchings |
| 8 |
* Thomas Richter |
| 9 |
* Kjetil Svalastog Matheussen |
| 10 |
* Anders Melchiorsen |
| 11 |
* Dirk Stoecker |
| 12 |
* Ronald V.D. |
| 13 |
* |
| 14 |
* The format of the Amiga info file is as follows: |
| 15 |
* |
| 16 |
* DiskObject header (78 bytes) |
| 17 |
* Optional DrawerData header (56 bytes) |
| 18 |
* First icon header (20 bytes) |
| 19 |
* First icon data |
| 20 |
* Second icon header (20 bytes) |
| 21 |
* Second icon data |
| 22 |
* |
| 23 |
* The DiskObject header contains, among other things, the magic number |
| 24 |
* (0xE310), the object width and height (inside the embedded Gadget header), |
| 25 |
* and the version. |
| 26 |
* |
| 27 |
* Each icon header contains the icon width and height, which can be smaller |
| 28 |
* than the object width and height, and the number of bit-planes. |
| 29 |
* |
| 30 |
* The icon data has the following format: |
| 31 |
* |
| 32 |
* BIT-PLANE planes, each with HEIGHT rows WIDTH bits long, rounded up to |
| 33 |
* a multiple of 2 bytes. |
| 34 |
* |
| 35 |
* So if you have a 9x3x2 icon, the icon data looks like this: |
| 36 |
* |
| 37 |
* aaaa aaaa a000 0000 |
| 38 |
* aaaa aaaa a000 0000 |
| 39 |
* aaaa aaaa a000 0000 |
| 40 |
* bbbb bbbb b000 0000 |
| 41 |
* bbbb bbbb b000 0000 |
| 42 |
* bbbb bbbb b000 0000 |
| 43 |
* |
| 44 |
* Where 'a' is a bit for the first bit-plane, 'b' is a bit for the second |
| 45 |
* bit-plane, and '0' is padding. Thanks again to Ben Hutchings for his |
| 46 |
* very helpful post! |
| 47 |
* |
| 48 |
*----------------------------------------------------------------------------- |
| 49 |
* The following specification for the DiskObject header is from |
| 50 |
* http://amigadev.elowar.com/read/ADCD_2.1/Libraries_Manual_guide/node0241.html |
| 51 |
* on 2024.03.14. |
| 52 |
* |
| 53 |
* The DiskObject C structure is defined in the include file |
| 54 |
* <workbench/workbench.h>. For a complete listing, see the Amiga ROM Kernel |
| 55 |
* Reference Manual: Includes and Autodocs. The DiskObject structure contains |
| 56 |
* the following elements: |
| 57 |
* |
| 58 |
* struct DiskObject { |
| 59 |
* UWORD do_Magic; magic number at start of file |
| 60 |
* UWORD do_Version; so we can change structure |
| 61 |
* struct Gadget do_Gadget; a copy of in core gadget |
| 62 |
* UBYTE do_Type; |
| 63 |
* char *do_DefaultTool; |
| 64 |
* char **do_ToolTypes; |
| 65 |
* LONG do_CurrentX; |
| 66 |
* LONG do_CurrentY; |
| 67 |
* struct DrawerData *do_DrawerData; |
| 68 |
* char *do_ToolWindow; applies only to tools |
| 69 |
* LONG do_StackSize; applies only to tools |
| 70 |
* }; |
| 71 |
* |
| 72 |
* do_Magic |
| 73 |
* |
| 74 |
* A magic number that the icon library looks for to make sure that the |
| 75 |
* file it is reading really contains an icon. It should be the manifest |
| 76 |
* constant WB_DISKMAGIC. PutDiskObject() will put this value in the |
| 77 |
* structure, and GetDiskObject() will not believe that a file is really |
| 78 |
* an icon unless this value is correct. |
| 79 |
* |
| 80 |
* do_Version |
| 81 |
* |
| 82 |
* This provides a way to enhance the .info file in an upwardly-compatible |
| 83 |
* way. It should be WB_DISKVERSION. The icon library will set this value |
| 84 |
* for you and will not believe weird values. |
| 85 |
* |
| 86 |
* do_Gadget |
| 87 |
* |
| 88 |
* This contains all the imagery for the icon. See the "Gadget Structure" |
| 89 |
* section below for more details. |
| 90 |
* |
| 91 |
* do_Type |
| 92 |
* |
| 93 |
* The type of the icon; can be set to any of the following values. |
| 94 |
* |
| 95 |
* WBDISK The root of a disk |
| 96 |
* WBDRAWER A directory on the disk |
| 97 |
* WBTOOL An executable program |
| 98 |
* WBPROJECT A data file |
| 99 |
* WBGARBAGE The Trashcan directory |
| 100 |
* WBKICK A Kickstart disk |
| 101 |
* WBAPPICON Any object not directly associated with a filing system |
| 102 |
* object, such as a print spooler (new in Release 2). |
| 103 |
* |
| 104 |
* do_DefaultTool |
| 105 |
* |
| 106 |
* Default tools are used for project and disk icons. For projects (data |
| 107 |
* files), the default tool is the program Workbench runs when the project |
| 108 |
* is activated. Any valid AmigaDOS path may be entered in this field |
| 109 |
* such as "SYS:myprogram", "df0:mypaint", "myeditor" or ":work/mytool". |
| 110 |
* |
| 111 |
* For disk icons, the default tool is the diskcopy program |
| 112 |
* ("SYS:System/DiskCopy") that will be used when this disk is the source |
| 113 |
* of a copy. |
| 114 |
* |
| 115 |
* do_ToolTypes |
| 116 |
* |
| 117 |
* This is an array of free-format strings. Workbench does not enforce |
| 118 |
* any rules on these strings, but they are useful for passing |
| 119 |
* environment information. See the section on "The Tool Types Array" |
| 120 |
* below for more information. |
| 121 |
* |
| 122 |
* do_CurrentX, do_CurrentY |
| 123 |
* |
| 124 |
* Drawers have a virtual coordinate system. The user can scroll around |
| 125 |
* in this system using the scroll gadgets on the window that opens when |
| 126 |
* the drawer is activated. Each icon in the drawer has a position in |
| 127 |
* the coordinate system. CurrentX and CurrentY contain the icon's |
| 128 |
* current position in the drawer. Picking a position for a newly |
| 129 |
* created icon can be tricky. NO_ICON_POSITION is a system constant |
| 130 |
* for do_CurrentX and do_CurrentY that instructs Workbench to pick a |
| 131 |
* reasonable place for the icon. Workbench will place the icon in an |
| 132 |
* unused region of the drawer. If there is no space in the drawers |
| 133 |
* window, the icon will be placed just to the right of the visible |
| 134 |
* region. |
| 135 |
* |
| 136 |
* do_DrawerData |
| 137 |
* |
| 138 |
* If the icon is associated with a directory (WBDISK, WBDRAWER, |
| 139 |
* WBGARBAGE), it needs a DrawerData structure to go with it. This |
| 140 |
* structure contains an Intuition NewWindow structure (see the |
| 141 |
* "Intuition Windows" chapter for more information): |
| 142 |
* |
| 143 |
* struct DrawerData { |
| 144 |
* struct NewWindow dd_NewWindow; structure to open window |
| 145 |
* LONG dd_CurrentX; current x coordinate of origin |
| 146 |
* LONG dd_CurrentY; current y coordinate of origin |
| 147 |
* }; |
| 148 |
* |
| 149 |
* Workbench uses this to hold the current window position and size of |
| 150 |
* the window so it will reopen in the same place. |
| 151 |
* |
| 152 |
* do_ToolWindow |
| 153 |
* |
| 154 |
* This field is reserved for future use. |
| 155 |
* |
| 156 |
* do_StackSize |
| 157 |
* |
| 158 |
* This is the size of the stack (in bytes) used for running the tool. |
| 159 |
* If this is NULL, then Workbench will use a reasonable default stack |
| 160 |
* size (currently 4K bytes). |
| 161 |
* |
| 162 |
* When a tool is run via the default tool mechanism (i.e., a project |
| 163 |
* was activated, not the tool itself), Workbench uses the stack size |
| 164 |
* specified in the project's .info file and the tool's .info file is |
| 165 |
* ignored. |
| 166 |
* |
| 167 |
*------------------------------------------------------------------------- |
| 168 |
* This program uses code from "sidplay" and an older "infotoxpm" program |
| 169 |
* Richard Griswold wrote, both of which are offered under GPL. |
| 170 |
*------------------------------------------------------------------------- |
| 171 |
* |
| 172 |
* This program is free software; you can redistribute it and/or |
| 173 |
* modify it under the terms of the GNU General Public License |
| 174 |
* as published by the Free Software Foundation; either version 2 |
| 175 |
* of the License, or (at your option) any later version. |
| 176 |
* |
| 177 |
* This program is distributed in the hope that it will be useful, |
| 178 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 179 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 180 |
* GNU General Public License for more details. |
| 181 |
* |
| 182 |
* You should have received a copy of the GNU General Public License |
| 183 |
* along with this program; if not, write to the Free Software |
| 184 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 185 |
*/ |
| 186 |
|
| 187 |
#include <stdbool.h> |
| 188 |
#include <stdlib.h> |
| 189 |
#include <string.h> |
| 190 |
#include <errno.h> |
| 191 |
#include <stdio.h> |
| 192 |
|
| 193 |
#include "pm_c_util.h" |
| 194 |
#include "mallocvar.h" |
| 195 |
#include "nstring.h" |
| 196 |
#include "shhopt.h" |
| 197 |
#include "pam.h" |
| 198 |
|
| 199 |
|
| 200 |
typedef struct CmdlineInfo_ { |
| 201 |
const char * inputFileNm; |
| 202 |
unsigned int forcecolor; |
| 203 |
pixel colors[4]; /* Colors to use for converted icons */ |
| 204 |
unsigned int selected; |
| 205 |
unsigned int verbose; |
| 206 |
} CmdlineInfo; |
| 207 |
|
| 208 |
typedef struct IconInfo_ { |
| 209 |
/* Miscellaneous icon information */ |
| 210 |
FILE * ifP; /* Input file */ |
| 211 |
bool hasDrawerData; /* Icon has drawer data */ |
| 212 |
unsigned int version; /* Icon version */ |
| 213 |
unsigned int width; /* Width in pixels */ |
| 214 |
unsigned int height; /* Height in pixels */ |
| 215 |
unsigned int depth; /* Bits of color per pixel */ |
| 216 |
unsigned int bpwidth; |
| 217 |
/* Bitplane width; Width of each row in icon, including padding */ |
| 218 |
unsigned char * icon; /* Completed icon */ |
| 219 |
|
| 220 |
} IconInfo; |
| 221 |
|
| 222 |
typedef struct IconHeader_ { /* 20 bytes */ |
| 223 |
/* Text of header for one icon image */ |
| 224 |
unsigned char type[4]; |
| 225 |
/* Reverse engineered. This always seems to be 0x00000000 in |
| 226 |
icon headers, but we've seen 0x00000010 in some 51-byte object |
| 227 |
we don't understand. |
| 228 |
*/ |
| 229 |
unsigned char iconWidth[2]; /* Width (usually equal to Gadget width) */ |
| 230 |
unsigned char iconHeight[2]; |
| 231 |
/* Height (usually equal to Gadget height -1) */ |
| 232 |
unsigned char bpp[2]; /* Bits per pixel */ |
| 233 |
unsigned char pad1[10]; /* ??? */ |
| 234 |
} IconHeader; |
| 235 |
|
| 236 |
/* |
| 237 |
* Gadget and DiskObject structs come from the libsidplay 1.36.57 info_.h file |
| 238 |
* http://www.geocities.com/SiliconValley/Lakes/5147/sidplay/linux.html |
| 239 |
*/ |
| 240 |
typedef struct DiskObject_ { /* 78 bytes (including Gadget struct) */ |
| 241 |
/* Text of Info Disk Object header */ |
| 242 |
unsigned char magic[2]; /* Magic number at the start of the file */ |
| 243 |
unsigned char version[2]; /* Object version number */ |
| 244 |
unsigned char gadget[44]; /* Copy of in memory gadget (44 by */ |
| 245 |
unsigned char type; /* ??? */ |
| 246 |
unsigned char pad; /* Pad it out to the next word boundary */ |
| 247 |
unsigned char pDefaultTool[4]; /* Pointer to default tool */ |
| 248 |
unsigned char ppToolTypes[4]; /* Pointer pointer to tool types */ |
| 249 |
unsigned char currentX[4]; /* Current X position (?) */ |
| 250 |
unsigned char currentY[4]; /* Current Y position (?) */ |
| 251 |
unsigned char pDrawerData[4]; /* Pointer to drawer data */ |
| 252 |
unsigned char pToolWindow[4]; /* Ptr to tool window - only for tools */ |
| 253 |
unsigned char stackSize[4]; /* Stack size - only for tools */ |
| 254 |
} DiskObject; |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
static void |
| 259 |
parseCommandLine(int argc, |
| 260 |
const char ** argv, |
| 261 |
CmdlineInfo * const cmdlineP) { |
| 262 |
|
| 263 |
unsigned int argIdx; |
| 264 |
optEntry * option_def; |
| 265 |
optStruct3 opt; |
| 266 |
unsigned int option_def_index; |
| 267 |
unsigned int numcolorsSpec; |
| 268 |
unsigned int numcolors; |
| 269 |
|
| 270 |
MALLOCARRAY_NOFAIL(option_def, 100); |
| 271 |
|
| 272 |
/* Set command line options */ |
| 273 |
option_def_index = 0; /* Incremented by OPTENT3 */ |
| 274 |
OPTENT3(0, "forcecolor", OPT_FLAG, NULL, &cmdlineP->forcecolor, |
| 275 |
0); |
| 276 |
OPTENT3(0, "numcolors", OPT_UINT, &numcolors, &numcolorsSpec, |
| 277 |
0); |
| 278 |
OPTENT3(0, "selected", OPT_FLAG, NULL, &cmdlineP->selected, |
| 279 |
0); |
| 280 |
OPTENT3(0, "verbose", OPT_FLAG, NULL, &cmdlineP->verbose, |
| 281 |
0); |
| 282 |
|
| 283 |
opt.opt_table = option_def; |
| 284 |
opt.short_allowed = false; /* No short (old-fashioned) options */ |
| 285 |
opt.allowNegNum = false; /* No negative number parameters */ |
| 286 |
|
| 287 |
pm_optParseOptions4(&argc, argv, opt, sizeof(opt), 0); |
| 288 |
|
| 289 |
{ |
| 290 |
const char * const colors[4] = { |
| 291 |
/* Pixel colors based on original Amiga colors */ |
| 292 |
"#0055AA", /* Blue 0, 85, 170 */ |
| 293 |
"#FFFFFF", /* White 255, 255, 255 */ |
| 294 |
"#000020", /* Black 0, 0, 32 */ |
| 295 |
"#FF8A00" /* Orange 255, 138, 0 */ |
| 296 |
}; |
| 297 |
|
| 298 |
unsigned int colorArgCt; |
| 299 |
/* Number of arguments for overriding colors */ |
| 300 |
|
| 301 |
unsigned int colorIdx; |
| 302 |
|
| 303 |
if (numcolorsSpec) { |
| 304 |
colorArgCt = numcolors * 2; |
| 305 |
if (argc-1 < colorArgCt) { |
| 306 |
pm_error("Insufficient arguments for %u color " |
| 307 |
"specifications. Need at least %u arguments", |
| 308 |
numcolors, colorArgCt); |
| 309 |
} |
| 310 |
} else |
| 311 |
colorArgCt = 0; |
| 312 |
|
| 313 |
/* Initialize palette to defaults */ |
| 314 |
for (colorIdx = 0; colorIdx < 4; ++colorIdx) |
| 315 |
cmdlineP->colors[colorIdx] = |
| 316 |
ppm_parsecolor(colors[colorIdx], 0xFF); |
| 317 |
|
| 318 |
/* Convert color arguments */ |
| 319 |
for (argIdx = 1; argIdx < colorArgCt; argIdx += 2) { |
| 320 |
char * endptr; /* End pointer for strtol() */ |
| 321 |
unsigned int colorIdx; |
| 322 |
|
| 323 |
/* Get color index from argument */ |
| 324 |
colorIdx = strtoul(argv[argIdx], &endptr, 0); |
| 325 |
|
| 326 |
if (*endptr != '\0') { |
| 327 |
pm_error("'%s' is not a valid color index", argv[argIdx]); |
| 328 |
} |
| 329 |
|
| 330 |
if ((colorIdx < 0) || (colorIdx > 3)) { |
| 331 |
pm_error( |
| 332 |
"%u is not a valid color index (minimum 0, maximum 3)", |
| 333 |
colorIdx); |
| 334 |
} |
| 335 |
|
| 336 |
cmdlineP->colors[colorIdx] = ppm_parsecolor(argv[argIdx+1], 0xFF); |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
if (argIdx > argc-1) |
| 341 |
cmdlineP->inputFileNm = "-"; /* Read from standard input */ |
| 342 |
else |
| 343 |
cmdlineP->inputFileNm = argv[argIdx]; |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
static void |
| 349 |
readDiskObjectHeader(FILE * const ifP, |
| 350 |
unsigned int * const versionP, |
| 351 |
bool * const hasDrawerDataP) { |
| 352 |
/*--------------------------------------------------------------------------- |
| 353 |
Read disk object header from file *ifP; validate it and return its contents. |
| 354 |
----------------------------------------------------------------------------*/ |
| 355 |
DiskObject dobj; /* Disk object structure */ |
| 356 |
size_t bytesReadCt; |
| 357 |
|
| 358 |
/* Read the disk object header */ |
| 359 |
bytesReadCt = fread(&dobj, 1, sizeof(dobj), ifP); |
| 360 |
if (ferror(ifP)) |
| 361 |
pm_error("Cannot read disk object header. " |
| 362 |
"fread() errno = %d (%s)", |
| 363 |
errno, strerror(errno)); |
| 364 |
else if (bytesReadCt != sizeof(dobj)) |
| 365 |
pm_error("Cannot read entire disk object header. " |
| 366 |
"Only read 0x%X of 0x%X bytes", |
| 367 |
(unsigned)bytesReadCt, (unsigned)sizeof(dobj)); |
| 368 |
|
| 369 |
/* Validate magic number */ |
| 370 |
if ((dobj.magic[0] != 0xE3) && (dobj.magic[1] != 0x10)) |
| 371 |
pm_error("Wrong magic number in icon file. " |
| 372 |
"Expected 0xE310, but got 0x%X%X", |
| 373 |
dobj.magic[0], dobj.magic[1]); |
| 374 |
|
| 375 |
*versionP = (dobj.version[0] << 8) + (dobj.version[1]); |
| 376 |
|
| 377 |
*hasDrawerDataP = |
| 378 |
(dobj.pDrawerData[0] << 24) + |
| 379 |
(dobj.pDrawerData[1] << 16) + |
| 380 |
(dobj.pDrawerData[2] << 8) + |
| 381 |
(dobj.pDrawerData[3] ) |
| 382 |
> 0; |
| 383 |
} |
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
static void |
| 388 |
readIconHeader(FILE * const ifP, |
| 389 |
unsigned int * const widthP, |
| 390 |
unsigned int * const heightP, |
| 391 |
unsigned int * const depthP, |
| 392 |
unsigned int * const bpwidthP) { |
| 393 |
/*------------------------------------------------------------------------- |
| 394 |
* Get fields from icon header portion of info file |
| 395 |
*-------------------------------------------------------------------------*/ |
| 396 |
IconHeader ihead; /* Icon header structure */ |
| 397 |
size_t bytesRead; |
| 398 |
|
| 399 |
/* Read icon header */ |
| 400 |
bytesRead = fread(&ihead, 1, sizeof(ihead), ifP); |
| 401 |
if (ferror(ifP)) |
| 402 |
pm_error("Failed to read icon header. fread() errno = %d (%s)", |
| 403 |
errno, strerror(errno)); |
| 404 |
else if (bytesRead != sizeof(ihead)) |
| 405 |
pm_error("Failed to read the entire icon header. " |
| 406 |
"Read only %u of %u bytes", |
| 407 |
(unsigned)bytesRead, (unsigned)sizeof(ihead)); |
| 408 |
|
| 409 |
if (!memeq(ihead.type, "\0\0\0\0", 4)) { |
| 410 |
pm_message("Unrecognized object where icon header expected. " |
| 411 |
"First 4 bytes are 0x%02x%02x%02x%02x. We expect " |
| 412 |
"0x00000000", |
| 413 |
ihead.type[0], ihead.type[1], ihead.type[2], ihead.type[3]); |
| 414 |
} |
| 415 |
|
| 416 |
*widthP = (ihead.iconWidth[0] << 8) + ihead.iconWidth[1]; |
| 417 |
*heightP = (ihead.iconHeight[0] << 8) + ihead.iconHeight[1]; |
| 418 |
*depthP = (ihead.bpp[0] << 8) + ihead.bpp[1]; |
| 419 |
|
| 420 |
if (*widthP < 1) |
| 421 |
pm_error("Invalid width value in icon header: %u", *widthP); |
| 422 |
|
| 423 |
if (*heightP < 1) |
| 424 |
pm_error("Invalid height value in icon header: %u", *heightP); |
| 425 |
|
| 426 |
if (*depthP > 2 || *depthP < 1) |
| 427 |
pm_error("We don't know how to interpret file with %u bitplanes. ", |
| 428 |
*depthP); |
| 429 |
|
| 430 |
*bpwidthP = ROUNDUP(*widthP, 16); |
| 431 |
} |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
static void |
| 436 |
addBitplane(unsigned char * const icon, |
| 437 |
unsigned int const bpsize, |
| 438 |
unsigned char * const buff) { |
| 439 |
/*---------------------------------------------------------------------------- |
| 440 |
Add bitplane to existing icon image |
| 441 |
-----------------------------------------------------------------------------*/ |
| 442 |
unsigned int i; |
| 443 |
|
| 444 |
for (i = 0; i < bpsize; ++i) { |
| 445 |
icon[(i*8)+0] = (icon[(i*8)+0] << 1) | ((buff[i] >> 7) & 0x01); |
| 446 |
icon[(i*8)+1] = (icon[(i*8)+1] << 1) | ((buff[i] >> 6) & 0x01); |
| 447 |
icon[(i*8)+2] = (icon[(i*8)+2] << 1) | ((buff[i] >> 5) & 0x01); |
| 448 |
icon[(i*8)+3] = (icon[(i*8)+3] << 1) | ((buff[i] >> 4) & 0x01); |
| 449 |
icon[(i*8)+4] = (icon[(i*8)+4] << 1) | ((buff[i] >> 3) & 0x01); |
| 450 |
icon[(i*8)+5] = (icon[(i*8)+5] << 1) | ((buff[i] >> 2) & 0x01); |
| 451 |
icon[(i*8)+6] = (icon[(i*8)+6] << 1) | ((buff[i] >> 1) & 0x01); |
| 452 |
icon[(i*8)+7] = (icon[(i*8)+7] << 1) | ((buff[i] >> 0) & 0x01); |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
static void |
| 459 |
readIconData(FILE * const fileP, |
| 460 |
unsigned int const width, |
| 461 |
unsigned int const height, |
| 462 |
unsigned int const depth, |
| 463 |
unsigned char ** const iconP) { |
| 464 |
/*------------------------------------------------------------------------- |
| 465 |
* Read icon data from file |
| 466 |
*-------------------------------------------------------------------------*/ |
| 467 |
int bitplane; /* Bitplane index */ |
| 468 |
unsigned char * buff; /* Buffer to hold bits for 1 bitplane */ |
| 469 |
unsigned char * icon; |
| 470 |
|
| 471 |
unsigned int const bpsize = height * (((width + 15) / 16) * 2); |
| 472 |
/* Bitplane size in bytes, with padding */ |
| 473 |
|
| 474 |
|
| 475 |
MALLOCARRAY(buff, bpsize); |
| 476 |
if ( buff == NULL ) |
| 477 |
pm_error( "Cannot allocate memory to hold icon pixels" ); |
| 478 |
|
| 479 |
MALLOCARRAY(icon, bpsize * 8); |
| 480 |
if (icon == NULL) |
| 481 |
pm_error( "Cannot allocate memory to hold icon" ); |
| 482 |
|
| 483 |
/* Initialize to zero */ |
| 484 |
memset(buff, 0, bpsize); |
| 485 |
memset(icon, 0, bpsize * 8); |
| 486 |
|
| 487 |
/* Each bitplane is stored independently in the icon file. This |
| 488 |
* loop reads one bitplane at a time into buff. Since fread() may |
| 489 |
* not read all of the bitplane on the first call, the inner loop |
| 490 |
* continues until all bytes are read. The buffer pointer, bp, |
| 491 |
* points to the next byte in buff to fill in. When the inner |
| 492 |
* loop is done, bp points to the end of buff. |
| 493 |
* |
| 494 |
* After reading in the entire bitplane, the second inner loop splits the |
| 495 |
* eight pixels in each byte of the bitplane into eight separate bytes in |
| 496 |
* the icon buffer. The existing contents of each byte in icon are left |
| 497 |
* shifted by one to make room for the next bit. |
| 498 |
* |
| 499 |
* Each byte in the completed icon contains a value from 0 to |
| 500 |
* 2^depth (0 to 1 for depth of 1 and 0 to 3 for a depth of 3). |
| 501 |
* This is an index into the colors array in the info struct. */ |
| 502 |
|
| 503 |
for (bitplane = 0; bitplane < depth; ++bitplane) { |
| 504 |
/* Read bitplane into buffer */ |
| 505 |
int toread; /* Number of bytes left to read */ |
| 506 |
unsigned char * buffp; /* Buffer point for reading data */ |
| 507 |
|
| 508 |
toread = bpsize; buffp = &buff[0]; |
| 509 |
|
| 510 |
while (toread > 0) { |
| 511 |
size_t bytesRead; |
| 512 |
|
| 513 |
bytesRead = fread(buffp, 1, toread, fileP); |
| 514 |
if (ferror(fileP)) |
| 515 |
pm_error("Cannot read from file info file. " |
| 516 |
"fread() errno = %d (%s)", |
| 517 |
errno, strerror(errno)); |
| 518 |
else if (bytesRead == 0) |
| 519 |
pm_error("Premature end-of-file. " |
| 520 |
"Still have 0x%X bytes to read", |
| 521 |
toread ); |
| 522 |
|
| 523 |
toread -= bytesRead; |
| 524 |
buffp += bytesRead; |
| 525 |
} |
| 526 |
addBitplane(icon, bpsize, buff); |
| 527 |
} |
| 528 |
*iconP = icon; |
| 529 |
|
| 530 |
free(buff); |
| 531 |
} |
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
static void |
| 536 |
writeRaster(IconInfo * const infoP, |
| 537 |
struct pam * const pamP, |
| 538 |
bool const wantColor, |
| 539 |
const pixel * const colors) { |
| 540 |
/*------------------------------------------------------------------------- |
| 541 |
Write out raster of PAM image described by *pamP. |
| 542 |
|
| 543 |
'wantColor' means the user wants the PAM to be tuple type RGB, regardless |
| 544 |
of the input image type. |
| 545 |
|
| 546 |
'colors' is the palette. It has 4 entries, one for each of the possible |
| 547 |
color indices in the input icon raster. |
| 548 |
--------------------------------------------------------------------------*/ |
| 549 |
unsigned int row; |
| 550 |
tuple * tuplerow; /* Output row */ |
| 551 |
|
| 552 |
tuplerow = pnm_allocpamrow(pamP); |
| 553 |
|
| 554 |
for (row = 0; row < infoP->height; ++row) { |
| 555 |
unsigned int col; |
| 556 |
|
| 557 |
for (col = 0; col < infoP->width; ++col) { |
| 558 |
if (infoP->depth == 1) { |
| 559 |
if (wantColor) { |
| 560 |
/* 1 is black and 0 is white */ |
| 561 |
unsigned int colorIdx = |
| 562 |
infoP->icon[row * infoP->bpwidth + col] ? 2 : 1; |
| 563 |
|
| 564 |
tuplerow[col][PAM_RED_PLANE] = PPM_GETR(colors[colorIdx]); |
| 565 |
tuplerow[col][PAM_GRN_PLANE] = PPM_GETG(colors[colorIdx]); |
| 566 |
tuplerow[col][PAM_BLU_PLANE] = PPM_GETB(colors[colorIdx]); |
| 567 |
} else { |
| 568 |
/* 1 is black and 0 is white */ |
| 569 |
tuplerow[col][0] = |
| 570 |
infoP->icon[row * infoP->bpwidth + col] ? 0 : 1; |
| 571 |
} |
| 572 |
} else { |
| 573 |
unsigned int const colorIdx = |
| 574 |
infoP->icon[row * infoP->bpwidth + col]; |
| 575 |
tuplerow[col][PAM_RED_PLANE] = PPM_GETR(colors[colorIdx]); |
| 576 |
tuplerow[col][PAM_GRN_PLANE] = PPM_GETG(colors[colorIdx]); |
| 577 |
tuplerow[col][PAM_BLU_PLANE] = PPM_GETB(colors[colorIdx]); |
| 578 |
} |
| 579 |
} |
| 580 |
pnm_writepamrow(pamP, tuplerow); |
| 581 |
} |
| 582 |
|
| 583 |
pnm_freepamrow(tuplerow); |
| 584 |
} |
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
int |
| 589 |
main(int argc, const char **argv) { |
| 590 |
|
| 591 |
CmdlineInfo cmdline; |
| 592 |
IconInfo info; /* Miscellaneous icon information */ |
| 593 |
struct pam pam; /* PAM header */ |
| 594 |
|
| 595 |
pm_proginit(&argc, argv); |
| 596 |
|
| 597 |
parseCommandLine(argc, argv, &cmdline); |
| 598 |
|
| 599 |
info.ifP = pm_openr(cmdline.inputFileNm); |
| 600 |
|
| 601 |
readDiskObjectHeader(info.ifP, &info.version, &info.hasDrawerData); |
| 602 |
|
| 603 |
/* Skip drawer data, if any */ |
| 604 |
if (info.hasDrawerData) { |
| 605 |
unsigned int const skipCt = 56; /* Draw data size */ |
| 606 |
|
| 607 |
int rc; |
| 608 |
|
| 609 |
rc = fseek(info.ifP, skipCt, SEEK_CUR); |
| 610 |
if (rc < 0) { |
| 611 |
pm_error("Failed to skip header information in input file. " |
| 612 |
"fseek() errno = %d (%s)", |
| 613 |
errno, strerror(errno)); |
| 614 |
} |
| 615 |
} |
| 616 |
|
| 617 |
/* Read header of first icon */ |
| 618 |
readIconHeader(info.ifP, &info.width, &info.height, &info.depth, |
| 619 |
&info.bpwidth); |
| 620 |
|
| 621 |
readIconData(info.ifP, info.width, info.height, info.depth, &info.icon); |
| 622 |
|
| 623 |
if (cmdline.selected) { |
| 624 |
/* He wants the second icon, so update info.width, etc. to be for the |
| 625 |
second icon. |
| 626 |
*/ |
| 627 |
readIconHeader(info.ifP, &info.width, &info.height, &info.depth, |
| 628 |
&info.bpwidth); |
| 629 |
|
| 630 |
readIconData(info.ifP, info.width, info.height, info.depth, |
| 631 |
&info.icon); |
| 632 |
} |
| 633 |
|
| 634 |
if (cmdline.verbose) { |
| 635 |
pm_message("Version %u .info file, %s icon: %uW x %uH x %u deep", |
| 636 |
info.version, cmdline.selected ? "second" : "first", |
| 637 |
info.width, info.height, info.depth); |
| 638 |
} |
| 639 |
pam.size = sizeof(pam); |
| 640 |
pam.len = PAM_STRUCT_SIZE(tuple_type); |
| 641 |
pam.file = stdout; |
| 642 |
pam.height = info.height; |
| 643 |
pam.width = info.width; |
| 644 |
pam.format = PAM_FORMAT; |
| 645 |
|
| 646 |
if ((info.depth == 1) && !cmdline.forcecolor) { |
| 647 |
pam.depth = 1; |
| 648 |
pam.maxval = 1; |
| 649 |
strcpy(pam.tuple_type, "BLACKANDWHITE"); |
| 650 |
} else { |
| 651 |
pam.depth = 3; |
| 652 |
pam.maxval = 0xFF; |
| 653 |
strcpy(pam.tuple_type, "RGB"); |
| 654 |
} |
| 655 |
pnm_writepaminit(&pam); |
| 656 |
|
| 657 |
writeRaster(&info, &pam, cmdline.forcecolor, cmdline.colors); |
| 658 |
|
| 659 |
free(info.icon); |
| 660 |
pm_close(pam.file); |
| 661 |
pm_close(info.ifP); |
| 662 |
|
| 663 |
return 0; |
| 664 |
} |
| 665 |
|
| 666 |
|