File: gdiplus.cpp

package info (click to toggle)
allegro5 2%3A5.0.10-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 14,856 kB
  • ctags: 15,948
  • sloc: ansic: 87,540; cpp: 9,693; objc: 3,491; python: 2,057; sh: 829; makefile: 93; perl: 37; pascal: 24
file content (463 lines) | stat: -rw-r--r-- 11,666 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
#include <windows.h>
#include <objidl.h>

#include "allegro5/allegro.h"
#include "allegro5/allegro_image.h"
#include "allegro5/internal/aintern_convert.h"
#include "allegro5/internal/aintern_exitfunc.h"
#include "allegro5/internal/aintern_image.h"

#include "iio.h"

#ifdef ALLEGRO_CFG_IIO_HAVE_GDIPLUS_LOWERCASE_H
   #include <gdiplus.h>
#else
   #include <GdiPlus.h>
#endif

/* Needed with the MinGW w32api-3.15 headers. */
using namespace Gdiplus;

#ifndef _MSC_VER
#define __uuidof(x) (IID_ ## x)
#endif

static bool gdiplus_inited = false;
static ULONG_PTR gdiplusToken = 0;

/* Source:
 *   http://msdn.microsoft.com/en-us/library/ms533843%28VS.85%29.aspx
 */
static int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
{
   UINT num = 0;          // number of image encoders
   UINT size = 0;         // size of the image encoder array in bytes

   Gdiplus::ImageCodecInfo* pImageCodecInfo = NULL;

   Gdiplus::GetImageEncodersSize(&num, &size);
   if (size == 0) {
      return -1;  
   }

   pImageCodecInfo = (Gdiplus::ImageCodecInfo*)(al_malloc(size));
   if (pImageCodecInfo == NULL) {
      return -1;  
   }

   GetImageEncoders(num, size, pImageCodecInfo);

   for (UINT j = 0; j < num; ++j) {
      if(wcscmp(pImageCodecInfo[j].MimeType, format) == 0) {
         *pClsid = pImageCodecInfo[j].Clsid;
         al_free(pImageCodecInfo);
         return j;
      }    
   }

   al_free(pImageCodecInfo);
   return -1;  
}

/* A wrapper around an already opened ALLEGRO_FILE* pointer
 */
class AllegroWindowsStream : public IStream 
{
   long refCount;
   ALLEGRO_FILE *fp;

public:
   /* Create a stream from an open file handle */
   AllegroWindowsStream(ALLEGRO_FILE *fp) :
      refCount(1),
      fp(fp)
   {
      this->fp = fp;
   }

   virtual ~AllegroWindowsStream()
   {
   }

   /* IUnknown */
   virtual ULONG STDMETHODCALLTYPE AddRef(void)
   {
      return (ULONG) InterlockedIncrement(&refCount);  
   }

   virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void **ppvObject)
   {
      if (iid == __uuidof(IUnknown) || iid == __uuidof(ISequentialStream)
         || iid == __uuidof(IStream)) {
         *ppvObject = static_cast<IStream*>(this);
         AddRef();
         return S_OK;
      }
      else {
         return E_NOINTERFACE;
      }
   }

   virtual ULONG STDMETHODCALLTYPE Release(void)
   {
      ULONG ret = InterlockedDecrement(&refCount);
      if (ret == 0) {
         delete this; 
         return 0; 
      }
      return ret; 
   }

   /* ISequentialStream */
   virtual HRESULT STDMETHODCALLTYPE Read(void *pv, ULONG cb, ULONG *pcbRead)
   {
      size_t read = al_fread(fp, pv, cb);
      if (pcbRead) {
         *pcbRead = read;
      }
      return read == cb ? S_OK : S_FALSE;
   }

   virtual HRESULT STDMETHODCALLTYPE Write(const void *pv, ULONG cb,
      ULONG *pcbWritten)
   {
      size_t written = al_fwrite(fp, pv, cb);
      if (pcbWritten) {
         *pcbWritten = written;
      }
      return written == cb ? S_OK : STG_E_CANTSAVE; 
   }
    
   /* IStream */
   virtual HRESULT STDMETHODCALLTYPE Seek(LARGE_INTEGER dlibMove, DWORD dwOrigin,
      ULARGE_INTEGER *plibNewPosition)
   {
      ALLEGRO_SEEK o;
      if (dwOrigin == STREAM_SEEK_SET) {
         o = ALLEGRO_SEEK_SET;
      }
      else if (dwOrigin == STREAM_SEEK_CUR) {
         o = ALLEGRO_SEEK_CUR; 
      }
      else {
         o = ALLEGRO_SEEK_END;
      }
      
      bool ret = al_fseek(fp, dlibMove.QuadPart, o);

      if (plibNewPosition) {
         int64_t pos = al_ftell(fp);
         if (pos == -1) {
            return STG_E_INVALIDFUNCTION;
         }

         plibNewPosition->QuadPart = pos;
      }

      return ret ? S_OK : STG_E_INVALIDFUNCTION;
   }

   /* The GDI+ image I/O methods need to know the file size */
   virtual HRESULT STDMETHODCALLTYPE Stat(STATSTG *pstatstg, DWORD grfStatFlag)
   {		
      (void) grfStatFlag;
      memset(pstatstg, 0, sizeof(*pstatstg));
      pstatstg->type = STGTY_STREAM;
      pstatstg->cbSize.QuadPart = al_fsize(fp); 
      return S_OK;
   }

   /* The following IStream methods aren't needed */
   virtual HRESULT STDMETHODCALLTYPE Clone(IStream **ppstm)
   {
      (void) ppstm;
      return E_NOTIMPL;
   }

   virtual HRESULT STDMETHODCALLTYPE Commit(DWORD grfCommitFlags)
   {
      (void) grfCommitFlags;
      return E_NOTIMPL;
   }

   virtual HRESULT STDMETHODCALLTYPE CopyTo (IStream *pstm, ULARGE_INTEGER cb,
      ULARGE_INTEGER *pcbRead, ULARGE_INTEGER *pcbWritten)
   {
      (void) pstm;
      (void) cb;
      (void) pcbRead;
      (void) pcbWritten;
      return E_NOTIMPL;
   }

   virtual HRESULT STDMETHODCALLTYPE LockRegion(ULARGE_INTEGER libOffset,
      ULARGE_INTEGER cb, DWORD dwLockType)
   {
      (void) libOffset;
      (void) cb;
      (void) dwLockType;
      return E_NOTIMPL;
   }
    
   virtual HRESULT STDMETHODCALLTYPE Revert()
   {
      return E_NOTIMPL;
   }

   virtual HRESULT STDMETHODCALLTYPE SetSize(ULARGE_INTEGER libNewSize)
   { 
      (void) libNewSize;
      return E_NOTIMPL;
   }
    
   virtual HRESULT STDMETHODCALLTYPE UnlockRegion(ULARGE_INTEGER libOffset,
      ULARGE_INTEGER cb, DWORD dwLockType)
   {
      (void) libOffset;
      (void) cb;
      (void) dwLockType;
      return E_NOTIMPL;
   }
};

