source: pwntcha/trunk/src/image.c @ 438

Last change on this file since 438 was 438, checked in by Sam Hocevar, 15 years ago
  • use IPicture's DC instead of CreateCompatibleDC.
  • nicer error message.
  • Property svn:keywords set to Id
File size: 9.4 KB
Line 
1/*
2 * image.c: image I/O functions
3 * $Id: image.c 438 2005-01-09 00:24:24Z sam $
4 *
5 * Copyright: (c) 2004 Sam Hocevar <sam@zoy.org>
6 *   This program is free software; you can redistribute it and/or
7 *   modify it under the terms of the Do What The Fuck You Want To
8 *   Public License as published by Banlu Kemiyatorn. See
9 *   http://sam.zoy.org/projects/COPYING.WTFPL for more details.
10 */
11
12#include <stdio.h>
13#include <stdlib.h>
14
15#include "config.h"
16#include "common.h"
17
18#if defined(WIN32)
19#   include <windows.h>
20#   include <ocidl.h>
21#   include <olectl.h>
22BOOL oleload(LPCTSTR name, LPPICTURE* pic);
23struct priv
24{
25    HBITMAP bitmap;
26    BITMAPINFO info;
27};
28#elif defined(HAVE_SDL_IMAGE_H)
29#   include <SDL_image.h>
30#elif defined(HAVE_IMLIB2_H)
31#   include <Imlib2.h>
32#elif defined(HAVE_CV_H)
33#   include <cv.h>
34#   include <highgui.h>
35#else
36#   error "No imaging library"
37#endif
38
39struct image *image_load(const char *name)
40{
41    struct image *img;
42#if defined(WIN32)
43    struct priv *priv = malloc(sizeof(struct priv));
44    LPPICTURE pic = NULL;
45    HDC dc;
46    long scrwidth = 0, scrheight = 0;
47    int width, height;
48    void *data = NULL;
49#elif defined(HAVE_SDL_IMAGE_H)
50    SDL_Surface *priv = IMG_Load(name);
51#elif defined(HAVE_IMLIB2_H)
52    Imlib_Image priv = imlib_load_image(name);
53#elif defined(HAVE_CV_H)
54    IplImage *priv = cvLoadImage(name, -1);
55#endif
56
57    if(!priv)
58        return NULL;
59
60#if defined(WIN32)
61    if(!oleload((LPCTSTR)name, &pic))
62    {
63        free(priv);
64        return NULL;
65    }
66
67#if 0
68    for(i = 0; ; i++)
69    {
70        DEVMODE devMode;
71        devMode.dmSize = sizeof(DEVMODE);
72
73        if(EnumDisplaySettings(NULL, i, &devMode) != 1)
74            break;
75
76        printf("mode %i x %i - %i\n", (int)devMode.dmPelsWidth,
77               (int)devMode.dmPelsHeight, (int)devMode.dmBitsPerPel);
78    }
79#endif
80
81    pic->lpVtbl->get_CurDC(pic, &dc);
82
83    if(GetDeviceCaps(dc, BITSPIXEL) < 24)
84    {
85        fprintf(stderr, "%s: 24bpp screen depth or better required\n", argv0);
86        DeleteDC(dc);
87        free(priv);
88        return NULL;
89    }
90
91    pic->lpVtbl->get_Width(pic, &scrwidth);
92    pic->lpVtbl->get_Height(pic, &scrheight);
93    width = (scrwidth * GetDeviceCaps(dc, LOGPIXELSX) + 2540 / 2) / 2540;
94    height = (scrheight * GetDeviceCaps(dc, LOGPIXELSY) + 2540 / 2) / 2540;
95
96    memset(&priv->info, 0, sizeof(BITMAPINFO));
97    priv->info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
98    priv->info.bmiHeader.biBitCount = 24;
99    priv->info.bmiHeader.biWidth = width;
100    priv->info.bmiHeader.biHeight = -height;
101    priv->info.bmiHeader.biCompression = BI_RGB;
102    priv->info.bmiHeader.biPlanes = 1;
103
104    priv->bitmap = CreateDIBSection(dc, &priv->info, DIB_RGB_COLORS, &data, 0, 0);
105    SelectObject(dc, priv->bitmap);
106    pic->lpVtbl->Render(pic, dc, 0, 0, width, height,
107                        0, scrheight, scrwidth, -scrheight, NULL);
108    pic->lpVtbl->Release(pic);
109    DeleteDC(dc);
110#elif defined(HAVE_SDL_IMAGE_H)
111    if(priv->format->BytesPerPixel == 1)
112    {
113        img = image_new(priv->w, priv->h);
114        SDL_BlitSurface(priv, NULL, img->priv, NULL);
115        SDL_FreeSurface(priv);
116        return img;
117    }
118#endif
119
120    img = (struct image *)malloc(sizeof(struct image));
121#if defined(WIN32)
122    img->width = width;
123    img->height = height;
124    img->pitch = 3 * width;
125    img->channels = 3;
126    img->pixels = data;
127#elif defined(HAVE_SDL_IMAGE_H)
128    img->width = priv->w;
129    img->height = priv->h;
130    img->pitch = priv->pitch;
131    img->channels = priv->format->BytesPerPixel;
132    img->pixels = priv->pixels;
133#elif defined(HAVE_IMLIB2_H)
134    imlib_context_set_image(priv);
135    img->width = imlib_image_get_width();
136    img->height = imlib_image_get_height();
137    img->pitch = 4 * imlib_image_get_width();
138    img->channels = 4;
139    img->pixels = (char *)imlib_image_get_data();
140#elif defined(HAVE_CV_H)
141    img->width = priv->width;
142    img->height = priv->height;
143    img->pitch = priv->widthStep;
144    img->channels = priv->nChannels;
145    img->pixels = priv->imageData;
146#endif
147    img->priv = (void *)priv;
148
149    return img;
150}
151
152struct image *image_new(int width, int height)
153{
154    struct image *img;
155#if defined(WIN32)
156    struct priv *priv = malloc(sizeof(struct priv));
157    HDC dc;
158    void *data = NULL;
159#elif defined(HAVE_SDL_IMAGE_H)
160    SDL_Surface *priv;
161    Uint32 rmask, gmask, bmask, amask;
162#   if SDL_BYTEORDER == SDL_BIG_ENDIAN
163    rmask = 0xff000000;
164    gmask = 0x00ff0000;
165    bmask = 0x0000ff00;
166    amask = 0x00000000;
167#   else
168    rmask = 0x000000ff;
169    gmask = 0x0000ff00;
170    bmask = 0x00ff0000;
171    amask = 0x00000000;
172#   endif
173    priv = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32,
174                                rmask, gmask, bmask, amask);
175#elif defined(HAVE_IMLIB2_H)
176    Imlib_Image priv = imlib_create_image(width, height);
177#elif defined(HAVE_CV_H)
178    IplImage *priv = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 3);
179#endif
180
181    if(!priv)
182        return NULL;
183
184    img = (struct image *)malloc(sizeof(struct image));
185#if defined(WIN32)
186    dc = CreateCompatibleDC(NULL);
187    if(GetDeviceCaps(dc, BITSPIXEL) < 24)
188    {
189        fprintf(stderr, "a screen depth of at least 24bpp is required\n");
190        DeleteDC(dc);
191        free(priv);
192        return NULL;
193    }
194    priv->info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
195    priv->info.bmiHeader.biWidth = width;
196    priv->info.bmiHeader.biHeight = -height;
197    priv->info.bmiHeader.biCompression = BI_RGB;
198    priv->info.bmiHeader.biBitCount = 24;
199    priv->info.bmiHeader.biPlanes = 1;
200    priv->bitmap = CreateDIBSection(dc, &priv->info,
201                                    DIB_RGB_COLORS, &data, 0, 0);
202    DeleteDC(dc);
203    img->width = width;
204    img->height = height;
205    img->pitch = 3 * width;
206    img->channels = 3;
207    img->pixels = data;
208#elif defined(HAVE_SDL_IMAGE_H)
209    img->width = priv->w;
210    img->height = priv->h;
211    img->pitch = priv->pitch;
212    img->channels = priv->format->BytesPerPixel;
213    img->pixels = priv->pixels;
214#elif defined(HAVE_IMLIB2_H)
215    imlib_context_set_image(priv);
216    img->width = imlib_image_get_width();
217    img->height = imlib_image_get_height();
218    img->pitch = 4 * imlib_image_get_width();
219    img->channels = 4;
220    img->pixels = (char *)imlib_image_get_data();
221#elif defined(HAVE_CV_H)
222    img->width = priv->width;
223    img->height = priv->height;
224    img->pitch = priv->widthStep;
225    img->channels = priv->nChannels;
226    img->pixels = priv->imageData;
227#endif
228    img->priv = (void *)priv;
229
230    return img;
231}
232
233void image_free(struct image *img)
234{
235#if defined(WIN32)
236    struct priv *priv = img->priv;
237    DeleteObject(priv->bitmap);
238    free(img->priv);
239#elif defined(HAVE_SDL_IMAGE_H)
240    SDL_FreeSurface(img->priv);
241#elif defined(HAVE_IMLIB2_H)
242    imlib_context_set_image(img->priv);
243    imlib_free_image();
244#elif defined(HAVE_CV_H)
245    IplImage *iplimg;
246    iplimg = (IplImage *)img->priv;
247    cvReleaseImage(&iplimg);
248#endif
249
250    free(img);
251}
252
253void image_save(struct image *img, const char *name)
254{
255#if defined(WIN32)
256
257#elif defined(HAVE_SDL_IMAGE_H)
258    SDL_SaveBMP(img->priv, name);
259#elif defined(HAVE_IMLIB2_H)
260    imlib_context_set_image(img->priv);
261    imlib_save_image(name);
262#elif defined(HAVE_CV_H)
263    cvSaveImage(name, img->priv);
264#endif
265}
266
267int getgray(struct image *img, int x, int y, int *g)
268{
269    if(x < 0 || y < 0 || x >= img->width || y >= img->height)
270    {
271        *g = 255;
272        return -1;
273    }
274
275    *g = (unsigned char)img->pixels[y * img->pitch + x * img->channels + 1];
276
277    return 0;
278}
279
280int getpixel(struct image *img, int x, int y, int *r, int *g, int *b)
281{
282    if(x < 0 || y < 0 || x >= img->width || y >= img->height)
283    {
284        *r = 255;
285        *g = 255;
286        *b = 255;
287        return -1;
288    }
289
290    *b = (unsigned char)img->pixels[y * img->pitch + x * img->channels];
291    *g = (unsigned char)img->pixels[y * img->pitch + x * img->channels + 1];
292    *r = (unsigned char)img->pixels[y * img->pitch + x * img->channels + 2];
293
294    return 0;
295}
296
297int setpixel(struct image *img, int x, int y, int r, int g, int b)
298{
299    if(x < 0 || y < 0 || x >= img->width || y >= img->height)
300        return -1;
301
302    img->pixels[y * img->pitch + x * img->channels] = b;
303    img->pixels[y * img->pitch + x * img->channels + 1] = g;
304    img->pixels[y * img->pitch + x * img->channels + 2] = r;
305
306    return 0;
307}
308
309#if defined(WIN32)
310BOOL oleload(LPCTSTR name, LPPICTURE* pic)
311{
312    HRESULT ret;
313    HANDLE h;
314    DWORD size, read = 0;
315    LPVOID data;
316    HGLOBAL buffer;
317    LPSTREAM stream = NULL;
318
319    h = CreateFile(name, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
320    if (h == INVALID_HANDLE_VALUE)
321        return FALSE;
322
323    size = GetFileSize(h, NULL);
324    if(size == (DWORD)-1)
325    {
326        CloseHandle(h);
327        return FALSE;
328    }
329
330    buffer = GlobalAlloc(GMEM_MOVEABLE, size);
331    if(!buffer)
332    {
333        CloseHandle(h);
334        return FALSE;
335    }
336
337    data = GlobalLock(buffer);
338    if(!data)
339    {
340        GlobalUnlock(buffer);
341        CloseHandle(h);
342        return FALSE;
343    }
344
345    ret = ReadFile(h, data, size, &read, NULL);
346    GlobalUnlock(buffer);
347    CloseHandle(h);
348
349    if(!ret)
350        return FALSE;
351
352    ret = CreateStreamOnHGlobal(buffer, TRUE, &stream);
353    if(!SUCCEEDED(ret))
354    {
355        if(stream)
356            stream->lpVtbl->Release(stream);
357        return FALSE;
358    }
359
360    if(!stream)
361        return FALSE;
362
363    if(*pic)
364        (*pic)->lpVtbl->Release(*pic);
365
366    ret = OleLoadPicture(stream, size, FALSE, &IID_IPicture, (PVOID *)pic);
367    stream->lpVtbl->Release(stream);
368
369    if(!SUCCEEDED(ret))
370        return FALSE;
371
372    if(!*pic)
373        return FALSE;
374
375    return TRUE;
376}
377#endif
378
Note: See TracBrowser for help on using the repository browser.