[382] | 1 | /* |
---|
| 2 | * image.c: image I/O functions |
---|
| 3 | * $Id: image.c 413 2005-01-04 17:06:04Z 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 | */ |
---|
[381] | 11 | |
---|
| 12 | #include <stdio.h> |
---|
| 13 | #include <stdlib.h> |
---|
| 14 | |
---|
| 15 | #include "config.h" |
---|
| 16 | #include "common.h" |
---|
| 17 | |
---|
[407] | 18 | #if defined(HAVE_SDL_IMAGE_H) |
---|
| 19 | # include <SDL_image.h> |
---|
| 20 | #elif defined(HAVE_IMLIB2_H) |
---|
| 21 | # include <Imlib2.h> |
---|
| 22 | #elif defined(HAVE_CV_H) |
---|
[381] | 23 | # include <cv.h> |
---|
| 24 | # include <highgui.h> |
---|
| 25 | #else |
---|
| 26 | # error "No imaging library" |
---|
| 27 | #endif |
---|
| 28 | |
---|
[413] | 29 | struct image *image_load(const char *name) |
---|
[381] | 30 | { |
---|
[389] | 31 | struct image *img; |
---|
[407] | 32 | #if defined(HAVE_SDL_IMAGE_H) |
---|
| 33 | SDL_Surface *priv = IMG_Load(name); |
---|
[389] | 34 | #elif defined(HAVE_IMLIB2_H) |
---|
[381] | 35 | Imlib_Image priv = imlib_load_image(name); |
---|
[407] | 36 | #elif defined(HAVE_CV_H) |
---|
| 37 | IplImage *priv = cvLoadImage(name, -1); |
---|
[381] | 38 | #endif |
---|
| 39 | |
---|
| 40 | if(!priv) |
---|
| 41 | return NULL; |
---|
| 42 | |
---|
| 43 | img = malloc(sizeof(struct image)); |
---|
[407] | 44 | #if defined(HAVE_SDL_IMAGE_H) |
---|
| 45 | img->width = priv->w; |
---|
| 46 | img->height = priv->h; |
---|
| 47 | img->pitch = priv->pitch; |
---|
| 48 | img->channels = priv->format->BytesPerPixel; |
---|
| 49 | img->pixels = priv->pixels; |
---|
[389] | 50 | #elif defined(HAVE_IMLIB2_H) |
---|
[381] | 51 | imlib_context_set_image(priv); |
---|
| 52 | img->width = imlib_image_get_width(); |
---|
| 53 | img->height = imlib_image_get_height(); |
---|
| 54 | img->pitch = 4 * imlib_image_get_width(); |
---|
| 55 | img->channels = 4; |
---|
| 56 | img->pixels = (char *)imlib_image_get_data(); |
---|
[407] | 57 | #elif defined(HAVE_CV_H) |
---|
| 58 | img->width = priv->width; |
---|
| 59 | img->height = priv->height; |
---|
| 60 | img->pitch = priv->widthStep; |
---|
| 61 | img->channels = priv->nChannels; |
---|
| 62 | img->pixels = priv->imageData; |
---|
[381] | 63 | #endif |
---|
| 64 | img->priv = (void *)priv; |
---|
| 65 | |
---|
| 66 | return img; |
---|
| 67 | } |
---|
| 68 | |
---|
[389] | 69 | struct image *image_new(int width, int height) |
---|
[381] | 70 | { |
---|
[389] | 71 | struct image *img; |
---|
[407] | 72 | #if defined(HAVE_SDL_IMAGE_H) |
---|
| 73 | SDL_Surface *priv; |
---|
| 74 | Uint32 rmask, gmask, bmask, amask; |
---|
| 75 | # if SDL_BYTEORDER == SDL_BIG_ENDIAN |
---|
| 76 | rmask = 0xff000000; |
---|
| 77 | gmask = 0x00ff0000; |
---|
| 78 | bmask = 0x0000ff00; |
---|
[413] | 79 | amask = 0x00000000; |
---|
[407] | 80 | # else |
---|
| 81 | rmask = 0x000000ff; |
---|
| 82 | gmask = 0x0000ff00; |
---|
| 83 | bmask = 0x00ff0000; |
---|
[413] | 84 | amask = 0x00000000; |
---|
[407] | 85 | # endif |
---|
| 86 | priv = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32, |
---|
| 87 | rmask, gmask, bmask, amask); |
---|
[389] | 88 | #elif defined(HAVE_IMLIB2_H) |
---|
[381] | 89 | Imlib_Image priv = imlib_create_image(width, height); |
---|
[407] | 90 | #elif defined(HAVE_CV_H) |
---|
| 91 | IplImage *priv = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 3); |
---|
[381] | 92 | #endif |
---|
| 93 | |
---|
| 94 | if(!priv) |
---|
| 95 | return NULL; |
---|
| 96 | |
---|
| 97 | img = malloc(sizeof(struct image)); |
---|
[407] | 98 | #if defined(HAVE_SDL_IMAGE_H) |
---|
| 99 | img->width = priv->w; |
---|
| 100 | img->height = priv->h; |
---|
| 101 | img->pitch = priv->pitch; |
---|
| 102 | img->channels = priv->format->BytesPerPixel; |
---|
| 103 | img->pixels = priv->pixels; |
---|
[389] | 104 | #elif defined(HAVE_IMLIB2_H) |
---|
[381] | 105 | imlib_context_set_image(priv); |
---|
| 106 | img->width = imlib_image_get_width(); |
---|
| 107 | img->height = imlib_image_get_height(); |
---|
| 108 | img->pitch = 4 * imlib_image_get_width(); |
---|
| 109 | img->channels = 4; |
---|
| 110 | img->pixels = (char *)imlib_image_get_data(); |
---|
[407] | 111 | #elif defined(HAVE_CV_H) |
---|
| 112 | img->width = priv->width; |
---|
| 113 | img->height = priv->height; |
---|
| 114 | img->pitch = priv->widthStep; |
---|
| 115 | img->channels = priv->nChannels; |
---|
| 116 | img->pixels = priv->imageData; |
---|
[381] | 117 | #endif |
---|
| 118 | img->priv = (void *)priv; |
---|
| 119 | |
---|
| 120 | return img; |
---|
| 121 | } |
---|
| 122 | |
---|
[389] | 123 | void image_free(struct image *img) |
---|
| 124 | { |
---|
[407] | 125 | #if defined(HAVE_SDL_IMAGE_H) |
---|
| 126 | SDL_FreeSurface(img->priv); |
---|
| 127 | #elif defined(HAVE_IMLIB2_H) |
---|
| 128 | imlib_context_set_image(img->priv); |
---|
| 129 | imlib_free_image(); |
---|
| 130 | #elif defined(HAVE_CV_H) |
---|
[389] | 131 | IplImage *iplimg; |
---|
| 132 | iplimg = (IplImage *)img->priv; |
---|
| 133 | cvReleaseImage(&iplimg); |
---|
| 134 | #endif |
---|
| 135 | |
---|
| 136 | free(img); |
---|
| 137 | } |
---|
| 138 | |
---|
[413] | 139 | void image_save(struct image *img, const char *name) |
---|
| 140 | { |
---|
| 141 | #if defined(HAVE_SDL_IMAGE_H) |
---|
| 142 | SDL_SaveBMP(img->priv, name); |
---|
| 143 | #elif defined(HAVE_IMLIB2_H) |
---|
| 144 | imlib_context_set_image(img->priv); |
---|
| 145 | imlib_save_image(name); |
---|
| 146 | #elif defined(HAVE_CV_H) |
---|
| 147 | cvSaveImage(name, img->priv); |
---|
| 148 | #endif |
---|
| 149 | } |
---|
| 150 | |
---|
[381] | 151 | int getgray(struct image *img, int x, int y, int *g) |
---|
| 152 | { |
---|
| 153 | if(x < 0 || y < 0 || x >= img->width || y >= img->height) |
---|
| 154 | { |
---|
| 155 | *g = 255; |
---|
| 156 | return -1; |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | *g = (unsigned char)img->pixels[y * img->pitch + x * img->channels + 1]; |
---|
| 160 | |
---|
| 161 | return 0; |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | int getpixel(struct image *img, int x, int y, int *r, int *g, int *b) |
---|
| 165 | { |
---|
| 166 | if(x < 0 || y < 0 || x >= img->width || y >= img->height) |
---|
| 167 | { |
---|
| 168 | *r = 255; |
---|
| 169 | *g = 255; |
---|
| 170 | *b = 255; |
---|
| 171 | return -1; |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | *b = (unsigned char)img->pixels[y * img->pitch + x * img->channels]; |
---|
| 175 | *g = (unsigned char)img->pixels[y * img->pitch + x * img->channels + 1]; |
---|
| 176 | *r = (unsigned char)img->pixels[y * img->pitch + x * img->channels + 2]; |
---|
| 177 | |
---|
| 178 | return 0; |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | int setpixel(struct image *img, int x, int y, int r, int g, int b) |
---|
| 182 | { |
---|
| 183 | if(x < 0 || y < 0 || x >= img->width || y >= img->height) |
---|
| 184 | return -1; |
---|
| 185 | |
---|
| 186 | img->pixels[y * img->pitch + x * img->channels] = b; |
---|
| 187 | img->pixels[y * img->pitch + x * img->channels + 1] = g; |
---|
| 188 | img->pixels[y * img->pitch + x * img->channels + 2] = r; |
---|
| 189 | |
---|
| 190 | return 0; |
---|
| 191 | } |
---|
| 192 | |
---|
[387] | 193 | void image_display(struct image *img) |
---|
[381] | 194 | { |
---|
[407] | 195 | #if defined(HAVE_SDL_IMAGE_H) |
---|
| 196 | //Nothing to do here |
---|
| 197 | #elif defined(HAVE_IMLIB2_H) |
---|
| 198 | //char name[BUFSIZ]; |
---|
| 199 | //static int i = 0; |
---|
| 200 | //sprintf(name, "image%i-%ix%i.png", i++, img->width, img->height); |
---|
| 201 | //imlib_context_set_image(img->priv); |
---|
| 202 | //imlib_save_image(name); |
---|
| 203 | //fprintf(stderr, "saved to %s\n", name); |
---|
| 204 | #elif defined(HAVE_CV_H) |
---|
[389] | 205 | char name[BUFSIZ]; |
---|
| 206 | sprintf(name, "Image %p (%i x %i)", img, img->width, img->height); |
---|
| 207 | cvNamedWindow(name, 0); |
---|
| 208 | cvShowImage(name, img->priv); |
---|
| 209 | cvResizeWindow(name, img->width * 2, img->height * 2 + 50); |
---|
| 210 | while((unsigned char)cvWaitKey(0) != 0x1b) |
---|
| 211 | ; |
---|
[381] | 212 | #endif |
---|
| 213 | } |
---|
| 214 | |
---|