00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include <cassert>
00011
00012 #include "pipeline/Image.h"
00013 #include "pipeline/ImagePixelFormats.h"
00014
00015
00016
00017
00018
00019
00020
00021
00022 DWORD TImageARGB::GetPixelSize(void) const
00023 {
00024 return 4;
00025 }
00026
00027
00028
00029
00030 DWORD TImageARGB::GetWidth(void) const
00031 {
00032 return m_width;
00033 }
00034
00035
00036
00037
00038 DWORD TImageARGB::GetHeight(void) const
00039 {
00040 return m_height;
00041 }
00042
00043
00044
00045
00046 DWORD TImageARGB::GetPitch(void) const
00047 {
00048 return m_width * 4;
00049 }
00050
00051
00052
00053
00054 DWORD TImageARGB::GetDataSize(void) const
00055 {
00056 return m_width * m_height * 4;
00057 }
00058
00059
00060
00061
00062 const void* TImageARGB::GetData(void) const
00063 {
00064 return m_data;
00065 }
00066
00067
00068
00069
00070 TImageARGB::~TImageARGB(void)
00071 {
00072 delete [] m_data;
00073 m_data = NULL;
00074 }
00075
00076
00077
00078
00079
00080
00081
00082 TImageARGB::TImageARGB(DWORD width, DWORD height)
00083 {
00084 m_width = width;
00085 m_height = height;
00086
00087 m_data = new unsigned char[ width * height * 4];
00088 }
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098 void TImageARGB::FromGray( const TImageGray * image )
00099 {
00100 if (
00101 this->GetWidth() != image->GetWidth() ||
00102 this->GetHeight() != image->GetHeight()
00103 )
00104 {
00105
00106 return;
00107 }
00108
00109 const unsigned char * data = (const unsigned char*)image->GetData();
00110 const DWORD pixelCount = this->GetWidth() * this->GetHeight();
00111
00112 for ( DWORD i = 0 ; i < pixelCount ; i++ )
00113 {
00114 unsigned char * pixel = (unsigned char*)(&((DWORD*)m_data)[i]);
00115
00116 pixel[0] = data[i];
00117 pixel[1] = data[i];
00118 pixel[2] = data[i];
00119 pixel[3] = 255;
00120 }
00121 }
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133 void TImageARGB::FromRGB( const TImageRGB * image )
00134 {
00135 if (
00136 this->GetWidth() != image->GetWidth() ||
00137 this->GetHeight() != image->GetHeight()
00138 )
00139 {
00140
00141 return;
00142 }
00143
00144 const unsigned char * src = (const unsigned char*)image->GetData();
00145 const DWORD byteCount = this->GetDataSize();
00146
00147 DWORD offsetSrc = 0;
00148 DWORD offsetDst = 0;
00149
00150 while ( offsetDst < byteCount )
00151 {
00152 m_data[ offsetDst++ ] = src[ offsetSrc++ ];
00153 m_data[ offsetDst++ ] = src[ offsetSrc++ ];
00154 m_data[ offsetDst++ ] = src[ offsetSrc++ ];
00155 m_data[ offsetDst++ ] = 255;
00156 }
00157 }
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173 void TImageARGB::CopyFrom( const TImageARGB * src, DWORD x, DWORD y, DWORD width, DWORD height)
00174 {
00175 if (
00176 ( width > 0 ) && ( height > 0 ) &&
00177 src->IsRectInside( x, y, width, height ) &&
00178 width <= m_width &&
00179 height <= m_height
00180 )
00181 {
00182
00183
00184 const DWORD * tmpDataSrc = (DWORD*) src->m_data;
00185 DWORD * tmpDataDst = (DWORD*) this->m_data;
00186
00187 for ( DWORD dy = 0 ; dy < height ; dy++ )
00188 {
00189 const DWORD * startSrc = &tmpDataSrc[ (y + dy) * src->m_width + x ];
00190 DWORD * startDst = &tmpDataDst[ dy * m_width ];
00191
00192 memcpy( startDst, startSrc, sizeof( DWORD ) * width );
00193 }
00194 }
00195 else
00196 {
00197
00198 }
00199 }
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210 DWORD TImageRGB::GetPixelSize(void) const
00211 {
00212 return 3;
00213 }
00214
00215
00216
00217
00218 DWORD TImageRGB::GetWidth(void) const
00219 {
00220 return m_width;
00221 }
00222
00223
00224
00225
00226 DWORD TImageRGB::GetHeight(void) const
00227 {
00228 return m_height;
00229 }
00230
00231
00232
00233
00234 DWORD TImageRGB::GetPitch(void) const
00235 {
00236 return m_width * 3;
00237 }
00238
00239
00240
00241
00242 DWORD TImageRGB::GetDataSize(void) const
00243 {
00244 return m_width * m_height * 3;
00245 }
00246
00247
00248
00249
00250 const void* TImageRGB::GetData(void) const
00251 {
00252 return m_data;
00253 }
00254
00255
00256
00257
00258 TImageRGB::~TImageRGB(void)
00259 {
00260 delete [] m_data;
00261 m_data = NULL;
00262 }
00263
00264
00265
00266
00267
00268
00269
00270 TImageRGB::TImageRGB(DWORD width, DWORD height)
00271 {
00272 m_width = width;
00273 m_height = height;
00274
00275 m_data = new unsigned char[ width * height * 3];
00276 }
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286 void TImageRGB::FromGray( const TImageGray * image )
00287 {
00288 if (
00289 this->GetWidth() != image->GetWidth() ||
00290 this->GetHeight() != image->GetHeight()
00291 )
00292 {
00293
00294 return;
00295 }
00296
00297 const unsigned char * data = (const unsigned char*)image->GetData();
00298 const DWORD pixelCount = this->GetWidth() * this->GetHeight();
00299
00300 DWORD offsetDst = 0;
00301
00302 for ( DWORD i = 0 ; i < pixelCount ; i++ )
00303 {
00304 m_data[ offsetDst++ ] = data[i];
00305 m_data[ offsetDst++ ] = data[i];
00306 m_data[ offsetDst++ ] = data[i];
00307 }
00308 }
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318 void TImageRGB::FromARGB( const TImageARGB * image )
00319 {
00320 if (
00321 this->GetWidth() != image->GetWidth() ||
00322 this->GetHeight() != image->GetHeight()
00323 )
00324 {
00325
00326 return;
00327 }
00328
00329 const unsigned char * src = (const unsigned char*)image->GetData();
00330 const DWORD byteCount = this->GetDataSize();
00331
00332 DWORD offsetSrc = 0;
00333 DWORD offsetDst = 0;
00334
00335 while ( offsetDst < byteCount )
00336 {
00337 m_data[ offsetDst++ ] = src[ offsetSrc++ ];
00338 m_data[ offsetDst++ ] = src[ offsetSrc++ ];
00339 m_data[ offsetDst++ ] = src[ offsetSrc++ ];
00340
00341 offsetSrc++;
00342 }
00343 }
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359 void TImageRGB::CopyFrom( const TImageRGB * src, DWORD x, DWORD y, DWORD width, DWORD height)
00360 {
00361 if (
00362 ( width > 0 ) && ( height > 0 ) &&
00363 src->IsRectInside( x, y, width, height ) &&
00364 width <= m_width &&
00365 height <= m_height
00366 )
00367 {
00368
00369
00370 const unsigned char * tmpDataSrc = (unsigned char*) src->m_data;
00371 unsigned char * tmpDataDst = (unsigned char*) this->m_data;
00372
00373 const DWORD pitchSrc = src->GetPitch();
00374 const DWORD pitchDst = this->GetPitch();
00375
00376 DWORD offsetSrc = ( y * src->m_width + x ) * 3;
00377 DWORD offsetDst = 0;
00378
00379 for ( DWORD dy = 0 ; dy < height ; dy++ )
00380 {
00381 const unsigned char * startSrc = &tmpDataSrc[ offsetSrc ];
00382 unsigned char * startDst = &tmpDataDst[ offsetDst ];
00383
00384 offsetSrc += pitchSrc;
00385 offsetDst += pitchDst;
00386
00387 memcpy( startDst, startSrc, sizeof( DWORD ) * width );
00388 }
00389 }
00390 else
00391 {
00392
00393 }
00394 }
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405 DWORD TImageGray::GetPixelSize(void) const
00406 {
00407 return 1;
00408 }
00409
00410
00411
00412
00413 DWORD TImageGray::GetWidth(void) const
00414 {
00415 return m_width;
00416 }
00417
00418
00419
00420
00421 DWORD TImageGray::GetHeight(void) const
00422 {
00423 return m_height;
00424 }
00425
00426
00427
00428
00429 DWORD TImageGray::GetPitch(void) const
00430 {
00431 return m_width * 1;
00432 }
00433
00434
00435
00436
00437 DWORD TImageGray::GetDataSize(void) const
00438 {
00439 return m_width * m_height * 1;
00440 }
00441
00442
00443
00444
00445 const void* TImageGray::GetData(void) const
00446 {
00447 return m_data;
00448 }
00449
00450
00451
00452
00453 TImageGray::~TImageGray(void)
00454 {
00455 delete [] m_data;
00456 m_data = NULL;
00457 }
00458
00459
00460
00461
00462
00463
00464
00465 TImageGray::TImageGray(DWORD width, DWORD height)
00466 {
00467 m_width = width;
00468 m_height = height;
00469
00470 m_data = new unsigned char[ width * height ];
00471 }
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482 void TImageGray::FromARGB( const TImageARGB * image )
00483 {
00484 if (
00485 this->GetWidth() != image->GetWidth() ||
00486 this->GetHeight() != image->GetHeight()
00487 )
00488 {
00489
00490 return;
00491 }
00492
00493
00494
00495
00496 const DWORD * data = (const DWORD*)image->GetData();
00497 const DWORD pixelCount = this->GetWidth() * this->GetHeight();
00498
00499 for ( DWORD i = 0 ; i < pixelCount ; i++ )
00500 {
00501 float blue = (float) ((unsigned char*)&data[i])[0];
00502 float green = (float) ((unsigned char*)&data[i])[1];
00503 float red = (float) ((unsigned char*)&data[i])[2];
00504
00505 float tmp =
00506 red * 0.2989f +
00507 green * 0.5866f +
00508 blue * 0.1144f;
00509
00510
00511 unsigned char res;
00512 #if 1
00513
00514 if ( tmp <= 255.f )
00515 {
00516
00517 res = (unsigned char) tmp;
00518 }
00519 else
00520 {
00521 res = 255;
00522 }
00523 #else
00524
00525 if ( tmp >= 0.f )
00526 {
00527 if ( tmp <= 255.f )
00528 {
00529 res = (unsigned char) tmp;
00530 }
00531 else
00532 {
00533 res = 255;
00534 }
00535 }
00536 else
00537 {
00538 res = 0;
00539 }
00540 #endif
00541 m_data[i] = res;
00542 }
00543 }
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554 void TImageGray::FromRGB( const TImageRGB * image )
00555 {
00556 if (
00557 this->GetWidth() != image->GetWidth() ||
00558 this->GetHeight() != image->GetHeight()
00559 )
00560 {
00561
00562 return;
00563 }
00564
00565
00566
00567
00568 const unsigned char * src = (unsigned char*)image->GetData();
00569 const DWORD byteCount = image->GetDataSize();
00570
00571 DWORD offsetSrc = 0;
00572 DWORD offsetDst = 0;
00573
00574 while ( offsetSrc < byteCount )
00575 {
00576 float blue = (float) src[ offsetSrc++ ];
00577 float green = (float) src[ offsetSrc++ ];
00578 float red = (float) src[ offsetSrc++ ];
00579
00580 float tmp =
00581 red * 0.2989f +
00582 green * 0.5866f +
00583 blue * 0.1144f;
00584
00585
00586 unsigned char res;
00587
00588
00589 if ( tmp <= 255.f )
00590 {
00591
00592 res = (unsigned char) tmp;
00593 }
00594 else
00595 {
00596 res = 255;
00597 }
00598
00599 m_data[ offsetDst++ ] = res;
00600 }
00601 }
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617 void TImageGray::CopyFrom( const TImageGray * src, DWORD x, DWORD y, DWORD width, DWORD height)
00618 {
00619 if (
00620 ( width > 0 ) && ( height > 0 ) &&
00621 src->IsRectInside( x, y, width, height ) &&
00622 width <= m_width &&
00623 height <= m_height
00624 )
00625 {
00626
00627
00628 const unsigned char * tmpDataSrc = (unsigned char*) src->m_data;
00629 unsigned char * tmpDataDst = (unsigned char*) this->m_data;
00630
00631 for ( DWORD dy = 0 ; dy < height ; dy++ )
00632 {
00633 const unsigned char * startSrc = &tmpDataSrc[ (y + dy) * src->m_width + x ];
00634 unsigned char * startDst = &tmpDataDst[ dy * m_width ];
00635
00636 memcpy( startDst, startSrc, sizeof( unsigned char ) * width );
00637 }
00638 }
00639 else
00640 {
00641
00642 }
00643 }
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654 const TImage* TImageSetReal::GetARGB(void) const
00655 {
00656 return m_image_ARGB;
00657 }
00658
00659
00660
00661
00662 const TImage* TImageSetReal::GetGray(void) const
00663 {
00664 return m_image_gray;
00665 }
00666
00667
00668
00669
00670 const TImage* TImageSetReal::GetRGB(void) const
00671 {
00672 return m_image_RGB;
00673 }
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683 TImageSetReal::TImageSetReal(TImageSetManager * manager, DWORD id, DWORD width, DWORD height)
00684 {
00685 m_manager = manager;
00686 m_managerID = id;
00687
00688 m_image_ARGB = new TImageARGB( width, height );
00689 m_image_gray = new TImageGray( width, height );
00690 m_image_RGB = new TImageRGB( width, height );
00691 }
00692
00693
00694
00695
00696
00697 TImageSetReal::~TImageSetReal(void)
00698 {
00699 delete m_image_ARGB;
00700 m_image_ARGB = NULL;
00701
00702 delete m_image_gray;
00703 m_image_gray = NULL;
00704
00705 delete m_image_RGB;
00706 m_image_RGB = NULL;
00707 }
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725 TImageSetManager::TImageSetManager( DWORD width, DWORD height, DWORD initSize )
00726 {
00727 assert(width);
00728 assert(height);
00729
00730 m_width = width;
00731 m_height = height;
00732
00733
00734 for( DWORD i = 0 ; i < initSize ; i++ )
00735 {
00736
00737 TImageSetReal * img = new TImageSetReal( this, i, m_width, m_height );
00738 m_imageSets.push_back(img);
00739
00740 m_imgFlags.push_back(FALSE);
00741 }
00742 }
00743
00744
00745
00746
00747
00748
00749 TImageSetManager::~TImageSetManager(void)
00750 {
00751 m_critical.Enter();
00752 const DWORD size = (DWORD)m_imageSets.size();
00753 for( DWORD i = 0 ; i < size ; i++ )
00754 {
00755 delete m_imageSets[i];
00756 m_imageSets[i] = NULL;
00757 }
00758 m_critical.Leave();
00759 }
00760
00761
00762
00763
00764
00765
00766
00767
00768 TImageSetReal* TImageSetManager::GetImageSet(void)
00769 {
00770 m_critical.Enter();
00771
00772 DWORD i = 0;
00773 const DWORD size = (DWORD)m_imageSets.size();
00774 BOOL found = FALSE;
00775 while( i < size )
00776 {
00777 if ( FALSE == m_imgFlags[i] )
00778 {
00779
00780 m_imgFlags[i] = TRUE;
00781 found = TRUE;
00782 break;
00783 }
00784 i++;
00785 }
00786
00787 TImageSetReal * ret = NULL;
00788 if(found)
00789 {
00790
00791 ret = m_imageSets[i];
00792 }
00793 else
00794 {
00795 #if 1
00796
00797 #else
00798
00799
00800 ret = new TImageSetReal( this, size, m_width, m_height );
00801 m_imageSets.push_back(ret);
00802
00803 m_imgFlags.push_back(TRUE);
00804 #endif
00805 }
00806 m_critical.Leave();
00807
00808 return ret;
00809 }
00810
00811
00812
00813
00814
00815
00816
00817
00818
00819 void TImageSetManager::ReleaseImageSet(const TImageSetReal * img)
00820 {
00821 m_critical.Enter();
00822
00823 const DWORD size = (DWORD)m_imageSets.size();
00824 const DWORD id = img->GetID();
00825 if(id < size)
00826 {
00827
00828 m_imgFlags[id] = FALSE;
00829 }
00830 else
00831 {
00832
00833 assert(false);
00834 }
00835
00836 m_critical.Leave();
00837 }
00838
00839
00840
00841
00842
00843
00844
00845
00846
00847
00848 const TImageSet* TFrameReal::GetImageSet(void) const
00849 {
00850 return m_image;
00851 }
00852
00853
00854
00855
00856 const TTimeStamp & TFrameReal::GetTimestamp(void) const
00857 {
00858 return m_time;
00859 }
00860
00861
00862
00863
00864 void TFrameReal::AddRefs(void)
00865 {
00866 m_critSection.Enter();
00867 m_refs++;
00868 m_critSection.Leave();
00869 }
00870
00871
00872
00873
00874
00875 void TFrameReal::Release(void)
00876 {
00877 bool remove = false;
00878
00879 m_critSection.Enter();
00880 if( m_refs > 0 )
00881 {
00882 m_refs--;
00883 if( m_refs == 0 )
00884 {
00885
00886 remove = true;
00887 }
00888 else
00889 {
00890
00891 }
00892 }
00893 else
00894 {
00895
00896 remove = true;
00897 }
00898 m_critSection.Leave();
00899
00900 if( remove )
00901 {
00902
00903
00904 delete this;
00905 }
00906 }
00907
00908
00909
00910
00911 DWORD TFrameReal::GetRefs(void) const
00912 {
00913 return m_refs;
00914 }
00915
00916
00917
00918
00919
00920
00921
00922
00923
00924 DWORD TFrameReal::AddLock(void)
00925 {
00926 m_critSection2.Enter();
00927 m_locks++;
00928 DWORD ret = m_locks;
00929 m_critSection2.Leave();
00930
00931 return ret;
00932 }
00933
00934
00935
00936
00937
00938
00939
00940
00941 DWORD TFrameReal::ReleaseLock(void)
00942 {
00943 m_critSection2.Enter();
00944 if( m_locks > 0 )
00945 {
00946 m_locks--;
00947 }
00948 else
00949 {
00950
00951 }
00952 DWORD ret = m_locks;
00953 m_critSection2.Leave();
00954
00955 return ret;
00956 }
00957
00958
00959
00960
00961 DWORD TFrameReal::GetLockCount(void) const
00962 {
00963 return m_locks;
00964 }
00965
00966
00967
00968
00969
00970
00971
00972
00973
00974 TFrameReal::~TFrameReal(void)
00975 {
00976
00977 m_image->GetManager()->ReleaseImageSet( m_image );
00978 }
00979
00980
00981
00982
00983
00984
00985
00986
00987
00988 TFrameReal::TFrameReal( TImageSetReal * images ):
00989 m_time()
00990 {
00991 m_image = images;
00992
00993 m_refs = 1;
00994 m_locks = 0;
00995 }
00996
00997
00998
00999
01000
01001