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

Revision 3074, 3.8 KB checked in by sam, 5 years ago (diff)

libpipi: the GDI+ codec can now compile using mingw32, unfortunately it
does not link yet due to a bug in mingw's ld.

  • Property svn:executable set to *
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#if 0
37    size_t len;
38    len = mbstowcs(NULL, name, 0);
39    wchar_t *wname = new wchar_t[len + 1];
40    if(mbstowcs(wname, name, len + 1) != (size_t)-1)
41    {
42        delete[] wname;
43        return NULL;
44    }
45
46    ULONG_PTR token;
47    Gdiplus::GdiplusStartupInput input;
48    Gdiplus::GdiplusStartup(&token, &input, NULL);
49
50    Gdiplus::Bitmap *b = Gdiplus::Bitmap::FromFile(wname, 0);
51    if(!b)
52    {
53        delete[] wname;
54        return NULL;
55    }
56    delete[] wname;
57
58    Gdiplus::BitmapData bdata;
59    Gdiplus::Rect rect(0, 0, b->GetWidth(), b->GetHeight());
60
61    if(b->LockBits(rect, Gdiplus::ImageLockModeRead,
62                   PixelFormat32bppARGB, &bdata) != Gdiplus::Ok)
63    {
64        delete b;
65        return NULL;
66    }
67
68    pipi_image_t *img = pipi_new(b->GetWidth(), b->GetHeight());
69    pipi_pixels_t *p = pipi_getpixels(img, PIPI_PIXELS_RGBA_C);
70    memcpy(p->pixels, bdata.Scan0, bdata.Width * bdata.Height * 4);
71
72    b->UnlockBits(&bdata);
73    delete b;
74
75    img->codec_priv = NULL;
76
77    img->wrap = 0;
78    img->u8 = 1;
79
80    return img;
81#endif
82return NULL;
83}
84
85extern "C" int pipi_save_gdiplus(pipi_image_t *img, const char *name)
86{
87#if 0
88    wchar_t const *fmt;
89    if(strstr(name, ".gif"))
90        fmt = L"image/gif";
91    else if(strstr(name, ".jpeg") || strstr(name, ".jpeg"))
92        fmt = L"image/jpeg";
93    else if(strstr(name, ".png"))
94        fmt = L"image/png";
95    else if(strstr(name, ".tiff"))
96        fmt = L"image/tiff";
97
98    unsigned int num = 0, size = 0;
99    Gdiplus::GetImageEncodersSize(&num, &size);
100    if(size == 0)
101        return -1;
102    Gdiplus::ImageCodecInfo *codecs
103        = (Gdiplus::ImageCodecInfo *)(malloc(size));
104    if(!codecs)
105        return -1;
106    Gdiplus::GetImageEncoders(num, size, codecs);
107    CLSID clsid;
108    for(unsigned int i = 0; i < num; i++)
109    {
110        if(wcscmp(codecs[i].MimeType, fmt) == 0)
111        {
112            clsid = codecs[i].Clsid;
113            break;
114        }
115    }
116
117    size_t len;
118    len = mbstowcs(NULL, name, 0);
119    wchar_t *wname = new wchar_t[len + 1];
120    if(mbstowcs(wname, name, len + 1) != (size_t)-1)
121    {
122        delete[] wname;
123        return -1;
124    }
125
126    ULONG_PTR token;
127    Gdiplus::GdiplusStartupInput input;
128    Gdiplus::GdiplusStartup(&token, &input, NULL);
129
130    Gdiplus::Bitmap *b = new Gdiplus::Bitmap(img->w, img->h,
131                                             PixelFormat32bppARGB);
132
133    Gdiplus::BitmapData bdata;
134    Gdiplus::Rect rect(0, 0, img->w, img->h);
135
136    if(b->LockBits(rect, (unsigned int)Gdiplus::ImageLockModeWrite,
137                   PixelFormat32bppARGB, &bdata) != Gdiplus::Ok)
138    {
139        delete b;
140        return -1;
141    }
142
143    pipi_pixels_t *p = pipi_getpixels(img, PIPI_PIXELS_RGBA_C);
144    memcpy(bdata.Scan0, p->pixels, bdata.Width * bdata.Height * 4);
145
146    b->UnlockBits(&bdata);
147
148    if(b->Save(wname, &clsid, NULL) != Gdiplus::Ok)
149    {
150        delete[] wname;
151        delete b;
152        return -1;
153    }
154    delete[] wname;
155    delete b;
156
157#endif
158    return 0;
159}
160
161/*
162 * XXX: The following functions are local.
163 */
164
Note: See TracBrowser for help on using the repository browser.