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

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