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

Last change on this file since 382 was 382, checked in by Sam Hocevar, 16 years ago
  • proper headers.
  • Property svn:keywords set to Id
File size: 3.8 KB
Line 
1/*
2 * image.c: image I/O functions
3 * $Id: image.c 382 2005-01-03 10:43:31Z 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 <stdint.h>
14#include <stdlib.h>
15
16#include "config.h"
17#include "common.h"
18
19#if defined(HAVE_IMLIB2_H)
20#   include <Imlib2.h>
21#elif defined(HAVE_CV_H)
22#   include <cv.h>
23#   include <highgui.h>
24#else
25#   error "No imaging library"
26#endif
27
28struct image * load_image(char *name)
29{
30    struct image * img;
31#if defined(HAVE_IMLIB2_H)
32    Imlib_Image priv = imlib_load_image(name);
33#elif defined(HAVE_CV_H)
34    IplImage * priv = cvLoadImage(name, -1);
35#endif
36
37    if(!priv)
38        return NULL;
39
40    img = malloc(sizeof(struct image));
41#if defined(HAVE_IMLIB2_H)
42    imlib_context_set_image(priv);
43    img->width = imlib_image_get_width();
44    img->height = imlib_image_get_height();
45    img->pitch = 4 * imlib_image_get_width();
46    img->channels = 4;
47    img->pixels = (char *)imlib_image_get_data();
48#elif defined(HAVE_CV_H)
49    img->width = priv->width;
50    img->height = priv->height;
51    img->pitch = priv->widthStep;
52    img->channels = priv->nChannels;
53    img->pixels = priv->imageData;
54#endif
55    img->priv = (void *)priv;
56
57    return img;
58}
59
60struct image * new_image(int width, int height)
61{
62    struct image * img;
63#if defined(HAVE_IMLIB2_H)
64    Imlib_Image priv = imlib_create_image(width, height);
65#elif defined(HAVE_CV_H)
66    IplImage * priv = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 3);
67#endif
68
69    if(!priv)
70        return NULL;
71
72    img = malloc(sizeof(struct image));
73#if defined(HAVE_IMLIB2_H)
74    imlib_context_set_image(priv);
75    img->width = imlib_image_get_width();
76    img->height = imlib_image_get_height();
77    img->pitch = 4 * imlib_image_get_width();
78    img->channels = 4;
79    img->pixels = (char *)imlib_image_get_data();
80#elif defined(HAVE_CV_H)
81    img->width = priv->width;
82    img->height = priv->height;
83    img->pitch = priv->widthStep;
84    img->channels = priv->nChannels;
85    img->pixels = priv->imageData;
86#endif
87    img->priv = (void *)priv;
88
89    return img;
90}
91
92int getgray(struct image *img, int x, int y, int *g)
93{
94    if(x < 0 || y < 0 || x >= img->width || y >= img->height)
95    {
96        *g = 255;
97        return -1;
98    }
99
100    *g = (unsigned char)img->pixels[y * img->pitch + x * img->channels + 1];
101
102    return 0;
103}
104
105int getpixel(struct image *img, int x, int y, int *r, int *g, int *b)
106{
107    if(x < 0 || y < 0 || x >= img->width || y >= img->height)
108    {
109        *r = 255;
110        *g = 255;
111        *b = 255;
112        return -1;
113    }
114
115    *b = (unsigned char)img->pixels[y * img->pitch + x * img->channels];
116    *g = (unsigned char)img->pixels[y * img->pitch + x * img->channels + 1];
117    *r = (unsigned char)img->pixels[y * img->pitch + x * img->channels + 2];
118
119    return 0;
120}
121
122int setpixel(struct image *img, int x, int y, int r, int g, int b)
123{
124    if(x < 0 || y < 0 || x >= img->width || y >= img->height)
125        return -1;
126
127    img->pixels[y * img->pitch + x * img->channels] = b;
128    img->pixels[y * img->pitch + x * img->channels + 1] = g;
129    img->pixels[y * img->pitch + x * img->channels + 2] = r;
130
131    return 0;
132}
133
134void display_image(struct image *img)
135{
136    char name[BUFSIZ];
137#if defined(HAVE_IMLIB2_H)
138    //static int i = 0;
139    //sprintf(name, "image%i-%ix%i.png", i++, img->width, img->height);
140    //imlib_context_set_image(img->priv);
141    //imlib_save_image(name);
142    //fprintf(stderr, "saved to %s\n", name);
143#elif defined(HAVE_CV_H)
144    sprintf(name, "Image %p (%i x %i)", img, img->width, img->height);
145    cvNamedWindow(name, 0);
146    cvShowImage(name, img->priv);
147    cvResizeWindow(name, 320, 120);
148#endif
149}
150
Note: See TracBrowser for help on using the repository browser.