# grf.cpp

Go to the documentation of this file.

    00001 /* $Id: grf.cpp 18476 2009-12-13 00:13:06Z rubidium $ */
    00002
    00005 #include "../stdafx.h"
    00006 #include "../gfx_func.h"
    00007 #include "../fileio_func.h"
    00008 #include "../debug.h"
    00009 #include "../core/alloc_func.hpp"
    00010 #include "../strings_func.h"
    00011 #include "table/strings.h"
    00012 #include "../gui.h"
    00013 #include "grf.hpp"
    00014
    00023 static bool WarnCorruptSprite(uint8 file_slot, size_t file_pos, int line)
    00024 {
    00025   static byte warning_level = 0;
    00026   if (warning_level == 0) {
    00027     SetDParamStr(0, FioGetFilename(file_slot));
    00028     ShowErrorMessage(INVALID_STRING_ID, STR_NEWGRF_ERROR_CORRUPT_SPRITE, 0, 0);
    00029   }
    00030   DEBUG(sprite, warning_level, "[%i] Loading corrupted sprite from %s at position %i", line, FioGetFilename(file_slot), (int)file_pos);
    00031   warning_level = 6;
    00032   return false;
    00033 }
    00034
    00035 bool SpriteLoaderGrf::LoadSprite(SpriteLoader::Sprite *sprite, uint8 file_slot, size_t file_pos, SpriteType sprite_type)
    00036 {
    00037   /* Open the right file and go to the correct position */
    00038   FioSeekToFile(file_slot, file_pos);
    00039
    00040   /* Read the size and type */
    00041   int num = FioReadWord();
    00042   byte type = FioReadByte();
    00043
    00044   /* Type 0xFF indicates either a colourmap or some other non-sprite info; we do not handle them here */
    00045   if (type == 0xFF) return false;
    00046
    00047   sprite->height = FioReadByte();
    00048   sprite->width  = FioReadWord();
    00049   sprite->x_offs = FioReadWord();
    00050   sprite->y_offs = FioReadWord();
    00051
    00052   /* 0x02 indicates it is a compressed sprite, so we can't rely on 'num' to be valid.
    00053    *  In case it is uncompressed, the size is 'num' - 8 (header-size). */
    00054   num = (type & 0x02) ? sprite->width * sprite->height : num - 8;
    00055
    00056   byte *dest_orig = AllocaM(byte, num);
    00057   byte *dest = dest_orig;
    00058   const int dest_size = num;
    00059
    00060   /* Read the file, which has some kind of compression */
    00061   while (num > 0) {
    00062     int8 code = FioReadByte();
    00063
    00064     if (code >= 0) {
    00065       /* Plain bytes to read */
    00066       int size = (code == 0) ? 0x80 : code;
    00067       num -= size;
    00068       if (num < 0) return WarnCorruptSprite(file_slot, file_pos, __LINE__);
    00069       for (; size > 0; size--) {
    00070         *dest = FioReadByte();
    00071         dest++;
    00072       }
    00073     } else {
    00074       /* Copy bytes from earlier in the sprite */
    00075       const uint data_offset = ((code & 7) << 8) | FioReadByte();
    00076       if (dest - data_offset < dest_orig) return WarnCorruptSprite(file_slot, file_pos, __LINE__);
    00077       int size = -(code >> 3);
    00078       num -= size;
    00079       if (num < 0) return WarnCorruptSprite(file_slot, file_pos, __LINE__);
    00080       for (; size > 0; size--) {
    00081         *dest = *(dest - data_offset);
    00082         dest++;
    00083       }
    00084     }
    00085   }
    00086
    00087   if (num != 0) return WarnCorruptSprite(file_slot, file_pos, __LINE__);
    00088
    00089   sprite->AllocateData(sprite->width * sprite->height);
    00090
    00091   /* When there are transparency pixels, this format has an other trick.. decode it */
    00092   if (type & 0x08) {
    00093     for (int y = 0; y < sprite->height; y++) {
    00094       bool last_item = false;
    00095       /* Look up in the header-table where the real data is stored for this row */
    00096       int offset = (dest_orig[y * 2 + 1] << 8) | dest_orig[y * 2];
    00097
    00098       /* Go to that row */
    00099       dest = dest_orig + offset;
    00100
    00101       do {
    00102         if (dest + 2 > dest_orig + dest_size) {
    00103           free(sprite->data);
    00104           return WarnCorruptSprite(file_slot, file_pos, __LINE__);
    00105         }
    00106
    00107         SpriteLoader::CommonPixel *data;
    00108         /* Read the header:
    00109          *  0 .. 14  - length
    00110          *  15       - last_item
    00111          *  16 .. 31 - transparency bytes */
    00112         last_item  = ((*dest) & 0x80) != 0;
    00113         int length =  (*dest++) & 0x7F;
    00114         int skip   =   *dest++;
    00115
    00116         data = &sprite->data[y * sprite->width + skip];
    00117
    00118         if (skip + length > sprite->width || dest + length > dest_orig + dest_size) {
    00119           free(sprite->data);
    00120           return WarnCorruptSprite(file_slot, file_pos, __LINE__);
    00121         }
    00122
    00123         for (int x = 0; x < length; x++) {
    00124           switch (sprite_type) {
    00125             case ST_NORMAL: data->m = _palette_remap_grf[file_slot] ? _palette_remap[*dest] : *dest; break;
    00126             case ST_FONT:   data->m = min(*dest, 2u); break;
    00127             default:        data->m = *dest; break;
    00128           }
    00129           dest++;
    00130           data++;
    00131         }
    00132       } while (!last_item);
    00133     }
    00134   } else {
    00135     if (dest_size < sprite->width * sprite->height) {
    00136       free(sprite->data);
    00137       return WarnCorruptSprite(file_slot, file_pos, __LINE__);
    00138     }
    00139
    00140     if (dest_size > sprite->width * sprite->height) {
    00141       static byte warning_level = 0;
    00142       DEBUG(sprite, warning_level, "Ignoring %i unused extra bytes from the sprite from %s at position %i", dest_size - sprite->width * sprite->height, FioGetFilename(file_slot), (int)file_pos);
    00143       warning_level = 6;
    00144     }
    00145
    00146     dest = dest_orig;
    00147
    00148     for (int i = 0; i < sprite->width * sprite->height; i++) {
    00149       switch (sprite_type) {
    00150         case ST_NORMAL: sprite->data[i].m = _palette_remap_grf[file_slot] ? _palette_remap[dest[i]] : dest[i]; break;
    00151         case ST_FONT:   sprite->data[i].m = min(dest[i], 2u); break;
    00152         default:        sprite->data[i].m = dest[i]; break;
    00153       }
    00154     }
    00155   }
    00156
    00157   /* Make sure to mark all transparent pixels transparent on the alpha channel too */
    00158   for (int i = 0; i < sprite->width * sprite->height; i++)
    00159     if (sprite->data[i].m != 0) sprite->data[i].a = 0xFF;
    00160
    00161   return true;
    00162 }