source: libpipi/trunk/pipi/codec/gdiplus.cpp @ 3077

Last change on this file since 3077 was 3077, checked in by Sam Hocevar, 12 years ago

Cope with disparities between GDI+ versions.

  • Property svn:executable set to *
File size: 3.7 KB
Line 
1/*
2 *  libpipi       Pathetic image processing interface library
3 *  Copyright (c) 2004-2008 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id$
7 *
8 *  This library is free software. It comes without any warranty, to
9 *  the extent permitted by applicable law. You can redistribute it
10 *  and/or modify it under the terms of the Do What The Fuck You Want
11 *  To Public License, Version 2, as published by Sam Hocevar. See
12 *  http://sam.zoy.org/wtfpl/COPYING for more details.
13 */
14
15/*
16 * gdi.c: Windows GDI I/O functions (BMP only)
17 */
18
19#include "config.h"
20
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24
25#include <windows.h>
26
27#include <Gdiplus.h>
28
29extern "C" {
30#include "pipi.h"
31#include "pipi_internals.h"
32}
33
34extern "C" pipi_image_t *pipi_load_gdiplus(const char *name)
35{
36    size_t len;
37    len = mbstowcs(NULL, name, 0);
38    wchar_t *wname = new wchar_t[len + 1];
39    if(mbstowcs(wname, name, len + 1) == (size_t)-1)
40    {
41        delete[] wname;
42        return NULL;
43    }
44
45    ULONG_PTR token;
46    Gdiplus::GdiplusStartupInput input;
47    Gdiplus::GdiplusStartup(&token, &input, NULL);
48
49    Gdiplus::Bitmap *b = Gdiplus::Bitmap::FromFile(wname, 0);
50    if(!b)
51    {
52        delete[] wname;
53        return NULL;
54    }
55    delete[] wname;
56
57    Gdiplus::BitmapData bdata;
58    Gdiplus::Rect rect(0, 0, b->GetWidth(), b->GetHeight());
59
60    if(b->LockBits(&rect, Gdiplus::ImageLockModeRead,
61                   PixelFormat32bppARGB, &bdata) != Gdiplus::Ok)
62    {
63        delete b;
64        return NULL;
65    }
66
67    pipi_image_t *img = pipi_new(b->GetWidth(), b->GetHeight());
68    pipi_pixels_t *p = pipi_getpixels(img, PIPI_PIXELS_RGBA_C);
69    memcpy(p->pixels, bdata.Scan0, bdata.Width * bdata.Height * 4);
70
71    b->UnlockBits(&bdata);
72    delete b;
73
74    img->codec_priv = NULL;
75
76    img->wrap = 0;
77    img->u8 = 1;
78
79    return img;
80}
81
82extern "C" int pipi_save_gdiplus(pipi_image_t *img, const char *name)
83{
84    wchar_t const *fmt;
85    if(strstr(name, ".gif"))
86        fmt = L"image/gif";
87    else if(strstr(name, ".jpeg") || strstr(name, ".jpeg"))
88        fmt = L"image/jpeg";
89    else if(strstr(name, ".png"))
90        fmt = L"image/png";
91    else if(strstr(name, ".tiff"))
92        fmt = L"image/tiff";
93
94    unsigned int num = 0, size = 0;
95    Gdiplus::GetImageEncodersSize(&num, &size);
96    if(size == 0)
97        return -1;
98    Gdiplus::ImageCodecInfo *codecs
99        = (Gdiplus::ImageCodecInfo *)(malloc(size));
100    if(!codecs)
101        return -1;
102    Gdiplus::GetImageEncoders(num, size, codecs);
103    CLSID clsid;
104    for(unsigned int i = 0; i < num; i++)
105    {
106        if(wcscmp(codecs[i].MimeType, fmt) == 0)
107        {
108            clsid = codecs[i].Clsid;
109            break;
110        }
111    }
112
113    size_t len;
114    len = mbstowcs(NULL, name, 0);
115    wchar_t *wname = new wchar_t[len + 1];
116    if(mbstowcs(wname, name, len + 1) == (size_t)-1)
117    {
118        delete[] wname;
119        return -1;
120    }
121
122    ULONG_PTR token;
123    Gdiplus::GdiplusStartupInput input;
124    Gdiplus::GdiplusStartup(&token, &input, NULL);
125
126    Gdiplus::Bitmap *b = new Gdiplus::Bitmap(img->w, img->h,
127                                             PixelFormat32bppARGB);
128
129    Gdiplus::BitmapData bdata;
130    Gdiplus::Rect rect(0, 0, img->w, img->h);
131
132    if(b->LockBits(&rect, (unsigned int)Gdiplus::ImageLockModeWrite,
133                   PixelFormat32bppARGB, &bdata) != Gdiplus::Ok)
134    {
135        delete b;
136        return -1;
137    }
138
139    pipi_pixels_t *p = pipi_getpixels(img, PIPI_PIXELS_RGBA_C);
140    memcpy(bdata.Scan0, p->pixels, bdata.Width * bdata.Height * 4);
141
142    b->UnlockBits(&bdata);
143
144    if(b->Save(wname, &clsid, NULL) != Gdiplus::Ok)
145    {
146        delete[] wname;
147        delete b;
148        return -1;
149    }
150    delete[] wname;
151    delete b;
152
153    return 0;
154}
155
156/*
157 * XXX: The following functions are local.
158 */
159
Note: See TracBrowser for help on using the repository browser.