# grf.cpp

Go to the documentation of this file.

    00001 /* $Id: grf.cpp 18683 2010-01-01 18:45:40Z rubidium $ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
    00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
    00008  */
    00009
    00012 #include "../stdafx.h"
    00013 #include "../gfx_func.h"
    00014 #include "../fileio_func.h"
    00015 #include "../debug.h"
    00016 #include "../strings_func.h"
    00017 #include "table/strings.h"
    00018 #include "../gui.h"
    00019 #include "grf.hpp"
    00020
    00029 static bool WarnCorruptSprite(uint8 file_slot, size_t file_pos, int line)
    00030 {
    00031   static byte warning_level = 0;
    00032   if (warning_level == 0) {
    00033     SetDParamStr(0, FioGetFilename(file_slot));
    00034     ShowErrorMessage(STR_NEWGRF_ERROR_CORRUPT_SPRITE, INVALID_STRING_ID, 0, 0);
    00035   }
    00036   DEBUG(sprite, warning_level, "[%i] Loading corrupted sprite from %s at position %i", line, FioGetFilename(file_slot), (int)file_pos);
    00037   warning_level = 6;
    00038   return false;
    00039 }
    00040
    00041 bool SpriteLoaderGrf::LoadSprite(SpriteLoader::Sprite *sprite, uint8 file_slot, size_t file_pos, SpriteType sprite_type)
    00042 {
    00043   /* Open the right file and go to the correct position */
    00044   FioSeekToFile(file_slot, file_pos);
    00045
    00046   /* Read the size and type */
    00047   int num = FioReadWord();
    00048   byte type = FioReadByte();
    00049
    00050   /* Type 0xFF indicates either a colourmap or some other non-sprite info; we do not handle them here */
    00051   if (type == 0xFF) return false;
    00052
    00053   sprite->height = FioReadByte();
    00054   sprite->width  = FioReadWord();
    00055   sprite->x_offs = FioReadWord();
    00056   sprite->y_offs = FioReadWord();
    00057
    00058   /* 0x02 indicates it is a compressed sprite, so we can't rely on 'num' to be valid.
    00059    *  In case it is uncompressed, the size is 'num' - 8 (header-size). */
    00060   num = (type & 0x02) ? sprite->width * sprite->height : num - 8;
    00061
    00062   byte *dest_orig = AllocaM(byte, num);
    00063   byte *dest = dest_orig;
    00064   const int dest_size = num;
    00065
    00066   /* Read the file, which has some kind of compression */
    00067   while (num > 0) {
    00068     int8 code = FioReadByte();
    00069
    00070     if (code >= 0) {
    00071       /* Plain bytes to read */
    00072       int size = (code == 0) ? 0x80 : code;
    00073       num -= size;
    00074       if (num < 0) return WarnCorruptSprite(file_slot, file_pos, __LINE__);
    00075       for (; size > 0; size--) {
    00076         *dest = FioReadByte();
    00077         dest++;
    00078       }
    00079     } else {
    00080       /* Copy bytes from earlier in the sprite */
    00081       const uint data_offset = ((code & 7) << 8) | FioReadByte();
    00082       if (dest - data_offset < dest_orig) return WarnCorruptSprite(file_slot, file_pos, __LINE__);
    00083       int size = -(code >> 3);
    00084       num -= size;
    00085       if (num < 0) return WarnCorruptSprite(file_slot, file_pos, __LINE__);
    00086       for (; size > 0; size--) {
    00087         *dest = *(dest - data_offset);
    00088         dest++;
    00089       }
    00090     }
    00091   }
    00092
    00093   if (num != 0) return WarnCorruptSprite(file_slot, file_pos, __LINE__);
    00094
    00095   sprite->AllocateData(sprite->width * sprite->height);
    00096
    00097   /* When there are transparency pixels, this format has another trick.. decode it */
    00098   if (type & 0x08) {
    00099     for (int y = 0; y < sprite->height; y++) {
    00100       bool last_item = false;
    00101       /* Look up in the header-table where the real data is stored for this row */
    00102       int offset = (dest_orig[y * 2 + 1] << 8) | dest_orig[y * 2];
    00103
    00104       /* Go to that row */
    00105       dest = dest_orig + offset;
    00106
    00107       do {
    00108         if (dest + 2 > dest_orig + dest_size) {
    00109           free(sprite->data);
    00110           return WarnCorruptSprite(file_slot, file_pos, __LINE__);
    00111         }
    00112
    00113         SpriteLoader::CommonPixel *data;
    00114         /* Read the header:
    00115          *  0 .. 14  - length
    00116          *  15       - last_item
    00117          *  16 .. 31 - transparency bytes */
    00118         last_item  = ((*dest) & 0x80) != 0;
    00119         int length =  (*dest++) & 0x7F;
    00120         int skip   =   *dest++;
    00121
    00122         data = &sprite->data[y * sprite->width + skip];
    00123
    00124         if (skip + length > sprite->width || dest + length > dest_orig + dest_size) {
    00125           free(sprite->data);
    00126           return WarnCorruptSprite(file_slot, file_pos, __LINE__);
    00127         }
    00128
    00129         for (int x = 0; x < length; x++) {
    00130           switch (sprite_type) {
    00131             case ST_NORMAL: data->m = _palette_remap_grf[file_slot] ? _palette_remap[*dest] : *dest; break;
    00132             case ST_FONT:   data->m = min(*dest, 2u); break;
    00133             default:        data->m = *dest; break;
    00134           }
    00135           dest++;
    00136           data++;
    00137         }
    00138       } while (!last_item);
    00139     }
    00140   } else {
    00141     if (dest_size < sprite->width * sprite->height) {
    00142       free(sprite->data);
    00143       return WarnCorruptSprite(file_slot, file_pos, __LINE__);
    00144     }
    00145
    00146     if (dest_size > sprite->width * sprite->height) {
    00147       static byte warning_level = 0;
    00148       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);
    00149       warning_level = 6;
    00150     }
    00151
    00152     dest = dest_orig;
    00153
    00154     for (int i = 0; i < sprite->width * sprite->height; i++) {
    00155       switch (sprite_type) {
    00156         case ST_NORMAL: sprite->data[i].m = _palette_remap_grf[file_slot] ? _palette_remap[dest[i]] : dest[i]; break;
    00157         case ST_FONT:   sprite->data[i].m = min(dest[i], 2u); break;
    00158         default:        sprite->data[i].m = dest[i]; break;
    00159       }
    00160     }
    00161   }
    00162
    00163   /* Make sure to mark all transparent pixels transparent on the alpha channel too */
    00164   for (int i = 0; i < sprite->width * sprite->height; i++)
    00165     if (sprite->data[i].m != 0) sprite->data[i].a = 0xFF;
    00166
    00167   return true;
    00168 }