ImageObserver/ImageObserver/ImageObserver.cc

Go to the documentation of this file.
00001 //
00002 // Copyright 2002,2003,2004 Sony Corporation 
00003 //
00004 // Permission to use, copy, modify, and redistribute this software for
00005 // non-commercial use is hereby granted.
00006 //
00007 // This software is provided "as is" without warranty of any kind,
00008 // either expressed or implied, including but not limited to the
00009 // implied warranties of fitness for a particular purpose.
00010 //
00011 
00012 #include <OPENR/ODataFormats.h>
00013 #include <OPENR/OFbkImage.h>
00014 #include <OPENR/OPENRAPI.h>
00015 #include <OPENR/OSyslog.h>
00016 #include <OPENR/core_macro.h>
00017 #include "ImageObserver.h"
00018 #include "BMP.h"
00019 
00020 ImageObserver::ImageObserver() : imageObserverState(IOS_IDLE),
00021                                  fbkID(oprimitiveID_UNDEF)
00022 {
00023 }
00024 
00025 OStatus
00026 ImageObserver::DoInit(const OSystemEvent& event)
00027 {
00028     NEW_ALL_SUBJECT_AND_OBSERVER;
00029     REGISTER_ALL_ENTRY;
00030     SET_ALL_READY_AND_NOTIFY_ENTRY;
00031 
00032     OpenPrimitive();
00033     SetCdtVectorDataOfPinkBall();
00034 
00035     return oSUCCESS;
00036 }
00037 
00038 OStatus
00039 ImageObserver::DoStart(const OSystemEvent& event)
00040 {
00041     imageObserverState = IOS_START;
00042 
00043     ENABLE_ALL_SUBJECT;
00044     ASSERT_READY_TO_ALL_OBSERVER;
00045 
00046     return oSUCCESS;
00047 }    
00048 
00049 OStatus
00050 ImageObserver::DoStop(const OSystemEvent& event)
00051 {
00052     imageObserverState = IOS_IDLE;
00053 
00054     DISABLE_ALL_SUBJECT;
00055     DEASSERT_READY_TO_ALL_OBSERVER;
00056 
00057     return oSUCCESS;
00058 }
00059 
00060 OStatus
00061 ImageObserver::DoDestroy(const OSystemEvent& event)
00062 {
00063     DELETE_ALL_SUBJECT_AND_OBSERVER;
00064     return oSUCCESS;
00065 }
00066 
00067 void
00068 ImageObserver::Notify(const ONotifyEvent& event)
00069 {
00070     static int counter = 0;
00071 
00072     if (imageObserverState == IOS_IDLE) {
00073         return; // do nothing
00074     }
00075 
00076     OFbkImageVectorData* fbkImageVectorData 
00077         = (OFbkImageVectorData*)event.Data(0);
00078 
00079     BMP bmp;
00080     if (counter == 0) {
00081 
00082         OSYSPRINT(("SAVE LAYER_H.BMP ... "));
00083         bmp.SaveYCrCb2RGB("/MS/OPEN-R/MW/DATA/P/LAYER_H.BMP", 
00084                           fbkImageVectorData, ofbkimageLAYER_H);
00085         OSYSPRINT(("DONE\n"));
00086 
00087         OSYSPRINT(("SAVE LAYER_H.RAW ... "));
00088         SaveRawData("/MS/OPEN-R/MW/DATA/P/LAYER_H.RAW", 
00089                     fbkImageVectorData, ofbkimageLAYER_H);
00090         OSYSPRINT(("DONE\n"));
00091 
00092         OSYSPRINT(("SAVE LAYER_HR.BMP ... "));
00093         byte* image;
00094         int width, height;
00095         ReconstructImage(fbkImageVectorData,
00096                          ofbkimageLAYER_H, &image, &width, &height);
00097         bmp.SaveRaw2Gray("/MS/OPEN-R/MW/DATA/P/LAYER_HR.BMP",
00098                          image, width, height, 0);
00099         FreeImage(image);
00100         OSYSPRINT(("DONE\n"));
00101 
00102     } else if (counter == 1) {
00103 
00104         OSYSPRINT(("SAVE LAYER_M.BMP ... "));
00105         bmp.SaveYCrCb2RGB("/MS/OPEN-R/MW/DATA/P/LAYER_M.BMP", 
00106                           fbkImageVectorData, ofbkimageLAYER_M);
00107         OSYSPRINT(("DONE\n"));
00108 
00109     } else if (counter == 2) {
00110 
00111         OSYSPRINT(("SAVE LAYER_L.BMP ... "));
00112         bmp.SaveYCrCb2RGB("/MS/OPEN-R/MW/DATA/P/LAYER_L.BMP", 
00113                           fbkImageVectorData, ofbkimageLAYER_L);
00114         OSYSPRINT(("DONE\n"));
00115 
00116     } else if (counter == 3) {
00117         
00118         OSYSPRINT(("SAVE LAYER_C ... "));
00119         bmp.SaveLayerC("/MS/OPEN-R/MW/DATA/P/LAYER_C", fbkImageVectorData);
00120         OSYSPRINT(("DONE\n"));- 
00121 
00122             OSYSPRINT(("SAVE LAYER_M2.BMP ... "));
00123             bmp.SaveYCrCb2RGB("/MS/OPEN-R/MW/DATA/P/LAYER_M2.BMP", 
00124                               fbkImageVectorData, ofbkimageLAYER_M);
00125             OSYSPRINT(("DONE\n"));
00126 
00127     } else {
00128 
00129         PrintTagInfo(fbkImageVectorData);
00130 
00131     }
00132 
00133     counter++;
00134     observer[event.ObsIndex()]->AssertReady(event.SenderID());
00135 }
00136 
00137 void
00138 ImageObserver::OpenPrimitive()
00139 {
00140     OStatus result = OPENR::OpenPrimitive(FBK_LOCATOR, &fbkID);
00141     if (result != oSUCCESS) {
00142         OSYSLOG1((osyslogERROR, "%s : %s %d",
00143                   "ImageObserver::OpenPrimitive()",
00144                   "OPENR::OpenPrimitive() FAILED", result));
00145     }
00146 }
00147 
00148 void
00149 ImageObserver::SetCdtVectorDataOfPinkBall()
00150 {
00151     OStatus result;
00152     MemoryRegionID  cdtVecID;
00153     OCdtVectorData* cdtVec;
00154     OCdtInfo*       cdt;
00155 
00156     result = OPENR::NewCdtVectorData(&cdtVecID, &cdtVec);
00157     if (result != oSUCCESS) {
00158         OSYSLOG1((osyslogERROR, "%s : %s %d",
00159                   "ImageObserver::SetCdtVectorDataOfPinkBall()",
00160                   "OPENR::NewCdtVectorData() FAILED", result));
00161         return;
00162     }
00163 
00164     cdtVec->SetNumData(1);
00165 
00166     cdt = cdtVec->GetInfo(0);
00167     cdt->Init(fbkID, ocdtCHANNEL0);
00168 
00169     //
00170     // cdt->Set(Y_segment, Cr_max,  Cr_min, Cb_max, Cb_min)
00171     //
00172     cdt->Set( 0, 230, 150, 190, 120);
00173     cdt->Set( 1, 230, 150, 190, 120);
00174     cdt->Set( 2, 230, 150, 190, 120);
00175     cdt->Set( 3, 230, 150, 190, 120);
00176     cdt->Set( 4, 230, 150, 190, 120);
00177     cdt->Set( 5, 230, 150, 190, 120);
00178     cdt->Set( 6, 230, 150, 190, 120);
00179     cdt->Set( 7, 230, 150, 190, 120);
00180     cdt->Set( 8, 230, 150, 190, 120);
00181     cdt->Set( 9, 230, 150, 190, 120);
00182     cdt->Set(10, 230, 150, 190, 120);
00183     cdt->Set(11, 230, 150, 190, 120);
00184     cdt->Set(12, 230, 150, 190, 120);
00185     cdt->Set(13, 230, 150, 190, 120);
00186     cdt->Set(14, 230, 150, 190, 120);
00187     cdt->Set(15, 230, 150, 190, 120);
00188     cdt->Set(16, 230, 150, 190, 120);
00189     cdt->Set(17, 230, 150, 190, 120);
00190     cdt->Set(18, 230, 150, 190, 120);
00191     cdt->Set(19, 230, 150, 190, 120);
00192     cdt->Set(20, 230, 160, 190, 120);
00193     cdt->Set(21, 230, 160, 190, 120);
00194     cdt->Set(22, 230, 160, 190, 120);
00195     cdt->Set(23, 230, 160, 190, 120);
00196     cdt->Set(24, 230, 160, 190, 120);
00197     cdt->Set(25, 230, 160, 190, 120);
00198     cdt->Set(26, 230, 160, 190, 120);
00199     cdt->Set(27, 230, 160, 190, 120);
00200     cdt->Set(28, 230, 160, 190, 120);
00201     cdt->Set(29, 230, 160, 190, 120);
00202     cdt->Set(30, 230, 160, 190, 120);
00203     cdt->Set(31, 230, 160, 190, 120);
00204 
00205     result = OPENR::SetCdtVectorData(cdtVecID);
00206     if (result != oSUCCESS) {
00207         OSYSLOG1((osyslogERROR, "%s : %s %d",
00208                   "ImageObserver::SetCdtVectorDataOfPinkBall()",
00209                   "OPENR::SetCdtVectorData() FAILED", result));
00210     }
00211 
00212     result = OPENR::DeleteCdtVectorData(cdtVecID);
00213     if (result != oSUCCESS) {
00214         OSYSLOG1((osyslogERROR, "%s : %s %d",
00215                   "ImageObserver::SetCdtVectorDataOfPinkBall()",
00216                   "OPENR::DeleteCdtVectorData() FAILED", result));
00217     }
00218 }
00219 
00220 void
00221 ImageObserver::PrintTagInfo(OFbkImageVectorData* imageVec)
00222 {
00223     OFbkImageInfo* info = imageVec->GetInfo(ofbkimageLAYER_H);
00224     byte*          data = imageVec->GetData(ofbkimageLAYER_H);
00225     OFbkImage yImage(info, data, ofbkimageBAND_Y);
00226     
00227     OSYSPRINT(("FrameNumber %x FieldCounter %x ColorFreq ",
00228                imageVec->GetInfo(0)->frameNumber, yImage.FieldCounter()));
00229 
00230     OSYSPRINT(("%d ", yImage.ColorFrequency(ocdtCHANNEL0)));
00231     OSYSPRINT(("%d ", yImage.ColorFrequency(ocdtCHANNEL1)));
00232     OSYSPRINT(("%d ", yImage.ColorFrequency(ocdtCHANNEL2)));
00233     OSYSPRINT(("%d ", yImage.ColorFrequency(ocdtCHANNEL3)));
00234     OSYSPRINT(("%d ", yImage.ColorFrequency(ocdtCHANNEL4)));
00235     OSYSPRINT(("%d ", yImage.ColorFrequency(ocdtCHANNEL5)));
00236     OSYSPRINT(("%d ", yImage.ColorFrequency(ocdtCHANNEL6)));
00237     OSYSPRINT(("%d ", yImage.ColorFrequency(ocdtCHANNEL7)));
00238     OSYSPRINT(("\n"));
00239 }
00240 
00241 void
00242 ImageObserver::SaveRawData(char* path,
00243                            OFbkImageVectorData* imageVec, OFbkImageLayer layer)
00244 {
00245     OFbkImageInfo* info = imageVec->GetInfo(layer);
00246     byte*          data = imageVec->GetData(layer);
00247 
00248     size_t size;
00249     if (layer == ofbkimageLAYER_C) {
00250 
00251         size = info->width * info->height;
00252 
00253     } else { // ofbkimageLAYER_H or ofbkimageLAYER_M or ofbkimageLAYER_L
00254 
00255         if (info->type == odataFBK_YCrCb) {
00256             size = 3 * info->width * info->height;
00257         } else if (info->type == odataFBK_YCrCb_HPF) {
00258             size = 6 * info->width * info->height;
00259         }
00260         
00261     }
00262 
00263     FILE* fp = fopen(path, "w");
00264     if (fp == 0) {
00265         OSYSLOG1((osyslogERROR, "can't open %s", path));
00266         return;
00267     }
00268     fwrite(data, 1, size, fp);
00269     fclose(fp);
00270 }
00271 
00272 bool
00273 ImageObserver::ReconstructImage(OFbkImageVectorData* imageVec,
00274                                 OFbkImageLayer layer,
00275                                 byte** image, int* width, int* height)
00276 {
00277     *image = 0;
00278     *width = *height = 0;
00279     if (layer == ofbkimageLAYER_C) return false;
00280 
00281     OFbkImageInfo* info = imageVec->GetInfo(layer);
00282     byte*          data = imageVec->GetData(layer);
00283 
00284     OFbkImage yLLImg(info, data, ofbkimageBAND_Y); // Y_LL
00285     OFbkImage yLHImg(info, data, ofbkimageBAND_Y_LH);
00286     OFbkImage yHLImg(info, data, ofbkimageBAND_Y_HL);
00287     OFbkImage yHHImg(info, data, ofbkimageBAND_Y_HH);
00288 
00289     int w = yLLImg.Width();
00290     int h = yLLImg.Height();
00291 
00292     byte* img = (byte*)malloc((2*w)*(2*h));
00293     if (img == 0) return false;
00294 
00295     for (int y = 0; y < h; y++) {
00296 
00297         for (int x = 0; x < w; x++) {
00298             //
00299             // yLH, yHL, yHH : offset binary [0, 255] -> signed int [-128, 127]
00300             //
00301             int yLL = (int)yLLImg.Pixel(x, y);
00302             int yLH = (int)yLHImg.Pixel(x, y) - 128;
00303             int yHL = (int)yHLImg.Pixel(x, y) - 128;
00304             int yHH = (int)yHHImg.Pixel(x, y) - 128;
00305 
00306             int a = yLL + yLH + yHL + yHH; // ypix11
00307             int b = 2 * (yLL + yLH);       // ypix11 + ypix01
00308             int c = 2 * (yLL + yHL);       // ypix11 + ypix10
00309             int d = 2 * (yLL + yHH);       // ypix11 + ypix00
00310             
00311             byte ypix00 = ClipRange(d - a);
00312             byte ypix10 = ClipRange(c - a);
00313             byte ypix01 = ClipRange(b - a);
00314             byte ypix11 = ClipRange(a);
00315             
00316             PutPixel(img, 2*w, 2*x,   2*y,   ypix00);
00317             PutPixel(img, 2*w, 2*x+1, 2*y,   ypix10);
00318             PutPixel(img, 2*w, 2*x,   2*y+1, ypix01);
00319             PutPixel(img, 2*w, 2*x+1, 2*y+1, ypix11);
00320         }
00321 
00322     }
00323 
00324     *image  = img;
00325     *width  = 2 * w;
00326     *height = 2 * h;
00327 
00328     return true;
00329 }
00330 
00331 void
00332 ImageObserver::FreeImage(byte* image)
00333 {
00334     if (image != 0) free(image);
00335 }

Generated on Sun Dec 2 23:04:28 2007 for openSDK by  doxygen 1.3.9.1