~ubuntu-branches/ubuntu/oneiric/nux/oneiric

« back to all changes in this revision

Viewing changes to NuxImage/NPng.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Didier Roche
  • Date: 2010-11-18 19:17:32 UTC
  • Revision ID: james.westby@ubuntu.com-20101118191732-rn35790vekj6o4my
Tags: upstream-0.9.4
ImportĀ upstreamĀ versionĀ 0.9.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2010 Inalogic Inc.
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify it
 
5
 * under the terms of the GNU Lesser General Public License version 3, as
 
6
 * published by the  Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful, but
 
9
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 
10
 * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
 
11
 * PURPOSE.  See the applicable version of the GNU Lesser General Public
 
12
 * License for more details.
 
13
 *
 
14
 * You should have received a copy of both the GNU Lesser General Public
 
15
 * License version 3 along with this program.  If not, see
 
16
 * <http://www.gnu.org/licenses/>
 
17
 *
 
18
 * Authored by: Jay Taoko <jay.taoko_AT_gmail_DOT_com>
 
19
 *
 
20
 */
 
21
 
 
22
 
 
23
#include "NuxCore/NuxCore.h"
 
24
 
 
25
#include "png.h"
 
26
#include "NPng.h"
 
27
#include "BitmapFormats.h"
 
28
 
 
29
 
 
30
#include <stdio.h>
 
31
#if defined(_WIN32)
 
32
#include <windows.h>
 
33
#endif
 
34
 
 
35
namespace nux
 
