Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

215 righe
5.5 KiB

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