1 | /* |
---|
2 | * image.c: image I/O functions |
---|
3 | * $Id: image.c 389 2005-01-03 21:48:54Z 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_CV_H) |
---|
19 | # include <cv.h> |
---|
20 | # include <highgui.h> |
---|
21 | #elif defined(HAVE_IMLIB2_H) |
---|
22 | # include <Imlib2.h> |
---|
23 | #else |
---|
24 | # error "No imaging library" |
---|
25 | #endif |
---|
26 | |
---|
27 | struct image *image_load(char *name) |
---|
28 | { |
---|
29 | struct image *img; |
---|
30 | #if defined(HAVE_CV_H) |
---|
31 | IplImage *priv = cvLoadImage(name, -1); |
---|
32 | #elif defined(HAVE_IMLIB2_H) |
---|
33 | Imlib_Image priv = imlib_load_image(name); |
---|
34 | #endif |
---|
35 | |
---|
36 | if(!priv) |
---|
37 | return NULL; |
---|
38 | |
---|
39 | img = malloc(sizeof(struct image)); |
---|
40 | #if defined(HAVE_CV_H) |
---|
41 | img->width = priv->width; |
---|
42 | img->height = priv->height; |
---|
43 | img->pitch = priv->widthStep; |
---|
44 | img->channels = priv->nChannels; |
---|
45 | img->pixels = priv->imageData; |
---|
46 | #elif defined(HAVE_IMLIB2_H) |
---|
47 | imlib_context_set_image(priv); |
---|
48 | img->width = imlib_image_get_width(); |
---|
49 | img->height = imlib_image_get_height(); |
---|
50 | img->pitch = 4 * imlib_image_get_width(); |
---|
51 | img->channels = 4; |
---|
52 | img->pixels = (char *)imlib_image_get_data(); |
---|
53 | #endif |
---|
54 | img->priv = (void *)priv; |
---|
55 | |
---|
56 | return img; |
---|
57 | } |
---|
58 | |
---|
59 | struct image *image_new(int width, int height) |
---|
60 | { |
---|
61 | struct image *img; |
---|
62 | #if defined(HAVE_CV_H) |
---|
63 | IplImage *priv = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 3); |
---|
64 | #elif defined(HAVE_IMLIB2_H) |
---|
65 | Imlib_Image priv = imlib_create_image(width, height); |
---|
66 | #endif |
---|
67 | |
---|
68 | if(!priv) |
---|
69 | return NULL; |
---|
70 | |
---|
71 | img = malloc(sizeof(struct image)); |
---|
72 | #if defined(HAVE_CV_H) |
---|
73 | img->width = priv->width; |
---|
74 | img->height = priv->height; |
---|
75 | img->pitch = priv->widthStep; |
---|
76 | img->channels = priv->nChannels; |
---|
77 | img->pixels = priv->imageData; |
---|
78 | #elif defined(HAVE_IMLIB2_H) |
---|
79 | imlib_context_set_image(priv); |
---|
80 | img->width = imlib_image_get_width(); |
---|
81 | img->height = imlib_image_get_height(); |
---|
82 | img->pitch = 4 * imlib_image_get_width(); |
---|
83 | img->channels = 4; |
---|
84 | img->pixels = (char *)imlib_image_get_data(); |
---|
85 | #endif |
---|
86 | img->priv = (void *)priv; |
---|
87 | |
---|
88 | return img; |
---|
89 | } |
---|
90 | |
---|
91 | void image_free(struct image *img) |
---|
92 | { |
---|
93 | #if defined(HAVE_CV_H) |
---|
94 | IplImage *iplimg; |
---|
95 | iplimg = (IplImage *)img->priv; |
---|
96 | cvReleaseImage(&iplimg); |
---|
97 | #elif defined(HAVE_IMLIB2_H) |
---|
98 | imlib_context_set_image(img->priv); |
---|
99 | imlib_free_image(); |
---|
100 | #endif |
---|
101 | |
---|
102 | free(img); |
---|
103 | } |
---|
104 | |
---|
105 | int getgray(struct image *img, int x, int y, int *g) |
---|
106 | { |
---|
107 | if(x < 0 || y < 0 || x >= img->width || y >= img->height) |
---|
108 | { |
---|
109 | *g = 255; |
---|
110 | return -1; |
---|
111 | } |
---|
112 | |
---|
113 | *g = (unsigned char)img->pixels[y * img->pitch + x * img->channels + 1]; |
---|
114 | |
---|
115 | return 0; |
---|
116 | } |
---|
117 | |
---|
118 | int getpixel(struct image *img, int x, int y, int *r, int *g, int *b) |
---|
119 | { |
---|
120 | if(x < 0 || y < 0 || x >= img->width || y >= img->height) |
---|
121 | { |
---|
122 | *r = 255; |
---|
123 | *g = 255; |
---|
124 | *b = 255; |
---|
125 | return -1; |
---|
126 | } |
---|
127 | |
---|
128 | *b = (unsigned char)img->pixels[y * img->pitch + x * img->channels]; |
---|
129 | *g = (unsigned char)img->pixels[y * img->pitch + x * img->channels + 1]; |
---|
130 | *r = (unsigned char)img->pixels[y * img->pitch + x * img->channels + 2]; |
---|
131 | |
---|
132 | return 0; |
---|
133 | } |
---|
134 | |
---|
135 | int setpixel(struct image *img, int x, int y, int r, int g, int b) |
---|
136 | { |
---|
137 | if(x < 0 || y < 0 || x >= img->width || y >= img->height) |
---|
138 | return -1; |
---|
139 | |
---|
140 | img->pixels[y * img->pitch + x * img->channels] = b; |
---|
141 | img->pixels[y * img->pitch + x * img->channels + 1] = g; |
---|
142 | img->pixels[y * img->pitch + x * img->channels + 2] = r; |
---|
143 | |
---|
144 | return 0; |
---|
145 | } |
---|
146 | |
---|
147 | void image_display(struct image *img) |
---|
148 | { |
---|
149 | #if defined(HAVE_CV_H) |
---|
150 | char name[BUFSIZ]; |
---|
151 | sprintf(name, "Image %p (%i x %i)", img, img->width, img->height); |
---|
152 | cvNamedWindow(name, 0); |
---|
153 | cvShowImage(name, img->priv); |
---|
154 | cvResizeWindow(name, img->width * 2, img->height * 2 + 50); |
---|
155 | while((unsigned char)cvWaitKey(0) != 0x1b) |
---|
156 | ; |
---|
157 | #elif defined(HAVE_IMLIB2_H) |
---|
158 | //char name[BUFSIZ]; |
---|
159 | //static int i = 0; |
---|
160 | //sprintf(name, "image%i-%ix%i.png", i++, img->width, img->height); |
---|
161 | //imlib_context_set_image(img->priv); |
---|
162 | //imlib_save_image(name); |
---|
163 | //fprintf(stderr, "saved to %s\n", name); |
---|
164 | #endif |
---|
165 | } |
---|
166 | |
---|