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.
 
 
 
 
 
 

141 line
3.4 KiB

  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <stdlib.h>
  4. #include "config.h"
  5. #include "common.h"
  6. #if defined(HAVE_IMLIB2_H)
  7. # include <Imlib2.h>
  8. #elif defined(HAVE_CV_H)
  9. # include <cv.h>
  10. # include <highgui.h>
  11. #else
  12. # error "No imaging library"
  13. #endif
  14. struct image * load_image(char *name)
  15. {
  16. struct image * img;
  17. #if defined(HAVE_IMLIB2_H)
  18. Imlib_Image priv = imlib_load_image(name);
  19. #elif defined(HAVE_CV_H)
  20. IplImage * priv = cvLoadImage(name, -1);
  21. #endif
  22. if(!priv)
  23. return NULL;
  24. img = malloc(sizeof(struct image));
  25. #if defined(HAVE_IMLIB2_H)
  26. imlib_context_set_image(priv);
  27. img->width = imlib_image_get_width();
  28. img->height = imlib_image_get_height();
  29. img->pitch = 4 * imlib_image_get_width();
  30. img->channels = 4;
  31. img->pixels = (char *)imlib_image_get_data();
  32. #elif defined(HAVE_CV_H)
  33. img->width = priv->width;
  34. img->height = priv->height;
  35. img->pitch = priv->widthStep;
  36. img->channels = priv->nChannels;
  37. img->pixels = priv->imageData;
  38. #endif
  39. img->priv = (void *)priv;
  40. return img;
  41. }
  42. struct image * new_image(int width, int height)
  43. {
  44. struct image * img;
  45. #if defined(HAVE_IMLIB2_H)
  46. Imlib_Image priv = imlib_create_image(width, height);
  47. #elif defined(HAVE_CV_H)
  48. IplImage * priv = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 3);
  49. #endif
  50. if(!priv)
  51. return NULL;
  52. img = malloc(sizeof(struct image));
  53. #if defined(HAVE_IMLIB2_H)
  54. imlib_context_set_image(priv);
  55. img->width = imlib_image_get_width();
  56. img->height = imlib_image_get_height();
  57. img->pitch = 4 * imlib_image_get_width();
  58. img->channels = 4;
  59. img->pixels = (char *)imlib_image_get_data();
  60. #elif defined(HAVE_CV_H)
  61. img->width = priv->width;
  62. img->height = priv->height;
  63. img->pitch = priv->widthStep;
  64. img->channels = priv->nChannels;
  65. img->pixels = priv->imageData;
  66. #endif
  67. img->priv = (void *)priv;
  68. return img;
  69. }
  70. int getgray(struct image *img, int x, int y, int *g)
  71. {
  72. if(x < 0 || y < 0 || x >= img->width || y >= img->height)
  73. {
  74. *g = 255;
  75. return -1;
  76. }
  77. *g = (unsigned char)img->pixels[y * img->pitch + x * img->channels + 1];
  78. return 0;
  79. }
  80. int getpixel(struct image *img, int x, int y, int *r, int *g, int *b)
  81. {
  82. if(x < 0 || y < 0 || x >= img->width || y >= img->height)
  83. {
  84. *r = 255;
  85. *g = 255;
  86. *b = 255;
  87. return -1;
  88. }
  89. *b = (unsigned char)img->pixels[y * img->pitch + x * img->channels];
  90. *g = (unsigned char)img->pixels[y * img->pitch + x * img->channels + 1];
  91. *r = (unsigned char)img->pixels[y * img->pitch + x * img->channels + 2];
  92. return 0;
  93. }
  94. int setpixel(struct image *img, int x, int y, int r, int g, int b)
  95. {
  96. if(x < 0 || y < 0 || x >= img->width || y >= img->height)
  97. return -1;
  98. img->pixels[y * img->pitch + x * img->channels] = b;
  99. img->pixels[y * img->pitch + x * img->channels + 1] = g;
  100. img->pixels[y * img->pitch + x * img->channels + 2] = r;
  101. return 0;
  102. }
  103. void display_image(struct image *img)
  104. {
  105. char name[BUFSIZ];
  106. #if defined(HAVE_IMLIB2_H)
  107. //static int i = 0;
  108. //sprintf(name, "image%i-%ix%i.png", i++, img->width, img->height);
  109. //imlib_context_set_image(img->priv);
  110. //imlib_save_image(name);
  111. //fprintf(stderr, "saved to %s\n", name);
  112. #elif defined(HAVE_CV_H)
  113. sprintf(name, "Image %p (%i x %i)", img, img->width, img->height);
  114. cvNamedWindow(name, 0);
  115. cvShowImage(name, img->priv);
  116. cvResizeWindow(name, 320, 120);
  117. #endif
  118. }