36
{
 
37
 
 
38
#define FAILMSGANDRETURN \
 
39
{\
 
40
    nuxAssertMsg(0, TEXT("[PNG Read Error] Cannot open %s for read."), FileNameAnsi); \
 
41
    return 0;\
 
42
}
 
43
#if defined(_WIN32)
 
44
 
 
45
#define PNG_FREERESOURCE if(hglobal) FreeResource(hglobal);
 
46
#define PNG_DECLARE_RESVAR \
 
47
    DWORD ressz;\
 
48
    HRSRC HRes  = NULL;\
 
49
    HGLOBAL hglobal = NULL;\
 
50
    void* resptr;
 
51
#define PNG_SETREADFN \
 
52
    if(!fp) png_set_read_fn(png_ptr, (void *)resptr, png_read_resource_fn);      else
 
53
#define PNG_READHEADER \
 
54
    if(!fp)\
 
55
    {\
 
56
    memcpy(sig, resptr, 8);\
 
57
    resptr = (void*)((char*)resptr + 8);\
 
58
    } else
 
59
 
 
60
#else
 
61
 
 
62
#define PNG_FREERESOURCE
 
63
#define PNG_DECLARE_RESVAR
 
64
#define PNG_SETREADFN
 
65
#define PNG_READHEADER
 
66
 
 
67
#endif
 
68
 
 
69
 
 
70
 
 
71
  namespace
 
72
  {
 
73
#if defined(_WIN32)
 
74
    struct png_resource_access
 
75
    {
 
76
      png_resource_access() : hModule (NULL), res_type_id (0) {}
 
77
      HMODULE hModule;
 
78
      std::string res_type_name;
 
79
      unsigned long res_type_id;
 
80
    };
 
81
    png_resource_access png_resource;
 
82
#endif
 
83
  }
 
84
 
 
85
  void png_read_resource_fn (png_structp png_ptr, png_bytep data, png_size_t leng);
 
86
 
 
87
#if defined(_WIN32)
 
88
  void set_png_module_handle (unsigned long hM)
 
89
  {
 
90
    png_resource.hModule = (HMODULE) hM;
 
91
  }
 
92
  void set_png_module_restypename (const char *restypename)
 
93
  {
 
94
    if (HIWORD (restypename) )
 
95
    {
 
96
      png_resource.res_type_name = restypename;
 
97
      png_resource.res_type_id = 0;
 
98
    }
 
99
    else
 
100
    {
 
101
      //resource.res_type_name.clear(); should exist (cf STL Doc)
 
102
      png_resource.res_type_id = (unsigned long) restypename;
 
103
    }
 
104
  }
 
105
#endif
 
106
 
 
107
 
 
108
#if defined(_WIN32)
 
109
  HRSRC get_resource (const char *filename)
 
110
  {
 
111
    HRSRC HRes;
 
112
#ifdef _UNICODE
 
113
    unsigned int count = strlen (filename);
 
114
    wchar_t *wfilename = new wchar_t[count];
 
115
    mbstowcs (wfilename, filename, count);
 
116
 
 
117
    count = png_resource.res_type_name.size();
 
118
    wchar_t *wres_type_name = new wchar_t[count];
 
119
    mbstowcs (wres_type_name, png_resource.res_type_name.c_str(), count);
 
120
 
 
121
    HRes = FindResource (png_resource.hModule, wfilename,
 
122
                         png_resource.res_type_id ? (LPCWSTR) png_resource.res_type_id : wres_type_name);
 
123
 
 
124
    delete [] wfilename;
 
125
    delete [] wres_type_name;
 
126
#else
 
127
    HRes = FindResource (png_resource.hModule, filename,
 
128
                         png_resource.res_type_id ? (LPCSTR) png_resource.res_type_id : png_resource.res_type_name.c_str() );
 
129
#endif
 
130
    return HRes;
 
131
  }
 
132
#endif
 
133
 
 
134
  NBitmapData *read_png_rgba (const TCHAR *filename)
 
135
  {
 
136
    //--------- Resource stuff
 
137
    PNG_DECLARE_RESVAR
 
138
    //---------
 
139
    FILE *fp = NULL;
 
140
    png_byte sig[8];
 
141
    int bit_depth, color_type;
 
142
    double              gamma;
 
143
    png_uint_32 channels, row_bytes;
 
144
    png_structp png_ptr = 0;
 
145
    png_infop info_ptr = 0;
 
146
 
 
147
    ANSICHAR *FileNameAnsi = (ANSICHAR *) TCHAR_TO_ANSI (filename);
 
148
 
 
149
    if (!FileNameAnsi)
 
150
    {
 
151
      nuxAssertMsg (0, TEXT ("[read_png_rgba] Incorrect file name: %s."), filename);
 
152
      return 0;
 
153
    }
 
154
 
 
155
 
 
156
#if defined(_WIN32)
 
157
    struct _stat file_info;
 
158
#else
 
159
    struct stat file_info;
 
160
#endif
 
161
 
 
162
 
 
163
    // System call: check if the file exist
 
164
#if defined(_WIN32)
 
165
 
 
166
    if (_stat (FileNameAnsi, &file_info) != 0)
 
167
#else
 
168
    if (stat (FileNameAnsi, &file_info) != 0)
 
169
#endif
 
170
    {
 
171
      nuxAssert (TEXT ("[read_png_rgba] File not found: %s.") );
 
172
      return 0;
 
173
    }
 
174
 
 
175
#if (defined NUX_VISUAL_STUDIO_2005) || (defined NUX_VISUAL_STUDIO_2008)
 
176
    fopen_s (&fp, FileNameAnsi, "rb");
 
177
#else
 
178
    fp = fopen (FileNameAnsi, "rb");
 
179
#endif
 
180
 
 
181
    if (!fp)
 
182
#if defined(_WIN32)
 
183
    {
 
184
      // Try resource access
 
185
      HRes = get_resource (FileNameAnsi);
 
186
 
 
187
      if (!HRes)
 
188
        FAILMSGANDRETURN
 
189
        hglobal =       LoadResource (png_resource.hModule, HRes);
 
190
 
 
191
      if (!hglobal)
 
192
        FAILMSGANDRETURN
 
193
        ressz = SizeofResource (png_resource.hModule, HRes);
 
194
 
 
195
      resptr =  (char *) LockResource (hglobal);
 
196
 
 
197
      if (!resptr)
 
198
        FAILMSGANDRETURN
 
199
      }
 
200
 
 
201
#else
 
202
      FAILMSGANDRETURN
 
203
#endif
 
204
 
 
205
    // first check the eight byte PNG signature
 
206
    t_size r = fread (sig, 1, 8, fp);
 
207
 
 
208
    if (r == 0)
 
209
    {
 
210
      nuxAssertMsg (0, TEXT ("[read_png_rgba] Png read error.") );
 
211
    }
 
212
 
 
213
    if (!png_check_sig (sig, 8) )
 
214
    {
 
215
      if (fp) fclose (fp);
 
216
 
 
217
      PNG_FREERESOURCE
 
218
      return 0;
 
219
    }
 
220
 
 
221
 
 
222
    // start back here!!!!
 
223
 
 
224
    // create the two png(-info) structures
 
225
 
 
226
    png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, 0, 0, 0);
 
227
 
 
228
    if (!png_ptr)
 
229
    {
 
230
      if (fp) fclose (fp);
 
231
 
 
232
      PNG_FREERESOURCE
 
233
      return 0;
 
234
    }
 
235
 
 
236
    info_ptr = png_create_info_struct (png_ptr);
 
237
 
 
238
    if (!info_ptr)
 
239
    {
 
240
      png_destroy_read_struct (&png_ptr, 0, 0);
 
241
 
 
242
      if (fp) fclose (fp);
 
243
 
 
244
      PNG_FREERESOURCE
 
245
    }
 
246
 
 
247
    // initialize the png structure
 
248
    PNG_SETREADFN png_init_io (png_ptr, fp);
 
249
 
 
250
    png_set_sig_bytes (png_ptr, 8);
 
251
 
 
252
    // read all PNG info up to image data
 
253
    png_read_info (png_ptr, info_ptr);
 
254
 
 
255
    // get width, height, bit-depth and color-type
 
256
    unsigned long width, height;
 
257
    png_get_IHDR (png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, 0, 0, 0);
 
258
 
 
259
    // expand images of all color-type and bit-depth to 3x8 bit RGB images
 
260
    // let the library process things like alpha, transparency, background
 
261
 
 
262
    if (bit_depth == 16) png_set_strip_16 (png_ptr);
 
263
 
 
264
    if (color_type == PNG_COLOR_TYPE_PALETTE) png_set_expand (png_ptr);
 
265
 
 
266
    if (bit_depth < 8) png_set_expand (png_ptr);
 
267
 
 
268
    if (png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS) ) png_set_expand (png_ptr);
 
