您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * libpipi Pathetic image processing interface 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 "config.h"
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <cv.h>
  22. #include <highgui.h>
  23. #include "pipi.h"
  24. #include "pipi_internals.h"
  25. /* FIXME: this whole file is broken until we support BGR24 images */
  26. static int pipi_free_opencv(pipi_image_t *);
  27. pipi_image_t *pipi_load_opencv(const char *name)
  28. {
  29. pipi_image_t *img;
  30. IplImage *priv = cvLoadImage(name, 1);
  31. if(!priv)
  32. return NULL;
  33. img = pipi_new(priv->width, priv->height);
  34. img->p[PIPI_PIXELS_BGR_U8].pixels = priv->imageData;
  35. img->p[PIPI_PIXELS_BGR_U8].w = priv->width;
  36. img->p[PIPI_PIXELS_BGR_U8].h = priv->height;
  37. img->p[PIPI_PIXELS_BGR_U8].pitch = priv->widthStep;
  38. img->p[PIPI_PIXELS_BGR_U8].bpp = 24;
  39. img->p[PIPI_PIXELS_BGR_U8].bytes = 3 * img->w * img->h;
  40. img->last_modified = PIPI_PIXELS_BGR_U8;
  41. img->codec_priv = (void *)priv;
  42. img->codec_format = PIPI_PIXELS_BGR_U8;
  43. img->codec_free = pipi_free_opencv;
  44. img->wrap = 0;
  45. img->u8 = 1;
  46. return img;
  47. }
  48. int pipi_save_opencv(pipi_image_t *img, const char *name)
  49. {
  50. if(!img->codec_priv)
  51. {
  52. IplImage *priv = cvCreateImage(cvSize(img->w, img->h),
  53. IPL_DEPTH_8U, 3);
  54. /* FIXME: check pitch differences here */
  55. if(img->last_modified == PIPI_PIXELS_BGR_U8)
  56. {
  57. memcpy(priv->imageData, img->p[PIPI_PIXELS_BGR_U8].pixels,
  58. 3 * img->w * img->h);
  59. free(img->p[PIPI_PIXELS_BGR_U8].pixels);
  60. }
  61. img->p[PIPI_PIXELS_BGR_U8].pixels = priv->imageData;
  62. img->p[PIPI_PIXELS_BGR_U8].w = priv->width;
  63. img->p[PIPI_PIXELS_BGR_U8].h = priv->height;
  64. img->p[PIPI_PIXELS_BGR_U8].pitch = priv->widthStep;
  65. img->p[PIPI_PIXELS_BGR_U8].bpp = 24;
  66. img->p[PIPI_PIXELS_BGR_U8].bytes = 3 * img->w * img->h;
  67. img->codec_priv = (void *)priv;
  68. img->codec_format = PIPI_PIXELS_BGR_U8;
  69. img->codec_free = pipi_free_opencv;
  70. img->wrap = 0;
  71. img->u8 = 1;
  72. }
  73. pipi_set_colorspace(img, img->codec_format);
  74. cvSaveImage(name, img->codec_priv);
  75. return 0;
  76. }
  77. /*
  78. * XXX: The following functions are local.
  79. */
  80. static int pipi_free_opencv(pipi_image_t *img)
  81. {
  82. IplImage *iplimg;
  83. iplimg = (IplImage *)img->codec_priv;
  84. cvReleaseImage(&iplimg);
  85. return 0;
  86. }