Skip to content

Instantly share code, notes, and snippets.

@albertz
Created January 2, 2012 16:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save albertz/1551241 to your computer and use it in GitHub Desktop.
Save albertz/1551241 to your computer and use it in GitHub Desktop.
test for SDL_image
/*
g++ sdlimagetest.cpp -framework SDL -framework SDLimage -I /Library/Frameworks/SDL.framework/Headers -I /Library/Frameworks/SDLimage.framework/Headers -o sdlimagetest
*/
#include <SDL.h>
#include <SDL_image.h>
#include <assert.h>
#include <sstream>
#include <string>
#include <iostream>
using namespace std;
Uint32 GetPixel(SDL_Surface* s, int x, int y) {
char* addr = (char*)s->pixels + y * s->pitch + x * s->format->BytesPerPixel;
switch(s->format->BytesPerPixel) {
case 4: return *(Uint32*)addr;
case 3: return
Uint32(*(Uint8*)addr) +
(Uint32(*(Uint8*)(addr+1)) << 8) +
(Uint32(*(Uint8*)(addr+2)) << 16);
case 2: return *(Uint16*)addr;
case 1: return (Uint8)*addr;
default: assert(false);
}
}
void DumpPixelFormat(const SDL_PixelFormat* format) {
std::ostringstream str;
str << "PixelFormat:" << std::endl
<< " BitsPerPixel: " << (int)format->BitsPerPixel << ","
<< " BytesPerPixel: " << (int)format->BytesPerPixel << std::endl
<< " R/G/B/A mask: " << std::hex
<< (uint)format->Rmask << "/"
<< (uint)format->Gmask << "/"
<< (uint)format->Bmask << "/"
<< (uint)format->Amask << std::endl
<< " R/G/B/A loss: "
<< (uint)format->Rloss << "/"
<< (uint)format->Gloss << "/"
<< (uint)format->Bloss << "/"
<< (uint)format->Aloss << std::endl
<< " Colorkey: " << std::hex << (uint)format->colorkey << ","
<< " Alpha: " << std::dec << (int)format->alpha;
cout << str.str() << endl;
}
void DumpSurface(SDL_Surface* s) {
DumpPixelFormat(s->format);
for(int y = 0; y < std::min(10, s->h); ++y) {
for(int x = 0; x < std::min(10, s->w); ++x)
printf(" %.8X", GetPixel(s, x, y));
printf("\n");
}
}
#undef main
char imgdata[] = {
0x89,0x50,0x4E,0x47,0x0D,0x0A,0x1A,0x0A,0x00,0x00,
0x00,0x0D,0x49,0x48,0x44,0x52,0x00,0x00,0x00,0x01,
0x00,0x00,0x00,0x0D,0x08,0x06,0x00,0x00,0x01,0x1F,
0xD0,0x34,0x64,0x00,0x00,0x00,0x01,0x73,0x52,0x47,
0x42,0x00,0xAE,0xCE,0x1C,0xE9,0x00,0x00,0x00,0x06,
0x62,0x4B,0x47,0x44,0x00,0xFF,0x00,0xFF,0x00,0xFF,
0xA0,0xBD,0xA7,0x93,0x00,0x00,0x00,0x09,0x70,0x48,
0x59,0x73,0x00,0x00,0x0B,0x13,0x00,0x00,0x0B,0x13,
0x01,0x00,0x9A,0x9C,0x18,0x00,0x00,0x00,0x07,0x74,
0x49,0x4D,0x45,0x07,0xDC,0x01,0x02,0x0F,0x3A,0x2B,
0xBC,0x08,0xF7,0xCB,0x00,0x00,0x00,0x16,0x49,0x44,
0x41,0x54,0x08,0xD7,0x63,0x60,0x60,0xF8,0xFF,0x9F,
0x89,0x01,0x02,0x30,0x58,0xD8,0xB8,0x68,0x04,0x00,
0x6A,0xEE,0x08,0x0B,0xBF,0x8A,0x7D,0x51,0x00,0x00,
0x00,0x00,0x49,0x45,0x4E,0x44,0xAE,0x42,0x60,0x82,
};
int main() {
SDL_Surface* img = IMG_Load_RW(SDL_RWFromMem(imgdata, sizeof(imgdata)), true);
assert(img != NULL);
DumpSurface(img);
SDL_FreeSurface(img);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment