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

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