00001
00017 #include "vetFrameGrey.h"
00018 #include <math.h>
00019
00023 vetFrameGrey::vetFrameGrey() : vetFrame()
00024 {
00025 data = NULL;
00026 width = 0;
00027 height = 0;
00028
00029 INFO("vetFrameGrey::vetFrameGrey() : vetFrame() [CONTRUCTOR]")
00030 }
00031
00037 vetFrameGrey::vetFrameGrey(unsigned int w, unsigned int h)
00038 {
00039 data = NULL;
00040 reAllocCanvas(w, h);
00041
00042 INFO("vetFrameGrey::vetFrameGrey(unsigned int w, unsigned int h) [CONTRUCTOR]")
00043 DEBUG(width)
00044 DEBUG(height)
00045 }
00046
00051 vetFrameGrey::vetFrameGrey(vetFrameGrey& img)
00052
00053 {
00054 data = NULL;
00055 reAllocCanvas(img.width, img.height);
00056
00057 if ( width != 0 && height != 0 )
00058 memcpy(data, img.data, width * height * sizeof(PixelGrey));
00059
00060 INFO("vetFrameGrey::vetFrameGrey(vetFrameGrey& img) [CONTRUCTOR]")
00061 DEBUG(width)
00062 DEBUG(height)
00063 }
00064
00065
00070 vetFrameGrey::vetFrameGrey(vetFrameYUV420& img)
00071 {
00072 INFO("vetFrameGrey::vetFrameGrey(vetFrameYUV420& img) [CONTRUCTOR]")
00073
00074 data = NULL;
00075 reAllocCanvas(img.width, img.height);
00076 *this << img;
00077
00078 DEBUG(width)
00079 DEBUG(height)
00080 }
00081
00086 vetFrameGrey::vetFrameGrey(vetFrameRGB24& img)
00087 {
00088 INFO("vetFrameGrey::vetFrameGrey(vetFrameRGB24& img) [CONTRUCTOR]")
00089
00090 data = NULL;
00091 reAllocCanvas(img.width, img.height);
00092 *this << img;
00093
00094 DEBUG(width)
00095 DEBUG(height)
00096 }
00097
00102 vetFrameGrey::vetFrameGrey(vetFrameRGB96& img)
00103 {
00104 INFO("vetFrameGrey::vetFrameGrey(vetFrameRGB96& img) [CONTRUCTOR]")
00105
00106 data = NULL;
00107 reAllocCanvas(img.width, img.height);
00108 *this << img;
00109
00110 DEBUG(width)
00111 DEBUG(height)
00112 }
00113
00118 vetFrameGrey::vetFrameGrey(vetFrameRGBA32& img)
00119 {
00120 INFO("vetFrameGrey::vetFrameGrey(vetFrameRGB32& img) [CONTRUCTOR]")
00121
00122 data = NULL;
00123 reAllocCanvas(img.width, img.height);
00124 *this << img;
00125
00126 DEBUG(width)
00127 DEBUG(height)
00128 }
00129
00130
00134 vetFrameGrey::~vetFrameGrey()
00135 {
00136 if ( data != NULL )
00137 delete [] data;
00138
00139 INFO("vetFrameGrey::~vetFrameGrey() [DESTRUCTOR]")
00140 }
00141
00142 VETRESULT vetFrameGrey::reAllocCanvas(unsigned int w, unsigned int h)
00143 {
00144 if (data != NULL)
00145 delete [] data;
00146
00147 height = h;
00148 width = w;
00149 data = NULL;
00150
00151 if ( width == 0 || height == 0)
00152 return VETRET_PARAM_ERR;
00153
00154 data = new PixelGrey[w * h];
00155
00156 return VETRET_OK;
00157 }
00158
00159 VETRESULT vetFrameGrey::extractBrightness(unsigned char* buffer, unsigned int* size)
00160 {
00161 if (width == 0 || height == 0 || data == NULL)
00162 return VETRET_ILLEGAL_USE;
00163
00164 if (buffer == NULL)
00165 {
00166 if ( size == NULL)
00167 return VETRET_PARAM_ERR;
00168
00169 *size = width*height;
00170 return VETRET_OK;
00171 }
00172
00173 memcpy (data, buffer, width*height);
00174 return VETRET_OK;
00175 }
00176
00177 VETRESULT vetFrameGrey::setWhite()
00178 {
00179 if (width == 0 || height == 0 || data == NULL)
00180 return VETRET_ILLEGAL_USE;
00181
00182 memset(data, 255, width * height);
00183 return VETRET_OK;
00184 }
00185
00186 VETRESULT vetFrameGrey::setBlack()
00187 {
00188 if (width == 0 || height == 0 || data == NULL)
00189 return VETRET_ILLEGAL_USE;
00190
00191 memset(data, 0, width * height);
00192
00193 return VETRET_OK;
00194 }
00195
00205 VETRESULT vetFrameGrey::setPixel(unsigned int x, unsigned int y, PixelGrey level)
00206 {
00207
00208 if ( x >= width || y >= height )
00209 return VETRET_PARAM_ERR;
00210
00211 data[y * width + x] = level;
00212
00213 return VETRET_OK;
00214 }
00215
00226 VETRESULT vetFrameGrey::setPixel(unsigned int x, unsigned int y, PixelRGB24 p)
00227 {
00228
00229 if ( x >= width || y >= height )
00230 return VETRET_PARAM_ERR;
00231
00232
00233 data[y * width + x] = (PixelGrey)((RED_COEF*(float)p[0]) +
00234 (GREEN_COEF*(float)p[1]) +
00235 (BLUE_COEF*(float)p[2]) );
00236 return VETRET_OK;
00237 }
00238
00248 VETRESULT vetFrameGrey::getPixel(unsigned int x, unsigned int y, PixelGrey& p) const
00249 {
00250
00251 if ( x >= width || y >= height )
00252 return VETRET_PARAM_ERR;
00253
00254 p = data[y * width + x];
00255 return VETRET_OK;
00256 }
00257
00258
00259 const PixelGrey& vetFrameGrey::getPixel(unsigned int x, unsigned int y) const
00260 {
00261
00262 if ( x >= width || y >= height )
00263 throw "Invalid Coordinates in method vetFrameRGB::getPixel(unsigned int x, unsigned int y, PixelGrey p)";
00264
00265 return data[y * width + x];
00266 }
00267
00268
00276 vetFrameGrey& vetFrameGrey::clearWith(PixelGrey p)
00277 {
00278 DEBUGMSG("vetFrameGrey& vetFrameGrey::clearWith(PixelGrey p)", p)
00279
00280 PixelGrey *piter = data;
00281
00282 for(unsigned int i=0; i < width * height; i++, piter++)
00283 *piter = p;
00284
00285 return *this;
00286 }
00287
00288 VETRESULT vetFrameGrey::setWidth(unsigned int newWidth)
00289 {
00290 if ( width != 0)
00291 return VETRET_ILLEGAL_USE;
00292
00293 width = newWidth;
00294
00295 return VETRET_OK;
00296 }
00297
00298 VETRESULT vetFrameGrey::setHeight(unsigned int newHeight)
00299 {
00300 if ( height != 0)
00301 return VETRET_ILLEGAL_USE;
00302
00303 height = newHeight;
00304
00305 return VETRET_OK;
00306 }
00307
00308 vetFrameGrey& vetFrameGrey::copy(vetFrameGrey& img)
00315 {
00316
00317 if (this == &img)
00318 throw "Source and Destination are same Image";
00319
00320
00321 if (width != img.width || height != img.height)
00322 throw "Difference in image dimensions";
00323
00324
00325 memcpy(data, img.data, width * height * sizeof(int));
00326
00327 return *this;
00328 }
00329
00330
00336 vetFrameGrey& vetFrameGrey::operator += (PixelGrey offset)
00337 {
00338 INFO("vetFrameGrey& vetFrameGrey::operator += (PixelGrey offset)")
00339
00340 PixelGrey *piter = data;
00341
00342 for(unsigned int i=0; i < width * height; i++, piter++)
00343 *piter += offset;
00344
00345 return *this;
00346 }
00347
00353 vetFrameGrey& vetFrameGrey::operator -= (PixelGrey offset)
00354 {
00355 INFO("vetFrameGrey& vetFrameGrey::operator -= (PixelGrey offset)")
00356
00357 PixelGrey *piter = data;
00358
00359 for(unsigned int i=0; i < width * height; i++, piter++)
00360 *piter -= offset;
00361
00362 return *this;
00363 }
00364
00370 vetFrameGrey& vetFrameGrey::operator *= (float factor)
00371 {
00372 INFO("vetFrameGrey& vetFrameGrey::operator *= (float factor)")
00373
00374 PixelGrey *piter = data;
00375
00376 for(unsigned int i=0; i < width * height; i++, piter++)
00377 *piter = (int)( ( (float) *piter ) * factor );
00378
00379 return *this;
00380 }
00381
00387 vetFrameGrey& vetFrameGrey::operator /= (float factor)
00388 {
00389 INFO("vetFrameGrey& vetFrameGrey::operator /= (float factor)")
00390
00391 PixelGrey *piter = data;
00392
00393 for(unsigned int i=0; i < width * height; i++, piter++)
00394 *piter = (int)(( (float) *piter ) / factor );
00395
00396 return *this;
00397 }
00398
00404 vetFrameGrey& vetFrameGrey::operator += (vetFrameGrey& img)
00405 {
00406 INFO("vetFrameGrey& vetFrameGrey::operator += (vetFrameGrey& img)")
00407
00408 if (width != img.width || height != img.height)
00409 throw "Difference in vetFrameRGB Dimensions";
00410
00411 for(unsigned int i=0; i < width * height; i++)
00412 data[i] += img.data[i];
00413
00414 return *this;
00415 }
00416
00422 vetFrameGrey& vetFrameGrey::operator -= (vetFrameGrey& img)
00423 {
00424 INFO("vetFrameGrey& vetFrameGrey::operator -= (vetFrameGrey& img)")
00425
00426 if (width != img.width || height != img.height)
00427 throw "Difference in vetFrameRGB Dimensions";
00428
00429 for(unsigned int i=0; i < width * height; i++)
00430 data[i] -= img.data[i];
00431
00432 return *this;
00433 }
00434
00435
00440 VETRESULT vetFrameGrey::invert()
00441 {
00442 INFO("void vetFrameGrey::absolute()")
00443
00444 PixelGrey *piter = data;
00445 for(unsigned int i=0; i < width * height; i++, piter++)
00446 *piter = 255 - *piter;
00447
00448 return VETRET_OK;
00449 }
00450
00456 VETRESULT vetFrameGrey::threshold(PixelGrey thresh)
00457 {
00458 PixelGrey *piter = data;
00459
00460 for(unsigned int i=0; i < width * height; i++, piter++)
00461 if ( *piter < thresh )
00462 *piter = 0;
00463 else
00464 *piter = 255;
00465
00466 return VETRET_OK;
00467 }
00468
00475 vetFrameGrey& vetFrameGrey::operator >> (vetFrameYUV420& img)
00476 {
00477 INFO("vetFrameGrey& vetFrameGrey::operator >> (vetFrameYUV420& img) [pushing data]")
00478
00479 if ( width == 0 || height == 0 )
00480 throw "Cannot do that with empty image (no size)";
00481
00482 if ( width != img.width || height != img.height)
00483 img.reAllocCanvas(width, height);
00484
00485
00486 memcpy(img.data, data, width * height);
00487 memset(img.U, '\0', width * height / 2);
00488
00489
00490
00491
00492
00493 return *this;
00494 }
00495
00502 vetFrameGrey& vetFrameGrey::operator >> (vetFrameRGB24& img)
00503 {
00504 INFO("vetFrameGrey& vetFrameGrey::operator >> (vetFrameRGB24& img) [pushing data]")
00505
00506 if ( width == 0 || height == 0 )
00507 throw "Cannot do that with empty image (no size)";
00508
00509 if ( width != img.width || height != img.height)
00510 img.reAllocCanvas(width, height);
00511
00512 const unsigned int size = width * height;
00513 for (unsigned int i=0; i < size; i++)
00514 {
00515 img.data[i][0] = data[i];
00516 img.data[i][1] = data[i];
00517 img.data[i][2] = data[i];
00518 }
00519
00520 return *this;
00521 }
00522
00529 vetFrameGrey& vetFrameGrey::operator >> (vetFrameRGB96& img)
00530 {
00531 INFO("vetFrameGrey& vetFrameGrey::operator >> (vetFrameRGB& img) [pushing data]")
00532
00533
00534 if ( width == 0 || height == 0 )
00535 throw "Cannot do that with empty image (no size)";
00536
00537 if ( width != img.width || height != img.height)
00538 img.reAllocCanvas(width, height);
00539
00540 const unsigned int size = width * height;
00541 for (unsigned int i=0; i < size; i++)
00542 {
00543 img.data[i][0] = (int) data[i];
00544 img.data[i][1] = (int) data[i];
00545 img.data[i][2] = (int) data[i];
00546 }
00547
00548 return *this;
00549 }
00550
00557 vetFrameGrey& vetFrameGrey::operator >> (vetFrameRGBA32& img)
00558 {
00559 INFO("vetFrameGrey& vetFrameGrey::operator >> (vetFrameRGB& img) [pushing data]")
00560
00561 if ( width == 0 || height == 0 )
00562 throw "Cannot do that with empty image (no size)";
00563
00564 if ( width != img.width || height != img.height)
00565 img.reAllocCanvas(width, height);
00566
00567
00568
00569 return *this;
00570 }
00571
00578 void vetFrameGrey::operator << (const vetFrameYUV420& img)
00579 {
00580 INFO("void vetFrameGrey::operator << (const vetFrameYUV420& img) [importing data]")
00581
00582 if ( img.width == 0 || img.height == 0 )
00583 throw "Cannot do that with empty image (no size)";
00584
00585 if ( width != img.width || height != img.height)
00586 reAllocCanvas(img.width, img.height);
00587
00588
00589
00590 memcpy(data, img.data, width * height);
00591
00592
00593
00594
00595
00596 }
00597
00604 void vetFrameGrey::operator << (const vetFrameRGB24& img)
00605
00606 {
00607 INFO("void vetFrameGrey::operator << (const vetFrameRGB24& img) [importing data]")
00608
00609 if ( img.width == 0 || img.height == 0 )
00610 throw "Cannot do that with empty image (no size)";
00611
00612 if ( width != img.width || height != img.height)
00613 reAllocCanvas(img.width, img.height);
00614
00615 const unsigned int size = width * height;
00616 for (unsigned int i=0; i < size; i++)
00617 data[i] = (PixelGrey) ( (RED_COEF *(float) img.data[i][0]) +
00618 (GREEN_COEF*(float) img.data[i][1]) +
00619 (BLUE_COEF *(float) img.data[i][2]) );
00620 }
00621
00622
00623
00630 void vetFrameGrey::operator << (const vetFrameRGB96& img)
00631 {
00632 INFO("void vetFrameGrey::operator << (const vetFrameRGB96& img) [importing data]")
00633
00634 if ( img.width == 0 || img.height == 0 )
00635 throw "Cannot do that with empty image (no size)";
00636
00637 if ( width != img.width || height != img.height)
00638 reAllocCanvas(img.width, img.height);
00639
00640 const unsigned int size = width * height;
00641 for (unsigned int i=0; i < size; i++)
00642 data[i] = (PixelGrey) ( (RED_COEF *(float) img.data[i][0]) +
00643 (GREEN_COEF*(float) img.data[i][1]) +
00644 (BLUE_COEF *(float) img.data[i][2]) );
00645 }
00646
00653 void vetFrameGrey::operator << (const vetFrameRGBA32& img)
00654 {
00655 INFO("void vetFrameGrey::operator << (const vetFrameRGB24& img) [importing data]")
00656
00657 if ( img.width == 0 || img.height == 0 )
00658 throw "Cannot do that with empty image (no size)";
00659
00660 if ( width != img.width || height != img.height)
00661 reAllocCanvas(img.width, img.height);
00662
00663
00664 }