/trunk/ui-cvimage.c

https://gitlab.com/BGCX067/eyetracker-svn-to-git · C · 186 lines · 130 code · 42 blank · 14 comment · 24 complexity · a963db723a58a1261a1dbce37db39360 MD5 · raw file

  1. /*
  2. * Eyetracker - Copyright 2010 by Alex Barry ( alex DOT barry AT gmail DOT com )
  3. * Eye Tracking Using OpenCV and Rendered using SDL
  4. * LGPL License
  5. *
  6. * This software is distributed "as-is" without any warranty of any kind.
  7. * You are granted permission to:
  8. * - Redistribute/Change this softare..
  9. * - As long as you make a reference to this project, and clearly mark what has been changed
  10. * - Use portions of this software...
  11. * - As long as you give myself and other authors of this software credit
  12. * Please read the included LICENSE file for more information.
  13. */
  14. #include "ui-cvimage.h"
  15. ui_widget *ui_newcvimage( char *name, int w, int h, int depth, int channels ) {
  16. ui_cvimage *image = (ui_cvimage *)malloc( sizeof( ui_cvimage ) );
  17. image->image = cvCreateImage( cvSize( w, h ), depth, channels );
  18. image->gammaLutMatrix = cvCreateMatHeader( 1, 256, CV_8UC1 );
  19. image->briConLutMatrix = cvCreateMatHeader( 1, 256, CV_8UC1 );
  20. ui_widget *widget = ui_newwidget( name, UI_CVIMAGE_TYPE, image );
  21. widget->onDestroy = &__deletecvimage;
  22. widget->onRedraw = &__redrawcvimage;
  23. ui_setwidgetsize( widget, w, h );
  24. return widget;
  25. }
  26. ui_widget *ui_newcvimagefrom( char *name, IplImage *source, int depth, int channels ) {
  27. ui_widget *widget = ui_newcvimage( name, source->width, source->height, source->depth, source->nChannels );
  28. ui_copycvimagefrom( widget, source );
  29. return widget;
  30. }
  31. ui_widget *ui_newcvimageload( char *name, char *path ) {
  32. ui_cvimage *image = (ui_cvimage *)malloc( sizeof( ui_cvimage ) );
  33. image->image = cvLoadImage( (const char *)path, CV_LOAD_IMAGE_UNCHANGED );
  34. ui_widget *widget = ui_newwidget( name, UI_CVIMAGE_TYPE, image );
  35. widget->onDestroy = &__deletecvimage;
  36. widget->onRedraw = &__redrawcvimage;
  37. ui_setwidgetsize( widget, image->image->width, image->image->height );
  38. return widget;
  39. }
  40. void __deletecvimage( ui_widget *widget ) {
  41. ui_cvimage *image = UI_CAST_CVIMAGE( widget );
  42. if( !image ) return;
  43. image->gammaLutMatrix = cvCreateMatHeader( 1, 256, CV_8UC1 );
  44. image->briConLutMatrix = cvCreateMatHeader( 1, 256, CV_8UC1 );
  45. cvReleaseMatHeader( &image->gammaLutMatrix );
  46. cvReleaseMatHeader( &image->briConLutMatrix );
  47. if( image->image ) cvReleaseImage( &image->image );
  48. }
  49. void __redrawcvimage( ui_widget *widget ) {
  50. ui_cvimage *image = UI_CAST_CVIMAGE( widget );
  51. if( !image ) return;
  52. IplImage *tconv = NULL;
  53. int bpp = image->image->depth * image->image->nChannels;
  54. if( bpp < 24 ) {
  55. tconv = cvCreateImage( cvGetSize( image->image ), 8, 3 );
  56. cvCvtColor( image->image, tconv, CV_GRAY2RGB );
  57. cvReleaseImage( &tconv );
  58. }
  59. SDL_Surface *tmp = SDL_CreateRGBSurfaceFrom( (void *)image->image->imageData,
  60. image->image->width,
  61. image->image->height,
  62. image->image->depth*image->image->nChannels,
  63. image->image->widthStep,
  64. 0xff0000, 0x00ff00, 0x0000ff, 0 );
  65. SDL_Surface *surface = SDL_CreateRGBSurface( 0, image->image->width, image->image->height, 24, 0, 0, 0, 0 );
  66. SDL_BlitSurface( tmp, NULL, surface, NULL );
  67. if( widget->texture ) SDL_DestroyTexture( widget->texture );
  68. widget->texture = SDL_CreateTextureFromSurface( 0, surface );
  69. SDL_FreeSurface( surface );
  70. SDL_FreeSurface( tmp );
  71. }
  72. // The following two functions were taken from the EyeWriter project's source code, and have been slightly modified
  73. void ui_applycvimagegamma( ui_widget *widget, float gamma ) {
  74. ui_cvimage *image = UI_CAST_CVIMAGE( widget );
  75. if( !image ) return;
  76. double minval;
  77. double maxval;
  78. if( image->image ) {
  79. cvMinMaxLoc( image->image, &minval, &maxval, NULL, NULL, NULL );
  80. }
  81. float spread = (float)(maxval - minval);
  82. int i = 0;
  83. for( i = 0; i < 256; i++ ){
  84. float pct = (float)( i - minval ) / spread;
  85. if( pct < 0 ) pct = 0;
  86. if( pct > 1 ) pct = 1;
  87. image->gammaLut[i] = (int)( 255.0 * powf( pct, gamma ) );
  88. }
  89. cvSetData( image->gammaLutMatrix, image->gammaLut, 0 );
  90. cvLUT( image->image, image->image, image->gammaLutMatrix);
  91. }
  92. void ui_applycvimagebc( ui_widget *widget, float brightness, float contrast ) {
  93. ui_cvimage *image = UI_CAST_CVIMAGE( widget );
  94. if( !image ) return;
  95. int i;
  96. if( contrast > 0 ) {
  97. double delta = 127. * contrast;
  98. double a = 255. / ( 255. - delta * 2 );
  99. double b = a * ( brightness * 100 - delta );
  100. for( i = 0; i < 256; i++ ) {
  101. int v = cvRound( a * i + b );
  102. if( v < 0 ) v = 0;
  103. if( v > 255 ) v = 255;
  104. image->briConLut[i] = (uchar)v;
  105. }
  106. } else {
  107. double delta = -128. * contrast;
  108. double a = ( 256. - delta * 2 ) / 255.;
  109. double b = a * brightness * 100. + delta;
  110. for( i = 0; i < 256; i++ ) {
  111. int v = cvRound( a * i + b);
  112. if( v < 0 ) v = 0;
  113. if( v > 255 ) v = 255;
  114. image->briConLut[i] = (uchar)v;
  115. }
  116. }
  117. cvSetData( image->briConLutMatrix, image->briConLut, 0 );
  118. cvLUT( image->image, image->image, image->briConLutMatrix);
  119. }
  120. void ui_getcvimagepicture( ui_widget *widget, IplImage **picture ) {
  121. ui_cvimage *image = UI_CAST_CVIMAGE( widget );
  122. if( !image ) return;
  123. *picture = image->image;
  124. }
  125. void ui_copycvimagefrom( ui_widget *widget, IplImage *source ) {
  126. ui_cvimage *image = UI_CAST_CVIMAGE( widget );
  127. if( !image ) return;
  128. cvConvertImage( source, image->image, 0 );
  129. }
  130. void ui_dilatecvimage( ui_widget *widget, int passes ) {
  131. ui_cvimage *image = UI_CAST_CVIMAGE( widget );
  132. if( !image ) return;
  133. cvDilate( image->image, image->image, NULL, passes );
  134. }
  135. void ui_convertcvimagethreshold( ui_widget *widget, double threshold, double maxvalue, int type ) {
  136. ui_cvimage *image = UI_CAST_CVIMAGE( widget );
  137. if( !image ) return;
  138. cvThreshold( image->image, image->image, threshold, maxvalue, type );
  139. }
  140. void ui_flipcvimage( ui_widget *widget, int flipmode ) {
  141. ui_cvimage *image = UI_CAST_CVIMAGE( widget );
  142. if( !image ) return;
  143. cvFlip( image->image, image->image, flipmode );
  144. }