Skip to content

Commit

Permalink
Implement hooks for image decoding into MojoSetup.
Browse files Browse the repository at this point in the history
  • Loading branch information
icculus committed Nov 24, 2007
1 parent b66b92d commit 9e78831
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 0 deletions.
28 changes: 28 additions & 0 deletions mojosetup.c
Expand Up @@ -583,6 +583,34 @@ void bz_internal_error(int errcode)
#endif


#if SUPPORT_STBIMAGE
unsigned char *stbi_load_from_memory(unsigned char *buffer, int len, int *x,
int *y, int *comp, int req_comp);
#endif

uint8 *decodeImage(const uint8 *data, uint32 size, uint32 *w, uint32 *h)
{
uint8 *retval = MojoPlatform_decodeImage(data, size, w, h);

#if SUPPORT_STBIMAGE
if (retval == NULL) // try our built-in routines.
{
const int siz = (int) size;
unsigned char *buf = (unsigned char *) data;
int x = 0, y = 0, comp = 0;
retval = (uint8 *) stbi_load_from_memory(buf, siz, &x, &y, &comp, 4);
*w = (uint32) x;
*h = (uint32) y;
} // if
#endif

if (retval == NULL)
*w = *h = 0;

return retval;
} // decodeImage


// This is called from main()/WinMain()/whatever.
int MojoSetup_main(int argc, char **argv)
{
Expand Down
14 changes: 14 additions & 0 deletions platform.h
Expand Up @@ -239,6 +239,20 @@ void MojoPlatform_sleep(uint32 ticks);
// characters. You should supply if needed.
void MojoPlatform_log(const char *str);

// This tries to decode a graphic file in memory into an RGBA framebuffer.
// Most platforms return NULL here. No one should call this; use decodeImage()
// instead, which will try included platform-independent code if this fails.
// This function is just here to allow a platform with the appropriate
// functionality to work without compiling in stb_image.c, or supply more
// formats over the built-in code.
// (data) points to the compressed data, (size) is the number of bytes
// of compressed data. (*w) and (*h) will contain the images dimensions on
// return.
// Returns NULL on failure (unsupported, etc) and a pointer to the
// uncompressed data on success. Caller must free() the returned pointer!
uint8 *MojoPlatform_decodeImage(const uint8 *data, uint32 size,
uint32 *w, uint32 *h);

// Get the current locale, in the format "xx_YY" where "xx" is the language
// (en, fr, de...) and "_YY" is the country. (_US, _CA, etc). The country
// can be omitted. Don't include encoding, it's always UTF-8 at this time.
Expand Down
8 changes: 8 additions & 0 deletions platform_unix.c
Expand Up @@ -1017,6 +1017,14 @@ void MojoPlatform_spawnTerminal(void)
} // MojoPlatform_spawnTerminal


uint8 *MojoPlatform_decodeImage(const uint8 *data, uint32 size,
uint32 *w, uint32 *h)
{
// !!! FIXME: try Quartz APIs on Mac OS X?
return NULL; // no platform-specific APIs. Just use the built-in ones.
} // MojoPlatform_decodeImage


static void signal_catcher(int sig)
{
static boolean first_shot = true;
Expand Down
7 changes: 7 additions & 0 deletions platform_windows.c
Expand Up @@ -641,6 +641,13 @@ void MojoPlatform_spawnTerminal(void)
} // MojoPlatform_spawnTerminal


uint8 *MojoPlatform_decodeImage(const uint8 *data, uint32 size,
uint32 *w, uint32 *h)
{
return NULL; // !!! FIXME: try IPicture?
} // MojoPlatform_decodeImage


char *MojoPlatform_currentWorkingDir(void)
{
char *retval = NULL;
Expand Down
9 changes: 9 additions & 0 deletions universal.h
Expand Up @@ -192,6 +192,15 @@ const char *translate(const char *str);
// profile("Something I did", start);
uint32 profile(const char *what, uint32 start_time);

// This tries to decode a graphic file in memory into an RGBA framebuffer,
// first with platform-specific facilities, if any, and then any built-in
// decoders, if that fails.
// (data) points to the compressed data, (size) is the number of bytes
// of compressed data. (*w) and (*h) will contain the images dimensions on
// return.
// Returns NULL on failure (unsupported, etc) and a pointer to the
// uncompressed data on success. Caller must free() the returned pointer!
uint8 *decodeImage(const uint8 *data, uint32 size, uint32 *w, uint32 *h);

// See if a given flag was on the command line
//
Expand Down

0 comments on commit 9e78831

Please sign in to comment.