/*
* Copyright (C) 2008 Tilman Sauerbeck (tilman at code-monkey de)
*
* This program 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; either version 2 of the
* License, or (at your option) any later version.
*
* This program 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. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
#include
#include
#include
SDL_Surface *
ad_load_img (SDL_RWops *src, uint8_t *palette)
{
SDL_Surface *surface;
Uint8 *row;
const char *error = NULL;
uint16_t magic;
int i, j, start, width, height;
assert (src);
assert (palette);
start = SDL_RWtell (src);
magic = SDL_ReadLE16 (src);
width = SDL_ReadLE32 (src);
height = SDL_ReadLE32 (src);
SDL_ReadLE32 (src);
SDL_ReadLE32 (src);
surface = SDL_AllocSurface (SDL_SWSURFACE, width, height,
8, 0, 0, 0, 0);
if (!surface) {
error = "cannot allocate surface";
goto done;
}
for (i = 0, j = 0; i < surface->format->palette->ncolors; i++) {
surface->format->palette->colors[i].r = palette[j++];
surface->format->palette->colors[i].g = palette[j++];
surface->format->palette->colors[i].b = palette[j++];
}
row = surface->pixels;
switch (magic) {
case 0x0400:
SDL_SetColorKey (surface, SDL_SRCCOLORKEY,
SDL_MapRGB (surface->format, 0, 0, 0));
SDL_ReadLE32 (src); /* data size? */
while (height--) {
uint16_t n;
n = SDL_ReadLE16 (src);
if (n) {
memset (row, 0x00, n);
row += n;
}
n = SDL_ReadLE16 (src);
SDL_RWread (src, row, 1, n);
row += n;
n = SDL_ReadLE16 (src);
if (n) {
memset (row, 0x00, n);
row += n;
}
SDL_ReadLE16 (src);
row -= width;
row += surface->pitch;
}
break;
case 0x0401:
while (height--) {
SDL_RWread (src, row, 1, width);
row += surface->pitch;
}
break;
}
done:
if (error) {
SDL_RWseek (src, start, SEEK_SET);
if (surface) {
SDL_FreeSurface (surface);
surface = NULL;
}
IMG_SetError (error);
}
return surface;
}