| 1 |
/* Allegro wrapper routines for FreeImage |
| 2 |
* by Karthik Kumar Viswanathan <[email protected]>. |
| 3 |
*/ |
| 4 |
|
| 5 |
#include <FreeImage.h> |
| 6 |
|
| 7 |
#include "allegro5/allegro.h" |
| 8 |
#include "allegro5/allegro_image.h" |
| 9 |
#include "allegro5/internal/aintern_exitfunc.h" |
| 10 |
#include "allegro5/internal/aintern_image.h" |
| 11 |
|
| 12 |
#include "iio.h" |
| 13 |
|
| 14 |
ALLEGRO_DEBUG_CHANNEL("image") |
| 15 |
|
| 16 |
static bool freeimage_initialized = false; |
| 17 |
|
| 18 |
static void _fiio_al_error_handler(FREE_IMAGE_FORMAT fif, const char *message) { |
| 19 |
ALLEGRO_ERROR("FreeImage %s : %s\n", (fif == FIF_UNKNOWN)? "UNKNOWN" : FreeImage_GetFormatFromFIF(fif), message); |
| 20 |
} |
| 21 |
|
| 22 |
bool _al_init_fi(void) |
| 23 |
{ |
| 24 |
if (freeimage_initialized) |
| 25 |
return true; |
| 26 |
FreeImage_Initialise(FALSE); |
| 27 |
_al_add_exit_func(_al_shutdown_fi, "_al_shutdown_fi"); |
| 28 |
FreeImage_SetOutputMessage(_fiio_al_error_handler); |
| 29 |
freeimage_initialized = true; |
| 30 |
return true; |
| 31 |
} |
| 32 |
|
| 33 |
void _al_shutdown_fi(void) |
| 34 |
{ |
| 35 |
if (!freeimage_initialized) |
| 36 |
return; |
| 37 |
FreeImage_DeInitialise(); |
| 38 |
freeimage_initialized = false; |
| 39 |
} |
| 40 |
|
| 41 |
static ALLEGRO_BITMAP *_al_fi_to_al_bitmap(FIBITMAP *fib) { |
| 42 |
int width = 0, height = 0; |
| 43 |
ALLEGRO_BITMAP *bitmap = NULL; |
| 44 |
width = FreeImage_GetWidth(fib); |
| 45 |
height = FreeImage_GetHeight(fib); |
| 46 |
bitmap = al_create_bitmap(width, height); |
| 47 |
if (bitmap) { |
| 48 |
ALLEGRO_LOCKED_REGION *a_lock = al_lock_bitmap(bitmap, |
| 49 |
ALLEGRO_PIXEL_FORMAT_ARGB_8888, ALLEGRO_LOCK_WRITEONLY); |
| 50 |
if (a_lock) { |
| 51 |
unsigned char *out = (unsigned char *)a_lock->data; |
| 52 |
int out_inc = a_lock->pitch - (width*4); |
| 53 |
for (int j=height - 1; j > -1; --j) { |
| 54 |
for (int i=0; i < width; ++i) { |
| 55 |
RGBQUAD color = { 0.0, 0.0, 0.0, 0.0 } ; |
| 56 |
if (FreeImage_GetPixelColor(fib, i, j, &color) == FALSE) { |
| 57 |
ALLEGRO_ERROR("Unable to get pixel data at %d,%d\n", i , j); |
| 58 |
} |
| 59 |
*out++ = (unsigned char) color.rgbBlue; |
| 60 |
*out++ = (unsigned char) color.rgbGreen; |
| 61 |
*out++ = (unsigned char) color.rgbRed; |
| 62 |
*out++ = (unsigned char) color.rgbReserved; |
| 63 |
} |
| 64 |
out += out_inc; |
| 65 |
} |
| 66 |
al_unlock_bitmap(bitmap); |
| 67 |
} |
| 68 |
} |
| 69 |
return bitmap; |
| 70 |
} |
| 71 |
|
| 72 |
ALLEGRO_BITMAP *_al_load_fi_bitmap(const char *filename, int flags) |
| 73 |
{ |
| 74 |
FIBITMAP *fib = NULL; |
| 75 |
ALLEGRO_BITMAP *bitmap = NULL; |
| 76 |
FREE_IMAGE_FORMAT fif = FIF_UNKNOWN; |
| 77 |
|
| 78 |
ASSERT(filename); |
| 79 |
ASSERT(freeimage_initialized == true); |
| 80 |
|
| 81 |
fif = FreeImage_GetFIFFromFilename(filename); |
| 82 |
if (fif == FIF_UNKNOWN) |
| 83 |
fif = FreeImage_GetFileType(filename, 0); |
| 84 |
if (fif == FIF_UNKNOWN) { |
| 85 |
ALLEGRO_WARN("Could not determine the file type for '%s'\n", filename); |
| 86 |
return NULL; |
| 87 |
} |
| 88 |
|
| 89 |
{ |
| 90 |
FIBITMAP *fibRaw = FreeImage_Load(fif, filename, flags); |
| 91 |
if (!fibRaw) |
| 92 |
return NULL; |
| 93 |
fib = FreeImage_ConvertTo32Bits(fibRaw); |
| 94 |
FreeImage_Unload(fibRaw); |
| 95 |
if (!fib) |
| 96 |
return NULL; |
| 97 |
} |
| 98 |
|
| 99 |
bitmap = _al_fi_to_al_bitmap(fib); |
| 100 |
FreeImage_Unload(fib); |
| 101 |
return bitmap; |
| 102 |
} |
| 103 |
|
| 104 |
/* FreeImage requires stdcall on win32 for these. DLL_CALLCONV is provided by FreeImage.h */ |
| 105 |
static unsigned int DLL_CALLCONV _fiio_al_read(void *buffer, unsigned size, unsigned count, fi_handle handle) { |
| 106 |
return (unsigned int) al_fread((ALLEGRO_FILE *)handle, buffer, (count * size)); |
| 107 |
} |
| 108 |
|
| 109 |
static unsigned int DLL_CALLCONV _fiio_al_write(void *buffer, unsigned size, unsigned count, fi_handle handle) { |
| 110 |
return (unsigned int) al_fwrite((ALLEGRO_FILE *)handle, buffer, (count * size)); |
| 111 |
} |
| 112 |
|
| 113 |
static int DLL_CALLCONV _fiio_al_fseek(fi_handle handle, long offset, int origin) { |
| 114 |
return al_fseek((ALLEGRO_FILE *)handle, offset, origin); |
| 115 |
} |
| 116 |
|
| 117 |
static long DLL_CALLCONV _fiio_al_ftell(fi_handle handle) { |
| 118 |
return (long) al_ftell((ALLEGRO_FILE *)handle); |
| 119 |
} |
| 120 |
|
| 121 |
ALLEGRO_BITMAP *_al_load_fi_bitmap_f(ALLEGRO_FILE *f, int flags) |
| 122 |
{ |
| 123 |
FreeImageIO fio; |
| 124 |
ALLEGRO_BITMAP *bitmap = NULL; |
| 125 |
FIBITMAP *fib = NULL; |
| 126 |
FREE_IMAGE_FORMAT fif = FIF_UNKNOWN; |
| 127 |
|
| 128 |
if (flags != 0) { |
| 129 |
ALLEGRO_WARN("Ignoring bitmap loading flags.\n"); |
| 130 |
} |
| 131 |
|
| 132 |
ASSERT(f); |
| 133 |
ASSERT(freeimage_initialized == true); |
| 134 |
|
| 135 |
fio.read_proc = _fiio_al_read; |
| 136 |
fio.write_proc = _fiio_al_write; |
| 137 |
fio.seek_proc = _fiio_al_fseek; |
| 138 |
fio.tell_proc = _fiio_al_ftell; |
| 139 |
|
| 140 |
fif = FreeImage_GetFileTypeFromHandle(&fio, (fi_handle)f, 0); |
| 141 |
if (fif == FIF_UNKNOWN) { |
| 142 |
ALLEGRO_WARN("Could not determine the file type for Allegro file.\n"); |
| 143 |
return NULL; |
| 144 |
} |
| 145 |
|
| 146 |
{ |
| 147 |
FIBITMAP *fibRaw = FreeImage_LoadFromHandle(fif, &fio, (fi_handle)f, 0); |
| 148 |
if (!fibRaw) |
| 149 |
return NULL; |
| 150 |
fib = FreeImage_ConvertTo32Bits(fibRaw); |
| 151 |
FreeImage_Unload(fibRaw); |
| 152 |
if (!fib) |
| 153 |
return NULL; |
| 154 |
} |
| 155 |
|
| 156 |
bitmap = _al_fi_to_al_bitmap(fib); |
| 157 |
FreeImage_Unload(fib); |
| 158 |
return bitmap; |
| 159 |
} |
| 160 |
|
| 161 |
bool _al_identify_fi(ALLEGRO_FILE *f) |
| 162 |
{ |
| 163 |
FreeImageIO fio; |
| 164 |
FREE_IMAGE_FORMAT fif = FIF_UNKNOWN; |
| 165 |
|
| 166 |
ASSERT(f); |
| 167 |
ASSERT(freeimage_initialized == true); |
| 168 |
|
| 169 |
fio.read_proc = _fiio_al_read; |
| 170 |
fio.write_proc = _fiio_al_write; |
| 171 |
fio.seek_proc = _fiio_al_fseek; |
| 172 |
fio.tell_proc = _fiio_al_ftell; |
| 173 |
|
| 174 |
fif = FreeImage_GetFileTypeFromHandle(&fio, (fi_handle)f, 0); |
| 175 |
if (fif == FIF_UNKNOWN) { |
| 176 |
ALLEGRO_WARN("Could not determine the file type for Allegro file.\n"); |
| 177 |
return false; |
| 178 |
} |
| 179 |
|
| 180 |
return true; |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
/* vim: set sts=3 sw=3 et: */ |