1d61900aaSMatthew Wilber /*****************************************************************************/ 2d61900aaSMatthew Wilber // tgainfo 3d61900aaSMatthew Wilber // Written by Michael Wilber, OBOS Translation Kit Team 4d61900aaSMatthew Wilber // 5d61900aaSMatthew Wilber // Version: 6d61900aaSMatthew Wilber // 7d61900aaSMatthew Wilber // tgainfo is a command line program for displaying information about 8d61900aaSMatthew Wilber // TGA images. 9d61900aaSMatthew Wilber // 10d61900aaSMatthew Wilber // 11d61900aaSMatthew Wilber // This application and all source files used in its construction, except 12d61900aaSMatthew Wilber // where noted, are licensed under the MIT License, and have been written 13d61900aaSMatthew Wilber // and are: 14d61900aaSMatthew Wilber // 15d61900aaSMatthew Wilber // Copyright (c) 2003 OpenBeOS Project 16d61900aaSMatthew Wilber // 17d61900aaSMatthew Wilber // Permission is hereby granted, free of charge, to any person obtaining a 18d61900aaSMatthew Wilber // copy of this software and associated documentation files (the "Software"), 19d61900aaSMatthew Wilber // to deal in the Software without restriction, including without limitation 20d61900aaSMatthew Wilber // the rights to use, copy, modify, merge, publish, distribute, sublicense, 21d61900aaSMatthew Wilber // and/or sell copies of the Software, and to permit persons to whom the 22d61900aaSMatthew Wilber // Software is furnished to do so, subject to the following conditions: 23d61900aaSMatthew Wilber // 24d61900aaSMatthew Wilber // The above copyright notice and this permission notice shall be included 25d61900aaSMatthew Wilber // in all copies or substantial portions of the Software. 26d61900aaSMatthew Wilber // 27d61900aaSMatthew Wilber // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 28d61900aaSMatthew Wilber // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 29d61900aaSMatthew Wilber // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 30d61900aaSMatthew Wilber // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 31d61900aaSMatthew Wilber // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 32d61900aaSMatthew Wilber // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 33d61900aaSMatthew Wilber // DEALINGS IN THE SOFTWARE. 34d61900aaSMatthew Wilber /*****************************************************************************/ 35d61900aaSMatthew Wilber #include <stdio.h> 36d61900aaSMatthew Wilber #include <stdlib.h> 37d61900aaSMatthew Wilber #include <string.h> 38d61900aaSMatthew Wilber #include <ByteOrder.h> 39d61900aaSMatthew Wilber #include <File.h> 40d61900aaSMatthew Wilber #include <TranslatorFormats.h> 41*5d71fdcbSshatty #include <StorageDefs.h> 42d61900aaSMatthew Wilber 43d61900aaSMatthew Wilber #define max(x,y) ((x > y) ? x : y) 44d61900aaSMatthew Wilber #define DATA_BUFFER_SIZE 64 45d61900aaSMatthew Wilber 46d61900aaSMatthew Wilber struct TGAFileHeader { 47d61900aaSMatthew Wilber uint8 idlength; 48d61900aaSMatthew Wilber // Number of bytes in the Image ID field 49d61900aaSMatthew Wilber uint8 colormaptype; 50d61900aaSMatthew Wilber uint8 imagetype; 51d61900aaSMatthew Wilber }; 52d61900aaSMatthew Wilber 53d61900aaSMatthew Wilber #define TGA_NO_COLORMAP 0 54d61900aaSMatthew Wilber #define TGA_COLORMAP 1 55d61900aaSMatthew Wilber 56d61900aaSMatthew Wilber #define TGA_NO_IMAGE_DATA 0 57d61900aaSMatthew Wilber 58d61900aaSMatthew Wilber #define TGA_NOCOMP_COLORMAP 1 59d61900aaSMatthew Wilber #define TGA_NOCOMP_TRUECOLOR 2 60d61900aaSMatthew Wilber #define TGA_NOCOMP_BW 3 61d61900aaSMatthew Wilber #define TGA_RLE_COLORMAP 9 62d61900aaSMatthew Wilber #define TGA_RLE_TRUECOLOR 10 63d61900aaSMatthew Wilber #define TGA_RLE_BW 11 64d61900aaSMatthew Wilber 65d61900aaSMatthew Wilber // Information about the color map (palette). These bytes are 66d61900aaSMatthew Wilber // always present, but are zero if no color map is present 67d61900aaSMatthew Wilber struct TGAColorMapSpec { 68d61900aaSMatthew Wilber uint16 firstentry; // first useful entry in the color map 69d61900aaSMatthew Wilber uint16 length; // number of color map entries 70d61900aaSMatthew Wilber uint8 entrysize; // number of bits per entry 71d61900aaSMatthew Wilber }; 72d61900aaSMatthew Wilber 73d61900aaSMatthew Wilber struct TGAImageSpec { 74d61900aaSMatthew Wilber uint16 xorigin; 75d61900aaSMatthew Wilber uint16 yorigin; 76d61900aaSMatthew Wilber uint16 width; 77d61900aaSMatthew Wilber uint16 height; 78d61900aaSMatthew Wilber uint8 depth; 79d61900aaSMatthew Wilber uint8 descriptor; 80d61900aaSMatthew Wilber }; 81d61900aaSMatthew Wilber 82d61900aaSMatthew Wilber #define TGA_ORIGIN_VERT_BIT 0x20 83d61900aaSMatthew Wilber #define TGA_ORIGIN_BOTTOM 0 84d61900aaSMatthew Wilber #define TGA_ORIGIN_TOP 1 85d61900aaSMatthew Wilber 86d61900aaSMatthew Wilber #define TGA_ORIGIN_HORZ_BIT 0x10 87d61900aaSMatthew Wilber #define TGA_ORIGIN_LEFT 0 88d61900aaSMatthew Wilber #define TGA_ORIGIN_RIGHT 1 89d61900aaSMatthew Wilber 90d61900aaSMatthew Wilber #define TGA_DESC_BITS76 0xc0 91d61900aaSMatthew Wilber #define TGA_DESC_ALPHABITS 0x0f 92d61900aaSMatthew Wilber 93d61900aaSMatthew Wilber #define TGA_HEADERS_SIZE 18 94d61900aaSMatthew Wilber #define TGA_FTR_LEN 26 95735e7442SMatthew Wilber #define TGA_EXT_LEN 495 96735e7442SMatthew Wilber #define LINE_LEN 82 97d61900aaSMatthew Wilber 98d61900aaSMatthew Wilber const char * 99d61900aaSMatthew Wilber colormaptype(uint8 n) 100d61900aaSMatthew Wilber { 101d61900aaSMatthew Wilber switch (n) { 102d61900aaSMatthew Wilber case 0: return "No colormap"; 103d61900aaSMatthew Wilber case 1: return "colormap"; 104d61900aaSMatthew Wilber } 105d61900aaSMatthew Wilber return "unknown"; 106d61900aaSMatthew Wilber } 107d61900aaSMatthew Wilber 108d61900aaSMatthew Wilber const char * 109d61900aaSMatthew Wilber imagetype(uint8 n) 110d61900aaSMatthew Wilber { 111d61900aaSMatthew Wilber switch (n) { 112d61900aaSMatthew Wilber case 0: return "No Image Data"; 113d61900aaSMatthew Wilber case 1: return "colormap"; 114d61900aaSMatthew Wilber case 2: return "true color"; 115d61900aaSMatthew Wilber case 3: return "grayscale"; 116d61900aaSMatthew Wilber case 9: return "RLE colormap"; 117d61900aaSMatthew Wilber case 10: return "RLE true color"; 118d61900aaSMatthew Wilber case 11: return "RLE grayscale"; 119d61900aaSMatthew Wilber } 120d61900aaSMatthew Wilber return "unknown"; 121d61900aaSMatthew Wilber } 122d61900aaSMatthew Wilber 123735e7442SMatthew Wilber uint16 124735e7442SMatthew Wilber tga_uint16(char *buffer, int32 offset) 125735e7442SMatthew Wilber { 126735e7442SMatthew Wilber return B_LENDIAN_TO_HOST_INT16(*(reinterpret_cast<uint16 *>(buffer + offset))); 127735e7442SMatthew Wilber } 128735e7442SMatthew Wilber 129735e7442SMatthew Wilber uint32 130735e7442SMatthew Wilber tga_uint32(char *buffer, int32 offset) 131735e7442SMatthew Wilber { 132735e7442SMatthew Wilber return B_LENDIAN_TO_HOST_INT32(*(reinterpret_cast<uint32 *>(buffer + offset))); 133735e7442SMatthew Wilber } 134735e7442SMatthew Wilber 135d61900aaSMatthew Wilber void 136d61900aaSMatthew Wilber print_tga_info(BFile &file) 137d61900aaSMatthew Wilber { 138d61900aaSMatthew Wilber uint8 buf[TGA_HEADERS_SIZE]; 139d61900aaSMatthew Wilber 140d61900aaSMatthew Wilber // read in TGA headers 141d61900aaSMatthew Wilber ssize_t size = TGA_HEADERS_SIZE; 142d61900aaSMatthew Wilber if (size > 0 && file.Read(buf, size) != size) { 143d61900aaSMatthew Wilber printf("Error: unable to read all TGA headers\n"); 144d61900aaSMatthew Wilber return; 145d61900aaSMatthew Wilber } 146d61900aaSMatthew Wilber 147d61900aaSMatthew Wilber // TGA file header 148d61900aaSMatthew Wilber TGAFileHeader fh; 149d61900aaSMatthew Wilber fh.idlength = buf[0]; 150d61900aaSMatthew Wilber fh.colormaptype = buf[1]; 151d61900aaSMatthew Wilber fh.imagetype = buf[2]; 152d61900aaSMatthew Wilber 153d61900aaSMatthew Wilber printf("\nFile Header:\n"); 154d61900aaSMatthew Wilber printf(" id length: %d\n", fh.idlength); 155d61900aaSMatthew Wilber 156d61900aaSMatthew Wilber printf("colormap type: %d (%s)\n", fh.colormaptype, 157d61900aaSMatthew Wilber colormaptype(fh.colormaptype)); 158d61900aaSMatthew Wilber printf(" image type: %d (%s)\n", fh.imagetype, imagetype(fh.imagetype)); 159d61900aaSMatthew Wilber 160d61900aaSMatthew Wilber 161d61900aaSMatthew Wilber // TGA color map spec 162d61900aaSMatthew Wilber TGAColorMapSpec mapspec; 163735e7442SMatthew Wilber mapspec.firstentry = tga_uint16(reinterpret_cast<char *>(buf), 3); 164735e7442SMatthew Wilber mapspec.length = tga_uint16(reinterpret_cast<char *>(buf), 5); 165d61900aaSMatthew Wilber mapspec.entrysize = buf[7]; 166d61900aaSMatthew Wilber 167d61900aaSMatthew Wilber printf("\nColormap Spec:\n"); 168d61900aaSMatthew Wilber printf("first entry: %d\n", mapspec.firstentry); 169d61900aaSMatthew Wilber printf(" length: %d\n", mapspec.length); 170d61900aaSMatthew Wilber printf(" entry size: %d\n", mapspec.entrysize); 171d61900aaSMatthew Wilber 172d61900aaSMatthew Wilber 173d61900aaSMatthew Wilber // TGA image spec 174d61900aaSMatthew Wilber TGAImageSpec imagespec; 175735e7442SMatthew Wilber imagespec.xorigin = tga_uint16(reinterpret_cast<char *>(buf), 8); 176735e7442SMatthew Wilber imagespec.yorigin = tga_uint16(reinterpret_cast<char *>(buf), 10); 177735e7442SMatthew Wilber imagespec.width = tga_uint16(reinterpret_cast<char *>(buf), 12); 178735e7442SMatthew Wilber imagespec.height = tga_uint16(reinterpret_cast<char *>(buf), 14); 179d61900aaSMatthew Wilber imagespec.depth = buf[16]; 180d61900aaSMatthew Wilber imagespec.descriptor = buf[17]; 181d61900aaSMatthew Wilber 182d61900aaSMatthew Wilber printf("\nImage Spec:\n"); 183d61900aaSMatthew Wilber printf(" x origin: %d\n", imagespec.xorigin); 184d61900aaSMatthew Wilber printf(" y origin: %d\n", imagespec.yorigin); 185d61900aaSMatthew Wilber printf(" width: %d\n", imagespec.width); 186d61900aaSMatthew Wilber printf(" height: %d\n", imagespec.height); 187d61900aaSMatthew Wilber printf(" depth: %d\n", imagespec.depth); 188d61900aaSMatthew Wilber printf("descriptor: 0x%.2lx\n", imagespec.descriptor); 189d61900aaSMatthew Wilber printf("\talpha (attr): %d\n", 190d61900aaSMatthew Wilber imagespec.descriptor & TGA_DESC_ALPHABITS); 191d61900aaSMatthew Wilber printf("\t origin: %d (%s %s)\n", 192d61900aaSMatthew Wilber imagespec.descriptor & (TGA_ORIGIN_VERT_BIT | TGA_ORIGIN_HORZ_BIT), 193d61900aaSMatthew Wilber ((imagespec.descriptor & TGA_ORIGIN_VERT_BIT) ? "top" : "bottom"), 194d61900aaSMatthew Wilber ((imagespec.descriptor & TGA_ORIGIN_HORZ_BIT) ? "right" : "left")); 195d61900aaSMatthew Wilber printf("\t bits 7 & 6: %d\n", imagespec.descriptor & TGA_DESC_BITS76); 196d61900aaSMatthew Wilber 197d61900aaSMatthew Wilber 198d61900aaSMatthew Wilber // Optional TGA Footer 199d61900aaSMatthew Wilber off_t filesize = 0; 200d61900aaSMatthew Wilber if (file.GetSize(&filesize) == B_OK) { 201d61900aaSMatthew Wilber 202d61900aaSMatthew Wilber char tgafooter[TGA_FTR_LEN + 1] = { 0 }; 203d61900aaSMatthew Wilber if (file.ReadAt(filesize - TGA_FTR_LEN, tgafooter, TGA_FTR_LEN) == TGA_FTR_LEN) { 204d61900aaSMatthew Wilber 205d61900aaSMatthew Wilber if (strcmp(tgafooter + 8, "TRUEVISION-XFILE.") == 0) { 206d61900aaSMatthew Wilber 207d61900aaSMatthew Wilber uint32 extoffset = 0, devoffset = 0; 208735e7442SMatthew Wilber extoffset = tga_uint32(tgafooter, 0); 209735e7442SMatthew Wilber devoffset = tga_uint32(tgafooter, 4); 210d61900aaSMatthew Wilber 211d61900aaSMatthew Wilber printf("\nTGA Footer:\n"); 212d61900aaSMatthew Wilber printf("extension offset: 0x%.8lx (%d)\n", extoffset, extoffset); 213d61900aaSMatthew Wilber printf("developer offset: 0x%.8lx (%d)\n", devoffset, devoffset); 214d61900aaSMatthew Wilber printf("signature: %s\n", tgafooter + 8); 215d61900aaSMatthew Wilber 216735e7442SMatthew Wilber if (extoffset) { 217735e7442SMatthew Wilber char extbuf[TGA_EXT_LEN]; 218735e7442SMatthew Wilber if (file.ReadAt(extoffset, extbuf, TGA_EXT_LEN) == TGA_EXT_LEN) { 219735e7442SMatthew Wilber 220735e7442SMatthew Wilber printf("\nExtension Area:\n"); 221735e7442SMatthew Wilber 222735e7442SMatthew Wilber char strbuffer[LINE_LEN]; 223735e7442SMatthew Wilber 224735e7442SMatthew Wilber uint16 extsize = tga_uint16(extbuf, 0); 225735e7442SMatthew Wilber if (extsize < TGA_EXT_LEN) { 226735e7442SMatthew Wilber printf("\nError: extension area is too small (%d)\n", extsize); 227735e7442SMatthew Wilber return; 228735e7442SMatthew Wilber } 229735e7442SMatthew Wilber printf("size: %d\n", extsize); 230735e7442SMatthew Wilber 231735e7442SMatthew Wilber memset(strbuffer, 0, LINE_LEN); 232735e7442SMatthew Wilber strncpy(strbuffer, extbuf + 2, 41); 233735e7442SMatthew Wilber printf("author: \"%s\"\n", strbuffer); 234735e7442SMatthew Wilber 235735e7442SMatthew Wilber printf("comments:\n"); 236735e7442SMatthew Wilber for (int32 i = 0; i < 4; i++) { 237735e7442SMatthew Wilber memset(strbuffer, 0, LINE_LEN); 238735e7442SMatthew Wilber strcpy(strbuffer, extbuf + 43 + (i * 81)); 239735e7442SMatthew Wilber printf("\tline %d: \"%s\"\n", i + 1, strbuffer); 240735e7442SMatthew Wilber } 241735e7442SMatthew Wilber 242735e7442SMatthew Wilber printf("date/time (yyyy-mm-dd hh:mm:ss): %.4d-%.2d-%.2d %.2d:%.2d:%.2d\n", 243735e7442SMatthew Wilber tga_uint16(extbuf, 367), tga_uint16(extbuf, 369), 244735e7442SMatthew Wilber tga_uint16(extbuf, 371), tga_uint16(extbuf, 373), 245735e7442SMatthew Wilber tga_uint16(extbuf, 375), tga_uint16(extbuf, 377)); 246735e7442SMatthew Wilber 247735e7442SMatthew Wilber memset(strbuffer, 0, LINE_LEN); 248735e7442SMatthew Wilber strncpy(strbuffer, extbuf + 379, 41); 249735e7442SMatthew Wilber printf("job name: \"%s\"\n", strbuffer); 250735e7442SMatthew Wilber 251735e7442SMatthew Wilber printf("job time (hh:mm:ss): %.2d:%.2d:%.2d\n", 252735e7442SMatthew Wilber tga_uint16(extbuf, 420), tga_uint16(extbuf, 422), 253735e7442SMatthew Wilber tga_uint16(extbuf, 424)); 254735e7442SMatthew Wilber 255735e7442SMatthew Wilber memset(strbuffer, 0, LINE_LEN); 256735e7442SMatthew Wilber strncpy(strbuffer, extbuf + 426, 41); 257735e7442SMatthew Wilber printf("software id: \"%s\"\n", strbuffer); 258735e7442SMatthew Wilber 259735e7442SMatthew Wilber char strver[] = "[null]"; 260735e7442SMatthew Wilber if (extbuf[469] != '\0') { 261735e7442SMatthew Wilber strver[0] = extbuf[469]; 262735e7442SMatthew Wilber strver[1] = '\0'; 263735e7442SMatthew Wilber } 264735e7442SMatthew Wilber printf("software version, letter: %d, %s\n", 265735e7442SMatthew Wilber tga_uint16(extbuf, 467), strver); 266735e7442SMatthew Wilber 267735e7442SMatthew Wilber printf("key color (A,R,G,B): %d, %d, %d, %d\n", 268735e7442SMatthew Wilber extbuf[470], extbuf[471], extbuf[472], extbuf[473]); 269735e7442SMatthew Wilber 270735e7442SMatthew Wilber printf("pixel aspect ratio: %d / %d\n", 271735e7442SMatthew Wilber tga_uint16(extbuf, 474), tga_uint16(extbuf, 476)); 272735e7442SMatthew Wilber 273735e7442SMatthew Wilber printf("gamma value: %d / %d\n", 274735e7442SMatthew Wilber tga_uint16(extbuf, 478), tga_uint16(extbuf, 480)); 275735e7442SMatthew Wilber 276735e7442SMatthew Wilber printf("color correction offset: 0x%.8lx (%d)\n", 277735e7442SMatthew Wilber tga_uint32(extbuf, 482), tga_uint32(extbuf, 482)); 278735e7442SMatthew Wilber printf("postage stamp offset: 0x%.8lx (%d)\n", 279735e7442SMatthew Wilber tga_uint32(extbuf, 486), tga_uint32(extbuf, 486)); 280735e7442SMatthew Wilber printf("scan line offset: 0x%.8lx (%d)\n", 281735e7442SMatthew Wilber tga_uint32(extbuf, 490), tga_uint32(extbuf, 490)); 282735e7442SMatthew Wilber 283735e7442SMatthew Wilber const char *strattrtype = NULL; 284735e7442SMatthew Wilber uint8 attrtype = extbuf[494]; 285735e7442SMatthew Wilber switch (attrtype) { 286735e7442SMatthew Wilber case 0: strattrtype = "no alpha"; break; 287735e7442SMatthew Wilber case 1: strattrtype = "undefined, ignore"; break; 288735e7442SMatthew Wilber case 2: strattrtype = "undefined, retain"; break; 289735e7442SMatthew Wilber case 3: strattrtype = "alpha"; break; 290735e7442SMatthew Wilber case 4: strattrtype = "pre-multiplied alpha"; break; 291735e7442SMatthew Wilber default: 292735e7442SMatthew Wilber if (attrtype > 4 && attrtype < 128) 293735e7442SMatthew Wilber strattrtype = "reserved"; 294735e7442SMatthew Wilber else 295735e7442SMatthew Wilber strattrtype = "unassigned"; 296735e7442SMatthew Wilber break; 297735e7442SMatthew Wilber } 298735e7442SMatthew Wilber printf("attributes type: %d (%s)\n", attrtype, strattrtype); 299735e7442SMatthew Wilber 300735e7442SMatthew Wilber } else 301735e7442SMatthew Wilber printf("\nError: Unable to read entire extension area\n"); 302735e7442SMatthew Wilber } 303735e7442SMatthew Wilber 304d61900aaSMatthew Wilber } else 305d61900aaSMatthew Wilber printf("\nTGA footer not found\n"); 306d61900aaSMatthew Wilber 307d61900aaSMatthew Wilber } else 308d61900aaSMatthew Wilber printf("\nError: Unable to read TGA footer section\n"); 309d61900aaSMatthew Wilber 310d61900aaSMatthew Wilber } else 311d61900aaSMatthew Wilber printf("\nError: Unable to get file size\n"); 312d61900aaSMatthew Wilber } 313d61900aaSMatthew Wilber 314d61900aaSMatthew Wilber int 315d61900aaSMatthew Wilber main(int argc, char **argv) 316d61900aaSMatthew Wilber { 317d61900aaSMatthew Wilber printf("\n"); 318d61900aaSMatthew Wilber 319735e7442SMatthew Wilber if (argc == 1) { 320d61900aaSMatthew Wilber printf("tgainfo - reports information about a TGA image file\n"); 321d61900aaSMatthew Wilber printf("\nUsage:\n"); 322d61900aaSMatthew Wilber printf("tgainfo filename.tga\n"); 323d61900aaSMatthew Wilber } 324735e7442SMatthew Wilber else { 325735e7442SMatthew Wilber BFile file; 326735e7442SMatthew Wilber 327735e7442SMatthew Wilber for (int32 i = 1; i < argc; i++) { 328735e7442SMatthew Wilber if (file.SetTo(argv[i], B_READ_ONLY) != B_OK) 329735e7442SMatthew Wilber printf("\nError opening %s\n", argv[i]); 330735e7442SMatthew Wilber else { 331735e7442SMatthew Wilber printf("\nTGA image information for: %s\n", argv[i]); 332735e7442SMatthew Wilber print_tga_info(file); 333735e7442SMatthew Wilber } 334735e7442SMatthew Wilber } 335735e7442SMatthew Wilber 336735e7442SMatthew Wilber } 337d61900aaSMatthew Wilber 338d61900aaSMatthew Wilber printf("\n"); 339d61900aaSMatthew Wilber 340d61900aaSMatthew Wilber return 0; 341d61900aaSMatthew Wilber } 342d61900aaSMatthew Wilber 343