source: trunk/src/image/codec/gdiplus-image.cpp @ 981

Last change on this file since 981 was 981, checked in by sam, 11 years ago

image: add a GDI+ loader.

  • Property svn:keywords set to Id
File size: 2.2 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://sam.zoy.org/projects/COPYING.WTFPL for more details.
9//
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#if defined USE_GDIPLUS
16
17#include <cmath>
18
19#include <algorithm>
20
21using namespace std;
22
23#include <windows.h>
24#include <Gdiplus.h>
25
26#include "core.h"
27#include "../../image/image-private.h"
28
29namespace lol
30{
31
32/*
33 * Image implementation class
34 */
35
36DECLARE_IMAGE_LOADER(GdiPlusImageData, 100)
37{
38public:
39    virtual bool Open(char const *);
40    virtual bool Close();
41
42    virtual void *GetData() const;
43
44private:
45    Gdiplus::Bitmap *bitmap;
46    Gdiplus::BitmapData bdata;
47};
48
49/*
50 * Public Image class
51 */
52
53bool GdiPlusImageData::Open(char const *path)
54{
55    size_t len;
56    len = mbstowcs(NULL, path, 0);
57    wchar_t *wpath = new wchar_t[len + 1];
58    if (mbstowcs(wpath, path, len + 1) == (size_t)-1)
59    {
60#if !LOL_RELEASE
61        Log::Error("invalid image name %s\n", path);
62#endif
63        delete[] wpath;
64        return false;
65    }
66
67    ULONG_PTR token;
68    Gdiplus::GdiplusStartupInput input;
69    Gdiplus::GdiplusStartup(&token, &input, NULL);
70
71    for (wchar_t const *wname = wpath; *wname; wname++)
72        if ((bitmap = Gdiplus::Bitmap::FromFile(wname, 0)))
73            break;
74
75    delete[] wpath;
76    if (!bitmap)
77    {
78#if !LOL_RELEASE
79        Log::Error("could not load %s\n", path);
80#endif
81        return false;
82    }
83
84    size = ivec2(bitmap->GetWidth(), bitmap->GetHeight());
85    format = Image::FORMAT_RGBA;
86
87    Gdiplus::Rect rect(0, 0, size.x, size.y);
88    if(bitmap->LockBits(&rect, Gdiplus::ImageLockModeRead,
89                        PixelFormat32bppARGB, &bdata) != Gdiplus::Ok)
90    {
91#if !LOL_RELEASE
92        Log::Error("could not lock bits in %s\n", path);
93#endif
94        delete bitmap;
95        return false;
96    }
97
98    return true;
99}
100
101bool GdiPlusImageData::Close()
102{
103    bitmap->UnlockBits(&bdata);
104    delete bitmap;
105
106    return true;
107}
108
109void * GdiPlusImageData::GetData() const
110{
111    return bdata.Scan0;
112}
113
114} /* namespace lol */
115
116#endif /* defined USE_GDIPLUS */
117
Note: See TracBrowser for help on using the repository browser.