269
 
 
270
    if (color_type == PNG_COLOR_TYPE_GRAY ||
 
271
        color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
 
272
      png_set_gray_to_rgb (png_ptr);
 
273
 
 
274
    // if required set gamma conversion
 
275
    if (png_get_gAMA (png_ptr, info_ptr, &gamma) ) png_set_gamma (png_ptr, (double) 2.2, gamma);
 
276
 
 
277
    // after the transformations have been registered update info_ptr data
 
278
    png_read_update_info (png_ptr, info_ptr);
 
279
 
 
280
    // get again width, height and the new bit-depth and color-type
 
281
    png_get_IHDR (png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, 0, 0, 0);
 
282
 
 
283
 
 
284
    // row_bytes is the width x number of channels
 
285
    row_bytes = png_get_rowbytes (png_ptr, info_ptr);
 
286
    channels = png_get_channels (png_ptr, info_ptr);
 
287
 
 
288
    if ( channels == 3 )
 
289
    {
 
290
      //nuxAssertMsg(0, TEXT("[read_png_rgba] Unable to open image of type RGB %s using read_png_rgba()."), filename);
 
291
      //nuxAssertMsg(0, TEXT("[read_png_rgba] Try using read_png_rgb() instead!"));
 
292
      if (fp) fclose (fp);
 
293
 
 
294
      return 0;
 
295
    }
 
296
 
 
297
    // now we can allocate memory to store the image
 
298
    png_byte *img = new png_byte[row_bytes * height];
 
299
    // and allocate memory for an array of row-pointers
 
300
    png_byte **row = new png_byte * [height];
 
301
 
 
302
 
 
303
    // set the individual row-pointers to point at the correct offsets
 
304
    for (unsigned int i = 0; i < height; i++)
 
305
      row[i] = img + i * row_bytes;
 
306
 
 
307
    // now we can go ahead and just read the whole image
 
308
    png_read_image (png_ptr, row);
 
309
 
 
310
    // read the additional chunks in the PNG file (not really needed)
 
311
    png_read_end (png_ptr, NULL);
 
312
 
 
313
    //image = array2<vec4ub>(w, h);
 
314
    NTextureData *TextureObjectData = new NTextureData (BITFMT_R8G8B8A8, width, height, 1);
 
315
    {
 
316
      for (unsigned int i = 0; i < width; i++)
 
317
        for (unsigned int j = 0; j < height; j++)
 
318
        {
 
319
          BYTE *png_data_pointer = img + ( (height - j - 1) * row_bytes + i * 4);
 
320
          UINT value =
 
321
            (* (png_data_pointer + 3) << 24) |  // a
 
322
            (* (png_data_pointer + 2) << 16) |  // b
 
323
            (* (png_data_pointer + 1) << 8)  |  // g
 
324
            * (png_data_pointer + 0);           // r
 
325
 
 
326
          TextureObjectData->GetSurface (0).Write32b (i, j, value); // = vec4ub(img + ((h-j-1)*row_bytes + i * 4));
 
327
        }
 
328
    }
 
329
 
 
330
    delete [] row;
 
331
    delete [] img;
 
332
 
 
333
    png_destroy_read_struct (&png_ptr, &info_ptr, 0);
 
334
 
 
335
    if (TextureObjectData)
 
336
      TextureObjectData->GetSurface (0).FlipVertical();
 
337
 
 
338
    if (fp) fclose (fp);
 
339
 
 
340
    PNG_FREERESOURCE
 
341
 
 
342
    return TextureObjectData;
 
343
  }
 
