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

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