~haggai-eran/nux/rtl-logical-packing

« back to all changes in this revision

Viewing changes to NuxImage/GdiImageLoader.cpp

  • Committer: Haggai Eran
  • Date: 2012-04-17 15:31:00 UTC
  • mfrom: (528.2.84 nux)
  • Revision ID: haggai.eran@gmail.com-20120417153100-5xp8l3llva9q8s49
Merge version 2.10.0 from trunk.

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, as
6
 
 * published by the  Free Software Foundation; either version 2.1 or 3.0
7
 
 * of the License.
8
 
 *
9
 
 * This program is distributed in the hope that it will be useful, but
10
 
 * WITHOUT ANY WARRANTY; without even the implied warranties of
11
 
 * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
12
 
 * PURPOSE.  See the applicable version of the GNU Lesser General Public
13
 
 * License for more details.
14
 
 *
15
 
 * You should have received a copy of both the GNU Lesser General Public
16
 
 * License along with this program. If not, see <http://www.gnu.org/licenses/>
17
 
 *
18
 
 * Authored by: Jay Taoko <jaytaoko@inalogic.com>
19
 
 *
20
 
 */
21
 
 
22
 
 
23
 
#include "NuxCore/NuxCore.h"
24
 
#include "GdiImageLoader.h"
25
 
 
26
 
namespace nux
27
 
{
28
 
  NBitmapData* GdiLoadImageFile(const TCHAR* filename)
29
 
  {
30
 
    ULONG_PTR token;
31
 
    Gdiplus::GdiplusStartupInput input;
32
 
    Gdiplus::GdiplusStartup(&token, &input, NULL);
33
 
 
34
 
    Gdiplus::Bitmap* bitmap = new Gdiplus::Bitmap(TCHAR_TO_UNICHAR(filename));
35
 
    if (bitmap == 0 || (bitmap->GetType() == Gdiplus::ImageTypeUnknown) || (bitmap->GetWidth() == 0) || (bitmap->GetHeight() == 0))
36
 
    {
37
 
      Gdiplus::GdiplusShutdown(token);
38
 
      return 0;
39
 
    }
40
 
 
41
 
    Gdiplus::BitmapData* bitmap_data = new Gdiplus::BitmapData;
42
 
    if (bitmap_data == 0)
43
 
    {
44
 
      delete bitmap;
45
 
      Gdiplus::GdiplusShutdown(token);
46
 
      return 0;
47
 
    }
48
 
 
49
 
    Gdiplus::PixelFormat pixel_format = bitmap->GetPixelFormat();
50
 
    int channels = 4;
51
 
    BitmapFormat bitmap_format = BITFMT_UNKNOWN;
52
 
 
53
 
    if (pixel_format == PixelFormat24bppRGB)
54
 
    {
55
 
      bitmap_format = BITFMT_B8G8R8;
56
 
      channels = 3;
57
 
    }
58
 
    else if ((pixel_format == PixelFormat32bppARGB) ||
59
 
      (pixel_format == PixelFormat32bppPARGB) ||
60
 
      (pixel_format == PixelFormat32bppRGB))
61
 
    {
62
 
      bitmap_format = BITFMT_B8G8R8A8;
63
 
      channels = 4;
64
 
    }
65
 
    else if ((pixel_format == PixelFormat16bppARGB1555) ||
66
 
      (pixel_format == PixelFormat16bppGrayScale) ||
67
 
      (pixel_format == PixelFormat16bppRGB555) ||
68
 
      (pixel_format == PixelFormat16bppRGB565))
69
 
    {
70
 
      pixel_format = PixelFormat24bppRGB;   // request conversion to RGB
71
 
      bitmap_format = BITFMT_B8G8R8;
72
 
      channels = 3;
73
 
    }
74
 
    else if ((pixel_format == PixelFormat1bppIndexed) ||
75
 
      (pixel_format == PixelFormat4bppIndexed) ||
76
 
      (pixel_format == PixelFormat8bppIndexed))
77
 
    {
78
 
      pixel_format = PixelFormat32bppARGB;  // request conversion to RGBA
79
 
      bitmap_format = BITFMT_B8G8R8A8;
80
 
      channels = 4;
81
 
    }
82
 
    else if (pixel_format == PixelFormat48bppRGB)
83
 
    {
84
 
      pixel_format = PixelFormat24bppRGB;  // request conversion to RGBA
85
 
      bitmap_format = BITFMT_B8G8R8;
86
 
      channels = 3;
87
 
    }
88
 
    else if ((pixel_format == PixelFormat64bppARGB) ||
89
 
      (pixel_format == PixelFormat64bppPARGB))
90
 
    {
91
 
      pixel_format = PixelFormat32bppARGB;  // request conversion to RGBA
92
 
      bitmap_format = BITFMT_B8G8R8A8;
93
 
      channels = 4;
94
 
    }
95
 
 
96
 
    Gdiplus::Rect rect(0, 0, bitmap->GetWidth(), bitmap->GetHeight());
97
 
 
98
 
    Gdiplus::Status status = bitmap->LockBits(&rect, Gdiplus::ImageLockModeRead, pixel_format, bitmap_data);
99
 
  
100
 
    if (status != Gdiplus::Ok)
101
 
    {
102
 
      delete bitmap_data;
103
 
      delete bitmap;
104
 
      Gdiplus::GdiplusShutdown(token);
105
 
      return 0;
106
 
    }
107
 
    
108
 
    unsigned int width = bitmap->GetWidth();
109
 
    unsigned int height = bitmap->GetHeight();
110
 
 
111
 
    NTextureData *Texture = NULL;
112
 
    Texture = new NTextureData(bitmap_format, width, height, 1);
113
 
    
114
 
    if (Texture == 0)
115
 
    {
116
 
      bitmap->UnlockBits(bitmap_data);
117
 
      delete bitmap_data;
118
 
      delete bitmap;
119
 
      Gdiplus::GdiplusShutdown(token);
120
 
      return 0;
121
 
    }
122
 
 
123
 
    ImageSurface &image_surface = Texture->GetSurface(0);
124
 
    t_u8* dest = image_surface.GetPtrRawData();
125
 
    int dest_pitch = image_surface.GetPitch();
126
 
 
127
 
    t_u8* dst = dest;
128
 
    unsigned char *src = (unsigned char*) bitmap_data->Scan0;
129
 
    unsigned int src_pitch = bitmap_data->Stride;
130
 
 
131
 
    for (unsigned int i = 0; i < height; i++)
132
 
    {
133
 
      Memcpy(dst, src + i*src_pitch, width*channels);
134
 
      dst += dest_pitch;
135
 
    }
136
 
 
137
 
    bitmap->UnlockBits(bitmap_data);
138
 
    delete bitmap_data;
139
 
    delete bitmap;
140
 
 
141
 
    Gdiplus::GdiplusShutdown(token);
142
 
 
143
 
    return Texture;
144
 
  }
145
 
}