2
* Copyright 2010 Inalogic® Inc.
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
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.
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/>
18
* Authored by: Jay Taoko <jaytaoko@inalogic.com>
23
#include "NuxCore/NuxCore.h"
24
#include "GdiImageLoader.h"
28
NBitmapData* GdiLoadImageFile(const TCHAR* filename)
31
Gdiplus::GdiplusStartupInput input;
32
Gdiplus::GdiplusStartup(&token, &input, NULL);
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))
37
Gdiplus::GdiplusShutdown(token);
41
Gdiplus::BitmapData* bitmap_data = new Gdiplus::BitmapData;
45
Gdiplus::GdiplusShutdown(token);
49
Gdiplus::PixelFormat pixel_format = bitmap->GetPixelFormat();
51
BitmapFormat bitmap_format = BITFMT_UNKNOWN;
53
if (pixel_format == PixelFormat24bppRGB)
55
bitmap_format = BITFMT_B8G8R8;
58
else if ((pixel_format == PixelFormat32bppARGB) ||
59
(pixel_format == PixelFormat32bppPARGB) ||
60
(pixel_format == PixelFormat32bppRGB))
62
bitmap_format = BITFMT_B8G8R8A8;
65
else if ((pixel_format == PixelFormat16bppARGB1555) ||
66
(pixel_format == PixelFormat16bppGrayScale) ||
67
(pixel_format == PixelFormat16bppRGB555) ||
68
(pixel_format == PixelFormat16bppRGB565))
70
pixel_format = PixelFormat24bppRGB; // request conversion to RGB
71
bitmap_format = BITFMT_B8G8R8;
74
else if ((pixel_format == PixelFormat1bppIndexed) ||
75
(pixel_format == PixelFormat4bppIndexed) ||
76
(pixel_format == PixelFormat8bppIndexed))
78
pixel_format = PixelFormat32bppARGB; // request conversion to RGBA
79
bitmap_format = BITFMT_B8G8R8A8;
82
else if (pixel_format == PixelFormat48bppRGB)
84
pixel_format = PixelFormat24bppRGB; // request conversion to RGBA
85
bitmap_format = BITFMT_B8G8R8;
88
else if ((pixel_format == PixelFormat64bppARGB) ||
89
(pixel_format == PixelFormat64bppPARGB))
91
pixel_format = PixelFormat32bppARGB; // request conversion to RGBA
92
bitmap_format = BITFMT_B8G8R8A8;
96
Gdiplus::Rect rect(0, 0, bitmap->GetWidth(), bitmap->GetHeight());
98
Gdiplus::Status status = bitmap->LockBits(&rect, Gdiplus::ImageLockModeRead, pixel_format, bitmap_data);
100
if (status != Gdiplus::Ok)
104
Gdiplus::GdiplusShutdown(token);
108
unsigned int width = bitmap->GetWidth();
109
unsigned int height = bitmap->GetHeight();
111
NTextureData *Texture = NULL;
112
Texture = new NTextureData(bitmap_format, width, height, 1);
116
bitmap->UnlockBits(bitmap_data);
119
Gdiplus::GdiplusShutdown(token);
123
ImageSurface &image_surface = Texture->GetSurface(0);
124
t_u8* dest = image_surface.GetPtrRawData();
125
int dest_pitch = image_surface.GetPitch();
128
unsigned char *src = (unsigned char*) bitmap_data->Scan0;
129
unsigned int src_pitch = bitmap_data->Stride;
131
for (unsigned int i = 0; i < height; i++)
133
Memcpy(dst, src + i*src_pitch, width*channels);
137
bitmap->UnlockBits(bitmap_data);
141
Gdiplus::GdiplusShutdown(token);