344
 
 
345
  NBitmapData *read_png_rgb (const TCHAR *filename)
 
346
  {
 
347
    //--------- Resource stuff
 
348
    PNG_DECLARE_RESVAR
 
349
    //----------
 
350
    FILE *fp;
 
351
    png_byte sig[8];
 
352
    int bit_depth, color_type;
 
353
    double              gamma;
 
354
    png_uint_32 channels, row_bytes;
 
355
    png_structp png_ptr = 0;
 
356
    png_infop info_ptr = 0;
 
357
 
 
358
    // open the PNG input file
 
359
    ANSICHAR *FileNameAnsi = (ANSICHAR *) TCHAR_TO_ANSI (filename);
 
360
 
 
361
    if (!FileNameAnsi)
 
362
    {
 
363
      nuxAssertMsg (0, TEXT ("[read_png_rgb] Incorrect file name: %s."), filename);
 
364
      return 0;
 
365
    }
 
366
 
 
367
#if defined(_WIN32)
 
368
    struct _stat file_info;
 
369
#else
 
370
    struct stat file_info;
 
371
#endif
 
372
 
 
373
 
 
374
    // System call: check if the file exist
 
375
#if defined(_WIN32)
 
376
 
 
377
    if (_stat (FileNameAnsi, &file_info) != 0)
 
378
#else
 
379
    if (stat (FileNameAnsi, &file_info) != 0)
 
380
#endif
 
381
    {
 
382
      nuxAssert (TEXT ("[read_png_rgb] File not found: %s.") );
 
383
      return 0;
 
384
    }
 
385
 
 
386
#if (defined NUX_VISUAL_STUDIO_2005) || (defined NUX_VISUAL_STUDIO_2008)
 
387
    fopen_s (&fp, FileNameAnsi, "rb");
 
388
#else
 
389
    fp = fopen (FileNameAnsi, "rb");
 
390
#endif
 
391
 
 
392
    if (!fp)
 
393
#if defined(_WIN32)
 
394
    {
 
395
      // Try resource access
 
396
      HRes = get_resource (FileNameAnsi);
 
397
 
 
398
      if (!HRes)
 
399
        FAILMSGANDRETURN
 
400
        hglobal =       LoadResource (png_resource.hModule, HRes);
 
401
 
 
402
      if (!hglobal)
 
403
        FAILMSGANDRETURN
 
404
        ressz = SizeofResource (png_resource.hModule, HRes);
 
405
 
 
406
      resptr =  (char *) LockResource (hglobal);
 
407
 
 
408
      if (!resptr)
 
409
        FAILMSGANDRETURN
 
410
      }
 
411
 
 
412
#else
 
413
      FAILMSGANDRETURN
 
414
#endif
 
415
 
 
416
    // first check the eight byte PNG signature
 
417
    t_size r = fread (sig, 1, 8, fp);
 
418
 
 
419
    if (r == 0)
 
420
    {
 
421
      nuxAssertMsg (0, TEXT ("[read_png_rgba] Png read error.") );
 
422
    }
 
423
 
 
424
    if (!png_check_sig (sig, 8) )
 
425
    {
 
426
      if (fp) fclose (fp);
 
427
 
 
428
      PNG_FREERESOURCE;
 
429
      return 0;
 
430
    }
 
431
 
 
432
    // start back here!!!!
 
433
 
 
434
    // create the two png(-info) structures
 
435
 
 
436
    png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, 0, 0, 0);
 