static void load_non_indexed_data(Gdiplus::Bitmap *gdi_bmp,
   ALLEGRO_BITMAP *a_bmp, uint32_t w, uint32_t h, bool premul)
{
   Gdiplus::BitmapData *gdi_lock = new Gdiplus::BitmapData();
   Gdiplus::Rect rect(0, 0, w, h);

   if (!gdi_bmp->LockBits(&rect, Gdiplus::ImageLockModeRead,
         PixelFormat32bppARGB, gdi_lock))
   {
      ALLEGRO_LOCKED_REGION *a_lock = al_lock_bitmap(a_bmp,
         ALLEGRO_PIXEL_FORMAT_ARGB_8888, ALLEGRO_LOCK_WRITEONLY);

      if (a_lock) {
         unsigned char *in = (unsigned char *)gdi_lock->Scan0;
         unsigned char *out = (unsigned char *)a_lock->data;

         if (premul) {
            int in_inc = gdi_lock->Stride - (w*4);
            int out_inc = a_lock->pitch - (w*4);
            for (unsigned int y = 0; y < h; y++) {
               for (unsigned int x = 0; x < w; x++) {
                  unsigned char r, g, b, a;
                  b = *in++;
                  g = *in++;
                  r = *in++;
                  a = *in++;
                  b = b * a / 255;
                  g = g * a / 255;
                  r = r * a / 255;
                  *out++ = b;
                  *out++ = g;
                  *out++ = r;
                  *out++ = a;
               }
               in += in_inc;
               out += out_inc;
            }
         }
         else {
            if (gdi_lock->Stride == a_lock->pitch) {
               memcpy(out, in, h * gdi_lock->Stride);
            }
            else {
               uint32_t rows = h;
               while (rows--) {
                  memcpy(out, in, w * 4);
                  in += gdi_lock->Stride;
                  out += a_lock->pitch;
               }
            }
         }
         al_unlock_bitmap(a_bmp);
      }

      gdi_bmp->UnlockBits(gdi_lock);
   }

   delete gdi_lock;
}

ALLEGRO_BITMAP *_al_load_gdiplus_bitmap_f(ALLEGRO_FILE *fp)
{
   AllegroWindowsStream *s = new AllegroWindowsStream(fp);
   if (!s) {
      return NULL;
   }

   ALLEGRO_BITMAP *a_bmp = NULL;
   Gdiplus::Bitmap *gdi_bmp = Gdiplus::Bitmap::FromStream(s, false);

   if (gdi_bmp) {
      const uint32_t w = gdi_bmp->GetWidth();
      const uint32_t h = gdi_bmp->GetHeight();
      bool premul =
         !(al_get_new_bitmap_flags() & ALLEGRO_NO_PREMULTIPLIED_ALPHA);

      a_bmp = al_create_bitmap(w, h);
      if (a_bmp) {
         load_non_indexed_data(gdi_bmp, a_bmp, w, h, premul);
      }
      delete gdi_bmp;
   }

   s->Release();

   return a_bmp;
}

