You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

189 line
4.7 KiB

  1. /*
  2. * libpipi Proper image processing implementation library
  3. * Copyright (c) 2004-2008 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This library is free software. It comes without any warranty, to
  9. * the extent permitted by applicable law. You can redistribute it
  10. * and/or modify it under the terms of the Do What The Fuck You Want
  11. * To Public License, Version 2, as published by Sam Hocevar. See
  12. * http://sam.zoy.org/wtfpl/COPYING for more details.
  13. */
  14. /*
  15. * image.c: image I/O functions
  16. */
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include "config.h"
  21. #include "common.h"
  22. #if defined(HAVE_SDL_IMAGE_H)
  23. # include <SDL_image.h>
  24. #elif defined(HAVE_IMLIB2_H)
  25. # include <Imlib2.h>
  26. #elif defined(HAVE_CV_H)
  27. # include <cv.h>
  28. # include <highgui.h>
  29. #else
  30. # error "No imaging library"
  31. #endif
  32. #include "pipi_internals.h"
  33. #include "pipi.h"
  34. pipi_image_t *pipi_load(const char *name)
  35. {
  36. pipi_image_t *img;
  37. #if defined(HAVE_SDL_IMAGE_H)
  38. SDL_Surface *priv = IMG_Load(name);
  39. #elif defined(HAVE_IMLIB2_H)
  40. Imlib_Image priv = imlib_load_image(name);
  41. #elif defined(HAVE_CV_H)
  42. IplImage *priv = cvLoadImage(name, -1);
  43. #endif
  44. if(!priv)
  45. return NULL;
  46. #if defined(HAVE_SDL_IMAGE_H)
  47. if(priv->format->BytesPerPixel == 1)
  48. {
  49. img = pipi_new(priv->w, priv->h);
  50. SDL_BlitSurface(priv, NULL, img->priv, NULL);
  51. SDL_FreeSurface(priv);
  52. return img;
  53. }
  54. #endif
  55. img = (pipi_image_t *)malloc(sizeof(pipi_image_t));
  56. #if defined(HAVE_SDL_IMAGE_H)
  57. img->width = priv->w;
  58. img->height = priv->h;
  59. img->pitch = priv->pitch;
  60. img->channels = priv->format->BytesPerPixel;
  61. img->pixels = priv->pixels;
  62. #elif defined(HAVE_IMLIB2_H)
  63. imlib_context_set_image(priv);
  64. img->width = imlib_image_get_width();
  65. img->height = imlib_image_get_height();
  66. img->pitch = 4 * imlib_image_get_width();
  67. img->channels = 4;
  68. img->pixels = (char *)imlib_image_get_data();
  69. #elif defined(HAVE_CV_H)
  70. img->width = priv->width;
  71. img->height = priv->height;
  72. img->pitch = priv->widthStep;
  73. img->channels = priv->nChannels;
  74. img->pixels = priv->imageData;
  75. #endif
  76. img->priv = (void *)priv;
  77. return img;
  78. }
  79. pipi_image_t *pipi_new(int width, int height)
  80. {
  81. pipi_image_t *img;
  82. #if defined(HAVE_SDL_IMAGE_H)
  83. SDL_Surface *priv;
  84. Uint32 rmask, gmask, bmask, amask;
  85. # if SDL_BYTEORDER == SDL_BIG_ENDIAN
  86. rmask = 0xff000000;
  87. gmask = 0x00ff0000;
  88. bmask = 0x0000ff00;
  89. amask = 0x00000000;
  90. # else
  91. rmask = 0x000000ff;
  92. gmask = 0x0000ff00;
  93. bmask = 0x00ff0000;
  94. amask = 0x00000000;
  95. # endif
  96. priv = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32,
  97. rmask, gmask, bmask, amask);
  98. #elif defined(HAVE_IMLIB2_H)
  99. Imlib_Image priv = imlib_create_image(width, height);
  100. #elif defined(HAVE_CV_H)
  101. IplImage *priv = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 3);
  102. #endif
  103. if(!priv)
  104. return NULL;
  105. img = (pipi_image_t *)malloc(sizeof(pipi_image_t));
  106. #if defined(HAVE_SDL_IMAGE_H)
  107. img->width = priv->w;
  108. img->height = priv->h;
  109. img->pitch = priv->pitch;
  110. img->channels = priv->format->BytesPerPixel;
  111. img->pixels = priv->pixels;
  112. #elif defined(HAVE_IMLIB2_H)
  113. imlib_context_set_image(priv);
  114. img->width = imlib_image_get_width();
  115. img->height = imlib_image_get_height();
  116. img->pitch = 4 * imlib_image_get_width();
  117. img->channels = 4;
  118. img->pixels = (char *)imlib_image_get_data();
  119. #elif defined(HAVE_CV_H)
  120. img->width = priv->width;
  121. img->height = priv->height;
  122. img->pitch = priv->widthStep;
  123. img->channels = priv->nChannels;
  124. img->pixels = priv->imageData;
  125. #endif
  126. img->priv = (void *)priv;
  127. return img;
  128. }
  129. pipi_image_t *pipi_copy(pipi_image_t const *img)
  130. {
  131. pipi_image_t *dst;
  132. int x, y;
  133. dst = pipi_new(img->width, img->height);
  134. for(y = 0; y < img->height; y++)
  135. {
  136. for(x = 0; x < img->width; x++)
  137. {
  138. int r, g, b;
  139. pipi_getpixel(img, x, y, &r, &g, &b);
  140. pipi_setpixel(dst, x, y, r, g, b);
  141. }
  142. }
  143. return dst;
  144. }
  145. void pipi_free(pipi_image_t *img)
  146. {
  147. #if defined(HAVE_SDL_IMAGE_H)
  148. SDL_FreeSurface(img->priv);
  149. #elif defined(HAVE_IMLIB2_H)
  150. imlib_context_set_image(img->priv);
  151. imlib_free_image();
  152. #elif defined(HAVE_CV_H)
  153. IplImage *iplimg;
  154. iplimg = (IplImage *)img->priv;
  155. cvReleaseImage(&iplimg);
  156. #endif
  157. free(img);
  158. }
  159. void pipi_save(pipi_image_t *img, const char *name)
  160. {
  161. #if defined(HAVE_SDL_IMAGE_H)
  162. SDL_SaveBMP(img->priv, name);
  163. #elif defined(HAVE_IMLIB2_H)
  164. imlib_context_set_image(img->priv);
  165. imlib_save_image(name);
  166. #elif defined(HAVE_CV_H)
  167. cvSaveImage(name, img->priv);
  168. #endif
  169. }