437
 
 
438
    if (!png_ptr)
 
439
    {
 
440
      if (fp) fclose (fp);
 
441
 
 
442
      PNG_FREERESOURCE;
 
443
      return 0;
 
444
    }
 
445
 
 
446
    info_ptr = png_create_info_struct (png_ptr);
 
447
 
 
448
    if (!info_ptr)
 
449
    {
 
450
      png_destroy_read_struct (&png_ptr, 0, 0);
 
451
 
 
452
      if (fp) fclose (fp);
 
453
 
 
454
      PNG_FREERESOURCE
 
455
    }
 
456
 
 
457
    // initialize the png structure
 
458
    PNG_SETREADFN png_init_io (png_ptr, fp);
 
459
 
 
460
    png_set_sig_bytes (png_ptr, 8);
 
461
 
 
462
    // read all PNG info up to image data
 
463
    png_read_info (png_ptr, info_ptr);
 
464
 
 
465
    // get width, height, bit-depth and color-type
 
466
    unsigned long w, h;
 
467
    png_get_IHDR (png_ptr, info_ptr, &w, &h, &bit_depth, &color_type, 0, 0, 0);
 
468
 
 
469
    // expand images of all color-type and bit-depth to 3x8 bit RGB images
 
470
    // let the library process things like alpha, transparency, background
 
471
 
 
472
    if (bit_depth == 16) png_set_strip_16 (png_ptr);
 
473
 
 
474
    if (color_type == PNG_COLOR_TYPE_PALETTE) png_set_expand (png_ptr);
 
475
 
 
476
    if (bit_depth < 8) png_set_expand (png_ptr);
 
477
 
 
478
    if (png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS) ) png_set_expand (png_ptr);
 
