Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

114 rader
2.8 KiB

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