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

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

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