Go to the documentation of this file.00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <memory.h>
00004 #include "ImageData.h"
00005 #ifdef _DEBUG
00006 #ifdef WIN32
00007 #include <crtdbg.h>
00008 #endif
00009 #endif
00010
00015 ImageData::ImageData(void) :
00016 m_width(0),
00017 m_height(0),
00018 m_image(0),
00019 m_cvImage(0),
00020 m_cvSimImage(0)
00021
00022 {
00023 }
00024
00029 ImageData::~ImageData(void)
00030 {
00031 DeleteImage();
00032 }
00033
00038 ImageData::ImageData(const ImageData& src)
00039 {
00040 m_image = 0;
00041 m_cvImage = 0;
00042 m_cvSimImage = 0;
00043
00044 CreateImage(src.m_width ,src.m_height);
00045 memcpy(m_image, src.m_image, src.m_width * src.m_height);
00046 }
00047
00053 ImageData ImageData::PyrDown()
00054 {
00055 ImageData result;
00056 unsigned long width, height;
00057
00058 unsigned long h_cnt, w_cnt;
00059 unsigned long x_pos, y_pos;
00060
00061 width = m_width / 2;
00062 height = m_height / 2;
00063 result.CreateImage(width, height);
00064
00065 for ( h_cnt = 0; h_cnt < height; h_cnt++ ) {
00066 for( w_cnt = 0; w_cnt < width; w_cnt++ ) {
00067 x_pos = (w_cnt * 2);
00068 y_pos = (h_cnt * 2) * m_width;
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085 result[ (int)(h_cnt * width + (width - w_cnt - 1)) ]
00086 = (unsigned int)( m_image[ y_pos + x_pos ] +
00087 m_image[ y_pos + x_pos + m_width] +
00088 m_image[ y_pos + x_pos + 1 ] +
00089 m_image[ y_pos + x_pos + 1 + m_width ] ) / 4;
00090 }
00091 }
00092
00093 return result;
00094 }
00095
00101 void ImageData::SetData(TimedOctetSeq orgData)
00102 {
00103 unsigned char *imgData;
00104
00105 DeleteImage();
00106
00107
00108 if(orgData.data.length() > 0){
00109 imgData = (unsigned char*)malloc(orgData.data.length());
00110 memcpy(imgData, (unsigned char*)(&(orgData.data[0])), orgData.data.length());
00111 m_cvSimImage = (IplImage *)imgData;
00112 m_cvSimImage->roi = NULL;
00113 m_cvSimImage->maskROI = NULL;
00114 m_cvSimImage->imageId = NULL;
00115 m_cvSimImage->tileInfo = NULL;
00116 m_cvSimImage->imageDataOrigin = NULL;
00117 m_cvSimImage->imageData = (char*)(&(imgData[sizeof(IplImage)]));
00118 SetData(m_cvSimImage);
00119 }
00120 }
00121
00127 void ImageData::CreateImage(const unsigned long width, const unsigned long height)
00128 {
00129 m_width = width;
00130 m_height = height;
00131
00132 DeleteImage();
00133 m_image = (unsigned char *)malloc( width * height );
00134 if(m_image == 0) {
00135 fprintf(stderr, "Can't malloc");
00136 }
00137 Clear(MAX_BRIGHTNESS);
00138 }
00139
00144 void ImageData::Clear(unsigned char value)
00145 {
00146 if(m_image != 0)
00147 memset(m_image, value, m_width * m_height);
00148 }
00149
00154 void ImageData::DeleteImage()
00155 {
00156 if(m_image != 0)
00157 free(m_image);
00158 m_image = 0;
00159
00160 if(m_cvImage != 0)
00161 cvReleaseImage(&m_cvImage);
00162 m_cvImage = 0;
00163
00164 if(m_cvSimImage)
00165 free(m_cvSimImage);
00166 m_cvSimImage = 0;
00167 }
00168
00177 int ImageData::LoadImage(std::string fileName, int flags)
00178 {
00179
00180 DeleteImage();
00181
00182 m_cvImage = cvLoadImage(fileName.c_str(), flags | CV_LOAD_IMAGE_ANYDEPTH);
00183 if(m_cvImage == 0) return -1;
00184 SetData(m_cvImage);
00185
00186 return 0;
00187 }
00188
00194 void ImageData::SetData(IplImage *cvImage)
00195 {
00196 IplImage *grayImage = 0;
00197 char *imageData;
00198
00199
00200 m_width = cvImage->width;
00201 m_height = cvImage->height;
00202 imageData = cvImage->imageData;
00203
00204 if((cvImage->width * cvImage->height) != cvImage->imageSize){
00205
00206
00207 if(cvImage->nChannels != 1){
00208
00209
00210 grayImage = cvCreateImage( cvGetSize(cvImage) , IPL_DEPTH_8U, 1 );
00211 cvCvtColor(cvImage, grayImage, CV_BGR2GRAY);
00212 imageData = grayImage->imageData;
00213 }
00214 else{
00215
00216 m_width = cvImage->widthStep;
00217 m_height = cvImage->height;
00218 }
00219
00220 }
00221
00222 m_image = (unsigned char *)malloc(m_width * m_height);
00223 memcpy(m_image, imageData, m_width * m_height);
00224 if(grayImage != 0) cvReleaseImage(&grayImage);
00225 }
00226
00231 ImageData& ImageData::operator=(const ImageData& org)
00232 {
00233 CreateImage(org.m_width, org.m_height);
00234 memcpy(m_image, org.m_image, org.m_width * org.m_height);
00235 return *this;
00236 }