view dmimage.c @ 96:6bf5220fa47e

Urgh .. use memset to silence some bogus GCC warnings about using potentially uninitialized values, while that will not actually be possible. In any case, it is annoying.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 02 Oct 2012 18:52:28 +0300
parents 32250b436bca
children dd59a650a318
line wrap: on
line source

/*
 * DMLib
 * -- Bitmap image conversion and loading
 * Programmed and designed by Matti 'ccr' Hamalainen
 * (C) Copyright 2012 Tecnic Software productions (TNSP)
 */
#include "dmimage.h"


#define STBI_FAILURE_USERMSG 1
#define STBI_NO_STDIO 1
#define STBI_NO_HDR 1
#include "stb_image.c"


SDL_Surface *dmCreateRGBSurfaceFrom(void *data, const int width, const int height, const int depth, const int pitch, const int rmask, const int gmask, const int bmask, const int amask)
{
    SDL_Surface *tmp, *pixtmp, *result = NULL;
    
    tmp = SDL_CreateRGBSurfaceFrom(data, width, height, depth, pitch, bmask, gmask, rmask, amask);

    pixtmp = SDL_CreateRGBSurface(SDL_SWSURFACE | SDL_SRCALPHA, 10, 10, 32, rmask, gmask, bmask, amask);
    if (tmp != NULL && pixtmp != NULL)
    {
        result = SDL_ConvertSurface(tmp, pixtmp->format, SDL_SWSURFACE | SDL_SRCALPHA);
        SDL_FreeSurface(tmp);
    }
    if (pixtmp != NULL)
        SDL_FreeSurface(pixtmp);
    
    return result;
}


SDL_Surface *dmCreatePaletteSurfaceFrom(void *data, const int width, const int height, const int pitch)
{
    int yc;
    Uint8 *dst, *src;
    SDL_Surface *result;
    result = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 8, 0,0,0,0);
    if (result == NULL)
        return NULL;
    
    dst = result->pixels;
    src = data;
    for (yc = 0; yc < height; yc++)
    {
        memcpy(dst, src, width * sizeof(Uint8));
        dst += result->pitch;
        src += pitch;
    }

    return result;
}



static int dmSTBIread(void *user, char *data, int size)
{
    return dmfread(data, 1, size, (DMResource *) user);
}


static void dmSTBIskip(void *user, unsigned int n)
{
    dmfseek((DMResource *) user, n, SEEK_CUR);
}


static int dmSTBIeof(void *user)
{
    return dmfeof((DMResource *) user);
}


static const stbi_io_callbacks dmSTBICallbacks =
{
    dmSTBIread,
    dmSTBIskip,
    dmSTBIeof,
};


SDL_Surface *dmLoadImage(DMResource *file)
{
    Uint32 rmask, gmask, bmask, amask;
    SDL_Surface *result = NULL;
    int width, height, comp;
    Uint8 *data;

    data = stbi_load_from_callbacks(&dmSTBICallbacks, file, &width, &height, &comp, 0);

    if (data == NULL)
    {
        dmError("Error decoding image resource %p '%s' [%d, %d, %d]: %s\n",
            file, file->filename, width, height, comp, stbi_failure_reason());
        return NULL;
    }

#if SDL_BYTEORDER == SDL_BIG_ENDIAN
    rmask = 0xff000000;
    gmask = 0x00ff0000;
    bmask = 0x0000ff00;
    amask = 0x000000ff;
#else
    rmask = 0x000000ff;
    gmask = 0x0000ff00;
    bmask = 0x00ff0000;
    amask = 0xff000000;
#endif

    switch (comp)
    {
        case 4:
        case 3:
            result = dmCreateRGBSurfaceFrom(data, width, height, comp * 8,
                width * comp, rmask, gmask, bmask, amask);
            break;

        case 1:
            result = dmCreatePaletteSurfaceFrom(data, width, height, width * comp);
            break;
    }
    
    stbi_image_free(data);

    if (result == NULL)
    {
        dmError("Format conversion failed for image resource %p '%s' [%d, %d, %d].\n",
            file, file->filename, width, height, comp);
    }

    return result;
}