| 1 |
#include <windows.h> |
| 2 |
#include <objidl.h> |
| 3 |
|
| 4 |
#include "allegro5/allegro.h" |
| 5 |
#include "allegro5/allegro_image.h" |
| 6 |
#include "allegro5/internal/aintern.h" |
| 7 |
#include "allegro5/internal/aintern_convert.h" |
| 8 |
#include "allegro5/internal/aintern_exitfunc.h" |
| 9 |
#include "allegro5/internal/aintern_image.h" |
| 10 |
|
| 11 |
#include "iio.h" |
| 12 |
|
| 13 |
#ifdef ALLEGRO_CFG_IIO_HAVE_GDIPLUS_LOWERCASE_H |
| 14 |
#include <gdiplus.h> |
| 15 |
#else |
| 16 |
#include <GdiPlus.h> |
| 17 |
#endif |
| 18 |
|
| 19 |
ALLEGRO_DEBUG_CHANNEL("image") |
| 20 |
|
| 21 |
/* Needed with the MinGW w32api-3.15 headers. */ |
| 22 |
using namespace Gdiplus; |
| 23 |
|
| 24 |
#if !defined(_MSC_VER) && !defined(__uuidof) |
| 25 |
#define __uuidof(x) (IID_ ## x) |
| 26 |
#endif |
| 27 |
|
| 28 |
static bool gdiplus_inited = false; |
| 29 |
static ULONG_PTR gdiplusToken = 0; |
| 30 |
|
| 31 |
/* Source: |
| 32 |
* http://msdn.microsoft.com/en-us/library/ms533843%28VS.85%29.aspx |
| 33 |
*/ |
| 34 |
static int GetEncoderClsid(const WCHAR* format, CLSID* pClsid) |
| 35 |
{ |
| 36 |
UINT num = 0; // number of image encoders |
| 37 |
UINT size = 0; // size of the image encoder array in bytes |
| 38 |
|
| 39 |
Gdiplus::ImageCodecInfo* pImageCodecInfo = NULL; |
| 40 |
|
| 41 |
Gdiplus::GetImageEncodersSize(&num, &size); |
| 42 |
if (size == 0) { |
| 43 |
return -1; |
| 44 |
} |
| 45 |
|
| 46 |
pImageCodecInfo = (Gdiplus::ImageCodecInfo*)(al_malloc(size)); |
| 47 |
if (pImageCodecInfo == NULL) { |
| 48 |
return -1; |
| 49 |
} |
| 50 |
|
| 51 |
GetImageEncoders(num, size, pImageCodecInfo); |
| 52 |
|
| 53 |
for (UINT j = 0; j < num; ++j) { |
| 54 |
if(wcscmp(pImageCodecInfo[j].MimeType, format) == 0) { |
| 55 |
*pClsid = pImageCodecInfo[j].Clsid; |
| 56 |
al_free(pImageCodecInfo); |
| 57 |
return j; |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
al_free(pImageCodecInfo); |
| 62 |
return -1; |
| 63 |
} |
| 64 |
|
| 65 |
/* A wrapper around an already opened ALLEGRO_FILE* pointer |
| 66 |
*/ |
| 67 |
class AllegroWindowsStream : public IStream |
| 68 |
{ |
| 69 |
long refCount; |
| 70 |
ALLEGRO_FILE *fp; |
| 71 |
|
| 72 |
public: |
| 73 |
/* Create a stream from an open file handle */ |
| 74 |
AllegroWindowsStream(ALLEGRO_FILE *fp) : |
| 75 |
refCount(1), |
| 76 |
fp(fp) |
| 77 |
{ |
| 78 |
this->fp = fp; |
| 79 |
} |
| 80 |
|
| 81 |
virtual ~AllegroWindowsStream() |
| 82 |
{ |
| 83 |
} |
| 84 |
|
| 85 |
/* IUnknown */ |
| 86 |
virtual ULONG STDMETHODCALLTYPE AddRef(void) |
| 87 |
{ |
| 88 |
return (ULONG) InterlockedIncrement(&refCount); |
| 89 |
} |
| 90 |
|
| 91 |
virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void **ppvObject) |
| 92 |
{ |
| 93 |
if (iid == __uuidof(IUnknown) || iid == __uuidof(ISequentialStream) |
| 94 |
|| iid == __uuidof(IStream)) { |
| 95 |
*ppvObject = static_cast<IStream*>(this); |
| 96 |
AddRef(); |
| 97 |
return S_OK; |
| 98 |
} |
| 99 |
else { |
| 100 |
return E_NOINTERFACE; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
virtual ULONG STDMETHODCALLTYPE Release(void) |
| 105 |
{ |
| 106 |
ULONG ret = InterlockedDecrement(&refCount); |
| 107 |
if (ret == 0) { |
| 108 |
delete this; |
| 109 |
return 0; |
| 110 |
} |
| 111 |
return ret; |
| 112 |
} |
| 113 |
|
| 114 |
/* ISequentialStream */ |
| 115 |
virtual HRESULT STDMETHODCALLTYPE Read(void *pv, ULONG cb, ULONG *pcbRead) |
| 116 |
{ |
| 117 |
size_t read = al_fread(fp, pv, cb); |
| 118 |
if (pcbRead) { |
| 119 |
*pcbRead = read; |
| 120 |
} |
| 121 |
return read == cb ? S_OK : S_FALSE; |
| 122 |
} |
| 123 |
|
| 124 |
virtual HRESULT STDMETHODCALLTYPE Write(const void *pv, ULONG cb, |
| 125 |
ULONG *pcbWritten) |
| 126 |
{ |
| 127 |
size_t written = al_fwrite(fp, pv, cb); |
| 128 |
if (pcbWritten) { |
| 129 |
*pcbWritten = written; |
| 130 |
} |
| 131 |
return written == cb ? S_OK : STG_E_CANTSAVE; |
| 132 |
} |
| 133 |
|
| 134 |
/* IStream */ |
| 135 |
virtual HRESULT STDMETHODCALLTYPE Seek(LARGE_INTEGER dlibMove, DWORD dwOrigin, |
| 136 |
ULARGE_INTEGER *plibNewPosition) |
| 137 |
{ |
| 138 |
ALLEGRO_SEEK o; |
| 139 |
if (dwOrigin == STREAM_SEEK_SET) { |
| 140 |
o = ALLEGRO_SEEK_SET; |
| 141 |
} |
| 142 |
else if (dwOrigin == STREAM_SEEK_CUR) { |
| 143 |
o = ALLEGRO_SEEK_CUR; |
| 144 |
} |
| 145 |
else { |
| 146 |
o = ALLEGRO_SEEK_END; |
| 147 |
} |
| 148 |
|
| 149 |
bool ret = al_fseek(fp, dlibMove.QuadPart, o); |
| 150 |
|
| 151 |
if (plibNewPosition) { |
| 152 |
int64_t pos = al_ftell(fp); |
| 153 |
if (pos == -1) { |
| 154 |
return STG_E_INVALIDFUNCTION; |
| 155 |
} |
| 156 |
|
| 157 |
plibNewPosition->QuadPart = pos; |
| 158 |
} |
| 159 |
|
| 160 |
return ret ? S_OK : STG_E_INVALIDFUNCTION; |
| 161 |
} |
| 162 |
|
| 163 |
/* The GDI+ image I/O methods need to know the file size */ |
| 164 |
virtual HRESULT STDMETHODCALLTYPE Stat(STATSTG *pstatstg, DWORD grfStatFlag) |
| 165 |
{ |
| 166 |
(void) grfStatFlag; |
| 167 |
memset(pstatstg, 0, sizeof(*pstatstg)); |
| 168 |
pstatstg->type = STGTY_STREAM; |
| 169 |
pstatstg->cbSize.QuadPart = al_fsize(fp); |
| 170 |
return S_OK; |
| 171 |
} |
| 172 |
|
| 173 |
/* The following IStream methods aren't needed */ |
| 174 |
virtual HRESULT STDMETHODCALLTYPE Clone(IStream **ppstm) |
| 175 |
{ |
| 176 |
(void) ppstm; |
| 177 |
return E_NOTIMPL; |
| 178 |
} |
| 179 |
|
| 180 |
virtual HRESULT STDMETHODCALLTYPE Commit(DWORD grfCommitFlags) |
| 181 |
{ |
| 182 |
(void) grfCommitFlags; |
| 183 |
return E_NOTIMPL; |
| 184 |
} |
| 185 |
|
| 186 |
virtual HRESULT STDMETHODCALLTYPE CopyTo (IStream *pstm, ULARGE_INTEGER cb, |
| 187 |
ULARGE_INTEGER *pcbRead, ULARGE_INTEGER *pcbWritten) |
| 188 |
{ |
| 189 |
(void) pstm; |
| 190 |
(void) cb; |
| 191 |
(void) pcbRead; |
| 192 |
(void) pcbWritten; |
| 193 |
return E_NOTIMPL; |
| 194 |
} |
| 195 |
|
| 196 |
virtual HRESULT STDMETHODCALLTYPE LockRegion(ULARGE_INTEGER libOffset, |
| 197 |
ULARGE_INTEGER cb, DWORD dwLockType) |
| 198 |
{ |
| 199 |
(void) libOffset; |
| 200 |
(void) cb; |
| 201 |
(void) dwLockType; |
| 202 |
return E_NOTIMPL; |
| 203 |
} |
| 204 |
|
| 205 |
virtual HRESULT STDMETHODCALLTYPE Revert() |
| 206 |
{ |
| 207 |
return E_NOTIMPL; |
| 208 |
} |
| 209 |
|
| 210 |
virtual HRESULT STDMETHODCALLTYPE SetSize(ULARGE_INTEGER libNewSize) |
| 211 |
{ |
| 212 |
(void) libNewSize; |
| 213 |
return E_NOTIMPL; |
| 214 |
} |
| 215 |
|
| 216 |
virtual HRESULT STDMETHODCALLTYPE UnlockRegion(ULARGE_INTEGER libOffset, |
| 217 |
ULARGE_INTEGER cb, DWORD dwLockType) |
| 218 |
{ |
| 219 |
(void) libOffset; |
| 220 |
(void) cb; |
| 221 |
(void) dwLockType; |
| 222 |
return E_NOTIMPL; |
| 223 |
} |
| 224 |
}; |
| 225 |
|
| 226 |
static void load_indexed_data(Gdiplus::Bitmap *gdi_bmp, |
| 227 |
ALLEGRO_BITMAP *a_bmp, uint32_t w, uint32_t h) |
| 228 |
{ |
| 229 |
Gdiplus::BitmapData *gdi_lock = new Gdiplus::BitmapData(); |
| 230 |
Gdiplus::Rect rect(0, 0, w, h); |
| 231 |
|
| 232 |
if (!gdi_bmp->LockBits(&rect, Gdiplus::ImageLockModeRead, |
| 233 |
PixelFormat8bppIndexed, gdi_lock)) |
| 234 |
{ |
| 235 |
ALLEGRO_LOCKED_REGION *a_lock = al_lock_bitmap(a_bmp, |
| 236 |
ALLEGRO_PIXEL_FORMAT_SINGLE_CHANNEL_8, ALLEGRO_LOCK_WRITEONLY); |
| 237 |
|
| 238 |
if (a_lock) { |
| 239 |
unsigned char *in = (unsigned char *)gdi_lock->Scan0; |
| 240 |
unsigned char *out = (unsigned char *)a_lock->data; |
| 241 |
|
| 242 |
if (gdi_lock->Stride == a_lock->pitch) { |
| 243 |
memcpy(out, in, h * gdi_lock->Stride); |
| 244 |
} |
| 245 |
else { |
| 246 |
uint32_t rows = h; |
| 247 |
while (rows--) { |
| 248 |
memcpy(out, in, w); |
| 249 |
in += gdi_lock->Stride; |
| 250 |
out += a_lock->pitch; |
| 251 |
} |
| 252 |
} |
| 253 |
al_unlock_bitmap(a_bmp); |
| 254 |
} |
| 255 |
|
| 256 |
gdi_bmp->UnlockBits(gdi_lock); |
| 257 |
} |
| 258 |
|
| 259 |
delete gdi_lock; |
| 260 |
} |
| 261 |
|
| 262 |
static void load_non_indexed_data(Gdiplus::Bitmap *gdi_bmp, |
| 263 |
ALLEGRO_BITMAP *a_bmp, uint32_t w, uint32_t h, bool premul) |
| 264 |
{ |
| 265 |
Gdiplus::BitmapData *gdi_lock = new Gdiplus::BitmapData(); |
| 266 |
Gdiplus::Rect rect(0, 0, w, h); |
| 267 |
|
| 268 |
if (!gdi_bmp->LockBits(&rect, Gdiplus::ImageLockModeRead, |
| 269 |
PixelFormat32bppARGB, gdi_lock)) |
| 270 |
{ |
| 271 |
ALLEGRO_LOCKED_REGION *a_lock = al_lock_bitmap(a_bmp, |
| 272 |
ALLEGRO_PIXEL_FORMAT_ARGB_8888, ALLEGRO_LOCK_WRITEONLY); |
| 273 |
|
| 274 |
if (a_lock) { |
| 275 |
unsigned char *in = (unsigned char *)gdi_lock->Scan0; |
| 276 |
unsigned char *out = (unsigned char *)a_lock->data; |
| 277 |
|
| 278 |
if (premul) { |
| 279 |
int in_inc = gdi_lock->Stride - (w*4); |
| 280 |
int out_inc = a_lock->pitch - (w*4); |
| 281 |
for (unsigned int y = 0; y < h; y++) { |
| 282 |
for (unsigned int x = 0; x < w; x++) { |
| 283 |
unsigned char r, g, b, a; |
| 284 |
b = *in++; |
| 285 |
g = *in++; |
| 286 |
r = *in++; |
| 287 |
a = *in++; |
| 288 |
b = b * a / 255; |
| 289 |
g = g * a / 255; |
| 290 |
r = r * a / 255; |
| 291 |
*out++ = b; |
| 292 |
*out++ = g; |
| 293 |
*out++ = r; |
| 294 |
*out++ = a; |
| 295 |
} |
| 296 |
in += in_inc; |
| 297 |
out += out_inc; |
| 298 |
} |
| 299 |
} |
| 300 |
else { |
| 301 |
if (gdi_lock->Stride == a_lock->pitch) { |
| 302 |
memcpy(out, in, h * gdi_lock->Stride); |
| 303 |
} |
| 304 |
else { |
| 305 |
uint32_t rows = h; |
| 306 |
while (rows--) { |
| 307 |
memcpy(out, in, w * 4); |
| 308 |
in += gdi_lock->Stride; |
| 309 |
out += a_lock->pitch; |
| 310 |
} |
| 311 |
} |
| 312 |
} |
| 313 |
al_unlock_bitmap(a_bmp); |
| 314 |
} |
| 315 |
|
| 316 |
gdi_bmp->UnlockBits(gdi_lock); |
| 317 |
} |
| 318 |
|
| 319 |
delete gdi_lock; |
| 320 |
} |
| 321 |
|
| 322 |
ALLEGRO_BITMAP *_al_load_gdiplus_bitmap_f(ALLEGRO_FILE *fp, int flags) |
| 323 |
{ |
| 324 |
AllegroWindowsStream *s = new AllegroWindowsStream(fp); |
| 325 |
if (!s) { |
| 326 |
ALLEGRO_ERROR("Unable to create AllegroWindowsStream.\n"); |
| 327 |
return NULL; |
| 328 |
} |
| 329 |
|
| 330 |
ALLEGRO_BITMAP *a_bmp = NULL; |
| 331 |
Gdiplus::Bitmap *gdi_bmp = Gdiplus::Bitmap::FromStream(s, false); |
| 332 |
|
| 333 |
if (gdi_bmp) { |
| 334 |
const uint32_t w = gdi_bmp->GetWidth(); |
| 335 |
const uint32_t h = gdi_bmp->GetHeight(); |
| 336 |
const PixelFormat pf = gdi_bmp->GetPixelFormat(); |
| 337 |
bool premul = !(flags & ALLEGRO_NO_PREMULTIPLIED_ALPHA); |
| 338 |
bool keep_index = (flags & ALLEGRO_KEEP_INDEX); |
| 339 |
|
| 340 |
a_bmp = al_create_bitmap(w, h); |
| 341 |
if (a_bmp) { |
| 342 |
if (pf == PixelFormat8bppIndexed && keep_index) { |
| 343 |
load_indexed_data(gdi_bmp, a_bmp, w, h); |
| 344 |
} |
| 345 |
else { |
| 346 |
load_non_indexed_data(gdi_bmp, a_bmp, w, h, premul); |
| 347 |
} |
| 348 |
} |
| 349 |
delete gdi_bmp; |
| 350 |
} |
| 351 |
|
| 352 |
s->Release(); |
| 353 |
|
| 354 |
return a_bmp; |
| 355 |
} |
| 356 |
|
| 357 |
ALLEGRO_BITMAP *_al_load_gdiplus_bitmap(const char *filename, int flags) |
| 358 |
{ |
| 359 |
ALLEGRO_BITMAP *bmp = NULL; |
| 360 |
ALLEGRO_FILE *fp; |
| 361 |
|
| 362 |
fp = al_fopen(filename, "rb"); |
| 363 |
if (fp) { |
| 364 |
bmp = _al_load_gdiplus_bitmap_f(fp, flags); |
| 365 |
al_fclose(fp); |
| 366 |
} |
| 367 |
|
| 368 |
return bmp; |
| 369 |
} |
| 370 |
|
| 371 |
bool _al_save_gdiplus_bitmap_f(ALLEGRO_FILE *fp, const char *ident, |
| 372 |
ALLEGRO_BITMAP *a_bmp) |
| 373 |
{ |
| 374 |
CLSID encoder; |
| 375 |
int encoder_status = -1; |
| 376 |
|
| 377 |
if (!_al_stricmp(ident, ".bmp")) { |
| 378 |
encoder_status = GetEncoderClsid(L"image/bmp", &encoder); |
| 379 |
} |
| 380 |
else if (!_al_stricmp(ident, ".jpg") || !_al_stricmp(ident, ".jpeg")) { |
| 381 |
encoder_status = GetEncoderClsid(L"image/jpeg", &encoder); |
| 382 |
} |
| 383 |
else if (!_al_stricmp(ident, ".gif")) { |
| 384 |
encoder_status = GetEncoderClsid(L"image/gif", &encoder); |
| 385 |
} |
| 386 |
else if (!_al_stricmp(ident, ".tif") || !_al_stricmp(ident, ".tiff")) { |
| 387 |
encoder_status = GetEncoderClsid(L"image/tiff", &encoder); |
| 388 |
} |
| 389 |
else if (!_al_stricmp(ident, ".png")) { |
| 390 |
encoder_status = GetEncoderClsid(L"image/png", &encoder); |
| 391 |
} |
| 392 |
|
| 393 |
if (encoder_status == -1) { |
| 394 |
ALLEGRO_ERROR("Invalid encoder status.\n"); |
| 395 |
return false; |
| 396 |
} |
| 397 |
|
| 398 |
AllegroWindowsStream *s = new AllegroWindowsStream(fp); |
| 399 |
if (!s) { |
| 400 |
ALLEGRO_ERROR("Couldn't create AllegroWindowsStream.\n"); |
| 401 |
return false; |
| 402 |
} |
| 403 |
|
| 404 |
const int w = al_get_bitmap_width(a_bmp), h = al_get_bitmap_height(a_bmp); |
| 405 |
bool ret = false; |
| 406 |
|
| 407 |
Gdiplus::Bitmap *gdi_bmp = new Gdiplus::Bitmap(w, h, PixelFormat32bppARGB); |
| 408 |
|
| 409 |
if (gdi_bmp) { |
| 410 |
Gdiplus::Rect rect(0, 0, w, h); |
| 411 |
Gdiplus::BitmapData *gdi_lock = new Gdiplus::BitmapData(); |
| 412 |
|
| 413 |
if (!gdi_bmp->LockBits(&rect, Gdiplus::ImageLockModeWrite, |
| 414 |
PixelFormat32bppARGB, gdi_lock)) { |
| 415 |
|
| 416 |
ALLEGRO_LOCKED_REGION *a_lock = al_lock_bitmap( |
| 417 |
a_bmp, ALLEGRO_PIXEL_FORMAT_ARGB_8888, ALLEGRO_LOCK_READONLY); |
| 418 |
|
| 419 |
if (a_lock) { |
| 420 |
unsigned char *in = (unsigned char *)a_lock->data; |
| 421 |
unsigned char *out = (unsigned char *)gdi_lock->Scan0; |
| 422 |
|
| 423 |
if (gdi_lock->Stride == a_lock->pitch) { |
| 424 |
memcpy(out, in, h * gdi_lock->Stride); |
| 425 |
} |
| 426 |
else { |
| 427 |
uint32_t rows = h; |
| 428 |
while (rows--) { |
| 429 |
memcpy(out, in, w * 4); |
| 430 |
in += a_lock->pitch; |
| 431 |
out += gdi_lock->Stride; |
| 432 |
} |
| 433 |
} |
| 434 |
|
| 435 |
al_unlock_bitmap(a_bmp); |
| 436 |
} |
| 437 |
gdi_bmp->UnlockBits(gdi_lock); |
| 438 |
} |
| 439 |
|
| 440 |
ret = (gdi_bmp->Save(s, &encoder, NULL) == 0); |
| 441 |
|
| 442 |
delete gdi_lock; |
| 443 |
delete gdi_bmp; |
| 444 |
} |
| 445 |
|
| 446 |
s->Release(); |
| 447 |
|
| 448 |
return ret; |
| 449 |
} |
| 450 |
|
| 451 |
bool _al_save_gdiplus_bitmap(const char *filename, ALLEGRO_BITMAP *bmp) |
| 452 |
{ |
| 453 |
ALLEGRO_FILE *fp; |
| 454 |
bool ret = false; |
| 455 |
|
| 456 |
fp = al_fopen(filename, "wb"); |
| 457 |
if (fp) { |
| 458 |
ALLEGRO_PATH *path = al_create_path(filename); |
| 459 |
if (path) { |
| 460 |
ret = _al_save_gdiplus_bitmap_f(fp, al_get_path_extension(path), bmp); |
| 461 |
al_destroy_path(path); |
| 462 |
} |
| 463 |
al_fclose(fp); |
| 464 |
} |
| 465 |
|
| 466 |
return ret; |
| 467 |
} |
| 468 |
|
| 469 |
bool _al_save_gdiplus_png_f(ALLEGRO_FILE *f, ALLEGRO_BITMAP *bmp) |
| 470 |
{ |
| 471 |
return _al_save_gdiplus_bitmap_f(f, ".png", bmp); |
| 472 |
} |
| 473 |
|
| 474 |
bool _al_save_gdiplus_jpg_f(ALLEGRO_FILE *f, ALLEGRO_BITMAP *bmp) |
| 475 |
{ |
| 476 |
return _al_save_gdiplus_bitmap_f(f, ".jpg", bmp); |
| 477 |
} |
| 478 |
|
| 479 |
bool _al_save_gdiplus_tif_f(ALLEGRO_FILE *f, ALLEGRO_BITMAP *bmp) |
| 480 |
{ |
| 481 |
return _al_save_gdiplus_bitmap_f(f, ".tif", bmp); |
| 482 |
} |
| 483 |
|
| 484 |
bool _al_save_gdiplus_gif_f(ALLEGRO_FILE *f, ALLEGRO_BITMAP *bmp) |
| 485 |
{ |
| 486 |
return _al_save_gdiplus_bitmap_f(f, ".gif", bmp); |
| 487 |
} |
| 488 |
|
| 489 |
bool _al_init_gdiplus() |
| 490 |
{ |
| 491 |
if (!gdiplus_inited) { |
| 492 |
Gdiplus::GdiplusStartupInput gdiplusStartupInput; |
| 493 |
if (Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL) |
| 494 |
== Gdiplus::Ok) { |
| 495 |
gdiplus_inited = TRUE; |
| 496 |
_al_add_exit_func(_al_shutdown_gdiplus, "_al_shutdown_gdiplus"); |
| 497 |
} |
| 498 |
} |
| 499 |
|
| 500 |
return gdiplus_inited; |
| 501 |
} |
| 502 |
|
| 503 |
void _al_shutdown_gdiplus() |
| 504 |
{ |
| 505 |
if (gdiplus_inited) { |
| 506 |
Gdiplus::GdiplusShutdown(gdiplusToken); |
| 507 |
gdiplus_inited = false; |
| 508 |
} |
| 509 |
} |
| 510 |
|
| 511 |
/* vim: set sts=3 sw=3 et: */ |