479
 
 
480
    if (color_type == PNG_COLOR_TYPE_GRAY ||
 
481
        color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
 
482
      png_set_gray_to_rgb (png_ptr);
 
483
 
 
484
    if (color_type == PNG_COLOR_TYPE_RGB_ALPHA)
 
485
      png_set_strip_alpha (png_ptr);
 
486
 
 
487
    // if required set gamma conversion
 
488
    if (png_get_gAMA (png_ptr, info_ptr, &gamma) ) png_set_gamma (png_ptr, (double) 2.2, gamma);
 
489
 
 
490
    // after the transformations have been registered update info_ptr data
 
491
    png_read_update_info (png_ptr, info_ptr);
 
492
 
 
493
    // get again width, height and the new bit-depth and color-type
 
494
    png_get_IHDR (png_ptr, info_ptr, &w, &h, &bit_depth, &color_type, 0, 0, 0);
 
495
 
 
496
 
 
497
    // row_bytes is the width x number of channels
 
498
    row_bytes = png_get_rowbytes (png_ptr, info_ptr);
 
499
    channels = png_get_channels (png_ptr, info_ptr);
 
500
 
 
501
    if ( channels == 4 )
 
502
    {
 
503
      //nuxAssertMsg(0, TEXT("[read_png_rgba] Unable to open image of type RGBA %s using read_png_rgb()."), filename);
 
504
      //nuxAssertMsg(0, TEXT("[read_png_rgba] Try using read_png_rgba() instead!"));
 
505
      if (fp) fclose (fp);
 
506
 
 
507
      return 0;
 
508
    }
 
509
 
 
510
    // now we can allocate memory to store the image
 
511
 
 
512
    png_byte *img = new png_byte[row_bytes * h];
 
513
 
 
514
    // and allocate memory for an array of row-pointers
 
515
 
 
516
    png_byte **row = new png_byte * [h];
 
517
 
 
518
 
 
519
    // set the individual row-pointers to point at the correct offsets
 
520
 
 
521
    for (unsigned int i = 0; i < h; i++)
 
522
      row[i] = img + i * row_bytes;
 
523
 
 
524
    // now we can go ahead and just read the whole image
 
525
 
 
526
    png_read_image (png_ptr, row);
 
527
 
 
528
    // read the additional chunks in the PNG file (not really needed)
 
529
 
 
530
    png_read_end (png_ptr, NULL);
 
531
 
 
532
    //image = array2<vec3ub>(w, h);
 
533
    NTextureData *TextureObjectData = new NTextureData (BITFMT_R8G8B8, w, h, 1);
 
534
    {
 
535
      for (unsigned int i = 0; i < w; i++)
 
536
        for (unsigned int j = 0; j < h; j++)
 
537
        {
 
538
          BYTE *png_data_pointer = img + ( (h - j - 1) * row_bytes + i * 3);
 
539
 
 
540
          UINT value =
 
541
            (* (png_data_pointer + 2) << 16) |  //r
 
542
            (* (png_data_pointer + 1) << 8)  |  //g
 
543
            * (png_data_pointer + 0);           //b
 
544
 
 
545
          TextureObjectData->GetSurface (0).Write24b (i, j, value);  // vec3ub(img + ((h-j-1)*row_bytes + i * 3));
 
546
        }
 
547
    }
 
548
 
 
549
 
 
550
    delete [] row;
 
551
    delete [] img;
 
552
 
 
553
    png_destroy_read_struct (&png_ptr, &info_ptr, 0);
 
554
 
 
555
    if (TextureObjectData)
 
556
      TextureObjectData->GetSurface (0).FlipVertical();
 
557
 
 
558
    if (fp) fclose (fp);
 
559
 
 
560
    PNG_FREERESOURCE
 
561
    return TextureObjectData;
 
562
  }
 
563
 
 
564
 
 
565
////FIXME: LIBPNG expands to RGB and only R is fetched...
 
566
//bool read_png_grey(const char * filename, glh::array2<unsigned char> & image)
 
567
//{
 
568
//    //--------- Resource stuff
 
569
//    PNG_DECLARE_RESVAR
 
570
//        //---------
 
571
//        FILE * fp;
 
572
//    png_byte sig[8];
 
