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

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