Mercurial > hg > dmlib
diff dmimage.c @ 0:32250b436bca
Initial re-import.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Fri, 28 Sep 2012 01:54:23 +0300 |
parents | |
children | dd59a650a318 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dmimage.c Fri Sep 28 01:54:23 2012 +0300 @@ -0,0 +1,134 @@ +/* + * 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; +}