573
//    int bit_depth, color_type;
 
574
//    double              gamma;
 
575
//    png_uint_32 channels, row_bytes;
 
576
//    png_structp png_ptr = 0;
 
577
//    png_infop info_ptr = 0;
 
578
//
 
579
//
 
580
//    // This is the SDK default path...
 
581
//    if(path.path.size() < 1)
 
582
//    {
 
583
//        path.path.push_back(""); // added by Ashu, for case where fully qualified path is given
 
584
//        path.path.push_back(".");
 
585
//        path.path.push_back("../../../MEDIA/textures/1D");
 
586
//        path.path.push_back("../../../../MEDIA/textures/1D");
 
587
//        path.path.push_back("../../../../../../../MEDIA/textures/1D");
 
588
//        path.path.push_back("../../../MEDIA/textures/2D");
 
589
//        path.path.push_back("../../../../MEDIA/textures/2D");
 
590
//        path.path.push_back("../../../../../../../MEDIA/textures/2D");
 
591
//        path.path.push_back("../../../MEDIA/textures/rectangles");
 
592
//        path.path.push_back("../../../../MEDIA/textures/rectangles");
 
593
//        path.path.push_back("../../../../../../../MEDIA/textures/rectangles");
 
594
//        path.path.push_back("../../../MEDIA/textures/cubemaps");
 
595
//        path.path.push_back("../../../../MEDIA/textures/cubemaps");
 
596
//        path.path.push_back("../../../../../../../MEDIA/textures/cubemaps");
 
597
//        path.path.push_back("../../../MEDIA/textures/3D");
 
598
//        path.path.push_back("../../../../MEDIA/textures/3D");
 
599
//        path.path.push_back("../../../../../../../MEDIA/textures/3D");
 
600
//    }
 
601
//
 
602
//    // open the PNG input file
 
603
//    if (!filename) return false;
 
604
//
 
605
//    if (!(fp = path.fopen(filename)))
 
606
//#if defined(_WIN32)
 
607
//    {
 
608
//        // Try resource access
 
609
//        HRes = get_resource(filename);
 
610
//
 
611
//        if(!HRes)
 
612
//            FAILMSGANDRETURN
 
613
//            hglobal   =       LoadResource(png_resource.hModule, HRes);
 
614
//        if(!hglobal)
 
615
//            FAILMSGANDRETURN
 
616
//            ressz = SizeofResource(png_resource.hModule, HRes);
 
617
//        resptr =      (char*)LockResource(hglobal);
 
618
//        if(!resptr)
 
619
//            FAILMSGANDRETURN
 
620
//    }
 
621
//#else
 
622
//        FAILMSGANDRETURN
 
623
//#endif
 
624
//
 
625
//        // first check the eight byte PNG signature
 
626
//        PNG_READHEADER fread(sig, 1, 8, fp);
 
627
//    if (!png_check_sig(sig, 8)) {
 
628
//        if(fp) fclose(fp);
 
629
//        PNG_FREERESOURCE
 
630
//            return false;
 
631
//    }
 
632
//
 
633
//
 
634
//    // start back here!!!!
 
635
//
 
636
//    // create the two png(-info) structures
 
637
//
 
638
//    png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
 
639
//    if (!png_ptr) {
 
640
//        if(fp) fclose(fp);
 
641
//        PNG_FREERESOURCE
 
642
//            return false;
 
643
//    }
 
644
//
 
645
//    info_ptr = png_create_info_struct(png_ptr);
 
646
//    if (!info_ptr)
 
647
//    {
 
648
//        png_destroy_read_struct(&png_ptr, 0, 0);
 
649
//        if(fp) fclose(fp);
 
650
//        PNG_FREERESOURCE
 
651
//    }
 
652
//
 
653
//    // initialize the png structure
 
654
//    PNG_SETREADFN png_init_io(png_ptr, fp);
 
655
//
 
656
//    png_set_sig_bytes(png_ptr, 8);
 