ALLEGRO_BITMAP *_al_load_gdiplus_bitmap(const char *filename)
{
   ALLEGRO_BITMAP *bmp = NULL;
   ALLEGRO_FILE *fp;
	
   fp = al_fopen(filename, "rb");		
   if (fp) {
      bmp = _al_load_gdiplus_bitmap_f(fp);
      al_fclose(fp);
   }

   return bmp;
}

bool _al_save_gdiplus_bitmap_f(ALLEGRO_FILE *fp, const char *ident,
   ALLEGRO_BITMAP *a_bmp)
{
   CLSID encoder;
   int encoder_status = -1;

   if (!strcmp(ident, ".bmp")) {
      encoder_status = GetEncoderClsid(L"image/bmp", &encoder);
   }
   else if (!strcmp(ident, ".jpg") || !strcmp(ident, ".jpeg")) {
      encoder_status = GetEncoderClsid(L"image/jpeg", &encoder);
   }
   else if (!strcmp(ident, ".gif")) {
      encoder_status = GetEncoderClsid(L"image/gif", &encoder);
   }
   else if (!strcmp(ident, ".tif") || !strcmp(ident, ".tiff")) {
      encoder_status = GetEncoderClsid(L"image/tiff", &encoder);
   }
   else if (!strcmp(ident, ".png")) {
      encoder_status = GetEncoderClsid(L"image/png", &encoder);
   }

   if (encoder_status == -1) {
      return false;    
   }

   AllegroWindowsStream *s = new AllegroWindowsStream(fp);
   if (!s) {
      return false;
   }

   const int w = al_get_bitmap_width(a_bmp), h = al_get_bitmap_height(a_bmp);
   bool ret = false;

   Gdiplus::Bitmap *gdi_bmp = new Gdiplus::Bitmap(w, h, PixelFormat32bppARGB);

   if (gdi_bmp) {
      Gdiplus::Rect rect(0, 0, w, h);
      Gdiplus::BitmapData *gdi_lock = new Gdiplus::BitmapData();

      if (!gdi_bmp->LockBits(&rect, Gdiplus::ImageLockModeWrite,
            PixelFormat32bppARGB, gdi_lock)) {
            	            	
         ALLEGRO_LOCKED_REGION *a_lock = al_lock_bitmap(
            a_bmp, ALLEGRO_PIXEL_FORMAT_ARGB_8888, ALLEGRO_LOCK_READONLY);

         if (a_lock) {
            unsigned char *in = (unsigned char *)a_lock->data;
            unsigned char *out = (unsigned char *)gdi_lock->Scan0;
           
            if (gdi_lock->Stride == a_lock->pitch) {
               memcpy(out, in, h * gdi_lock->Stride);
            }
            else {
               uint32_t rows = h;
               while (rows--) {
                  memcpy(out, in, w * 4);
                  in += a_lock->pitch;
                  out += gdi_lock->Stride;				
               }
            }

            al_unlock_bitmap(a_bmp);
         }
         gdi_bmp->UnlockBits(gdi_lock);
      }
				
      ret = (gdi_bmp->Save(s, &encoder, NULL) == 0);

      delete gdi_lock;		
      delete gdi_bmp;
   }

   s->Release();

   return ret;
}

bool _al_save_gdiplus_bitmap(const char *filename, ALLEGRO_BITMAP *bmp)
{
   ALLEGRO_FILE *fp;
   bool ret = false;

   fp = al_fopen(filename, "wb");
   if (fp) {
      ALLEGRO_PATH *path = al_create_path(filename);
      if (path) {
         ret = _al_save_gdiplus_bitmap_f(fp, al_get_path_extension(path), bmp);
         al_destroy_path(path);
      }
      al_fclose(fp);
   }

   return ret;
}	

bool _al_save_gdiplus_png_f(ALLEGRO_FILE *f, ALLEGRO_BITMAP *bmp)
{
   return _al_save_gdiplus_bitmap_f(f, ".png", bmp);
}

bool _al_save_gdiplus_jpg_f(ALLEGRO_FILE *f, ALLEGRO_BITMAP *bmp)
{
   return _al_save_gdiplus_bitmap_f(f, ".jpg", bmp);
}

bool _al_save_gdiplus_tif_f(ALLEGRO_FILE *f, ALLEGRO_BITMAP *bmp)
{
   return _al_save_gdiplus_bitmap_f(f, ".tif", bmp);
}

bool _al_save_gdiplus_gif_f(ALLEGRO_FILE *f, ALLEGRO_BITMAP *bmp)
{
   return _al_save_gdiplus_bitmap_f(f, ".gif", bmp);
}

bool _al_init_gdiplus()
{
   if (!gdiplus_inited) {
      Gdiplus::GdiplusStartupInput gdiplusStartupInput;
      if (Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL)
            == Gdiplus::Ok) {
         gdiplus_inited = TRUE;
         _al_add_exit_func(_al_shutdown_gdiplus, "_al_shutdown_gdiplus");
      }
   }

   return gdiplus_inited;
}

void _al_shutdown_gdiplus()
{
   if (gdiplus_inited) {
      Gdiplus::GdiplusShutdown(gdiplusToken);
      gdiplus_inited = false;
   }
}

/* vim: set sts=3 sw=3 et: */