stereobuffer.cpp
上传用户:fengshi120
上传日期:2014-07-17
资源大小:6155k
文件大小:4k
源码类别:

3D图形编程

开发平台:

C/C++

  1. /*************************************************************************** 
  2. *
  3. * Copyright 2000 by David Demirdjian.   All rights reserved. 
  4. *  
  5. * Developed  by David Demirdjian
  6. *  
  7. * Permission to use, copy, or modify this software and  its documentation 
  8. * for  educational  and  research purposes only and without fee  is hereby 
  9. * granted, provided  that this copyright notice and the original authors's 
  10. * names appear  on all copies and supporting documentation.  If individual 
  11. * files are  separated from  this  distribution directory  structure, this 
  12. * copyright notice must be included.  For any other uses of this software, 
  13. * in original or  modified form, including but not limited to distribution 
  14. * in whole or in  part, specific  prior permission  must be  obtained from 
  15. * MIT.  These programs shall not  be  used, rewritten, or  adapted as  the 
  16. * basis  of  a  commercial  software  or  hardware product  without  first 
  17. * obtaining appropriate  licenses from David Demirdjian.  The author makes 
  18. * no representations about the suitability of this software for any purpose.  
  19. * It is provided "as is" without express or implied warranty. 
  20. *  
  21. **************************************************************************/
  22. #include "stdafx.h"
  23. #include "stereoMatching.h"
  24. #include "processingMMX.h"
  25. #include <math.h>
  26. typedef unsigned char uchar;
  27. typedef unsigned short ushort;
  28. #define ALLOC_ALIGN_MEMORY(X, X_origin, type, size) (X_origin)=((type*)malloc((size)*sizeof(type)+127)); (X) = (type*)((((unsigned int)(X_origin))+127) & (~127));
  29. #define DELETENULL(object) if (object) {delete object;object = NULL;}
  30. #define FREENULL(object) if (object) {free(object); object = NULL;}
  31. #ifdef _DEBUG
  32. #define new DEBUG_NEW
  33. #undef THIS_FILE
  34. static char THIS_FILE[] = __FILE__;
  35. #endif
  36. StereoBuffer::StereoBuffer()
  37. {
  38. Depth16_origin = NULL;
  39. imDepth_ref_origin = NULL;
  40. count_non_null_pixels_origin = NULL;
  41. corrScore_origin = NULL;
  42. corrScoreSec_origin = NULL;
  43. buffTemp_origin = NULL;
  44. bigBuffer16_origin = NULL;
  45. bigBuffer_origin = NULL;
  46. buff = NULL;
  47. buff16 = NULL;
  48. }
  49. StereoBuffer::~StereoBuffer()
  50. {
  51. deAlloc();
  52. }
  53. void StereoBuffer::setSize(int width, int height, int maxNbDepth)
  54. {
  55. //int f = pow(2, scale);
  56. this->width = width;
  57. this->height= height;
  58. this->maxNbDepth = maxNbDepth;
  59. }
  60. void StereoBuffer::alloc()
  61. {
  62. deAlloc();
  63. // stereo grow
  64. ALLOC_ALIGN_MEMORY(Depth16, Depth16_origin, ushort, width*height);
  65. ALLOC_ALIGN_MEMORY(imDepth_ref, imDepth_ref_origin, uchar, width*height);
  66. ALLOC_ALIGN_MEMORY(count_non_null_pixels, count_non_null_pixels_origin, uchar, width*height);
  67. ALLOC_ALIGN_MEMORY(corrScore, corrScore_origin, uchar, width*height);
  68. ALLOC_ALIGN_MEMORY(corrScoreSec, corrScoreSec_origin, uchar, width*height);
  69. // initialization of buffers ... with 64 bits alignment
  70. ALLOC_ALIGN_MEMORY(buffTemp, buffTemp_origin, ushort, 2*width*height)
  71. buffStep = width*height;
  72. ALLOC_ALIGN_MEMORY(bigBuffer, bigBuffer_origin, uchar, buffStep*maxNbDepth);
  73. buff = new uchar*[maxNbDepth];
  74. for (int i=0; i<maxNbDepth; ++i)
  75. {
  76. buff[i] = bigBuffer+ i*buffStep;
  77. }
  78. allocTempBuffer16();
  79. }
  80. void StereoBuffer::deAlloc()
  81. {
  82. deAllocTempBuffer16();
  83. FREENULL(bigBuffer_origin);
  84. FREENULL(buffTemp_origin);
  85. if(buff)
  86. {
  87. delete[] buff;
  88. buff = NULL;
  89. }
  90. FREENULL(Depth16_origin);
  91. FREENULL(imDepth_ref_origin);
  92. FREENULL(count_non_null_pixels_origin);
  93. FREENULL(corrScore_origin);
  94. FREENULL(corrScoreSec_origin);
  95. }
  96. void StereoBuffer::allocTempBuffer16()
  97. {
  98. ALLOC_ALIGN_MEMORY(bigBuffer16, bigBuffer16_origin, ushort, width*height*maxNbDepth);
  99. buff16 = new ushort*[maxNbDepth];
  100. for (int i=0; i<maxNbDepth; ++i) 
  101. buff16[i]= bigBuffer16+ i*(width*height);
  102. }
  103. void StereoBuffer::deAllocTempBuffer16()
  104. {
  105. FREENULL(bigBuffer16_origin);
  106. if(buff16)
  107. {
  108. delete[] buff16;
  109. buff16 = NULL;
  110. }
  111. }
  112. uchar* StereoBuffer::getCorrScores() const
  113. {
  114. return corrScore;
  115. }
  116. uchar* StereoBuffer::getCorrScores_Sec() const
  117. {
  118. return corrScoreSec;
  119. }