657
//
 
658
//    // read all PNG info up to image data
 
659
//    png_read_info(png_ptr, info_ptr);
 
660
//
 
661
//    // get width, height, bit-depth and color-type
 
662
//    unsigned long w, h;
 
663
//    png_get_IHDR(png_ptr, info_ptr, &w, &h, &bit_depth, &color_type, 0, 0, 0);
 
664
//
 
665
//    // expand images of all color-type and bit-depth to 3x8 bit RGB images
 
666
//    // let the library process things like alpha, transparency, background
 
667
//
 
668
//    if (bit_depth == 16) png_set_strip_16(png_ptr);
 
669
//    if (color_type == PNG_COLOR_TYPE_PALETTE) png_set_expand(png_ptr);
 
670
//    if (bit_depth < 8) png_set_expand(png_ptr);
 
671
//    if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) png_set_expand(png_ptr);
 
672
//    if (color_type == PNG_COLOR_TYPE_GRAY ||
 
673
//        color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
 
674
//        png_set_gray_to_rgb(png_ptr);
 
675
//
 
676
//    // if required set gamma conversion
 
677
//    if (png_get_gAMA(png_ptr, info_ptr, &gamma)) png_set_gamma(png_ptr, (double) 2.2, gamma);
 
678
//
 
679
//    // after the transformations have been registered update info_ptr data
 
680
//    png_read_update_info(png_ptr, info_ptr);
 
681
//
 
682
//    // get again width, height and the new bit-depth and color-type
 
683
//    png_get_IHDR(png_ptr, info_ptr, &w, &h, &bit_depth, &color_type, 0, 0, 0);
 
684
//
 
685
//
 
686
//    // row_bytes is the width x number of channels
 
687
//    row_bytes = png_get_rowbytes(png_ptr, info_ptr);
 
688
//    channels = png_get_channels(png_ptr, info_ptr);
 
689
//
 
690
//    // now we can allocate memory to store the image
 
691
//
 
692
//    png_byte * img = new png_byte[row_bytes * h];
 
693
//
 
694
//    // and allocate memory for an array of row-pointers
 
695
//
 
696
//    png_byte ** row = new png_byte * [h];
 
697
//
 
698
//
 
699
//    // set the individual row-pointers to point at the correct offsets
 
700
//
 
701
//    for (unsigned int i = 0; i < h; i++)
 
702
//        row[i] = img + i * row_bytes;
 
703
//
 
704
//    // now we can go ahead and just read the whole image
 
705
//
 
706
//    png_read_image(png_ptr, row);
 
707
//
 
708
//    // read the additional chunks in the PNG file (not really needed)
 
709
//
 
710
//    png_read_end(png_ptr, NULL);
 
711
//
 
712
//    image = array2<unsigned char>(w, h);
 
713
//
 
714
//    {
 
715
//        for(unsigned int i=0; i < w; i++)
 
716
//            for(unsigned int j=0; j < h; j++)
 
717
//            { image(i,j) = *(img + ((h-j-1)*row_bytes + i * 3)); }
 
718
//    }
 
719
//
 
720
//    delete [] row;
 
721
//    delete [] img;
 
722
//
 
723
//    png_destroy_read_struct(&png_ptr, &info_ptr, 0);
 
724
//
 
725
//    if(fp) fclose (fp);
 
726
//    PNG_FREERESOURCE
 
727
//
 
728
//        return true;
 
729
//}
 
730
 
 
731
  void png_read_resource_fn (png_structp png_ptr, png_bytep data, png_size_t leng)
 
732
  {
 
733
    png_bytep src = (png_bytep) png_ptr->io_ptr;
 
734
 
 
735
    for (unsigned int i = 0; i < leng; i++)
 
736
      data[i] = *src++;
 
737
 
 
738
    png_ptr->io_ptr = (void *) src;
 
739
  }
 
740
 
 
741
}