ImageCapture/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 <sys/types.h>
00013 #include <sys/stat.h>
00014 #include <unistd.h>
00015 #include <string.h>
00016 #include <OPENR/ODataFormats.h>
00017 #include <OPENR/OFbkImage.h>
00018 #include <OPENR/OPENRAPI.h>
00019 #include <OPENR/OSyslog.h>
00020 #include <OPENR/core_macro.h>
00021 #include "ImageObserver.h"
00022 #include "BMP.h"
00023 
00024 ImageObserver::ImageObserver() : imageObserverState(IOS_IDLE),
00025                                  fbkID(oprimitiveID_UNDEF),
00026                                  tinswID(oprimitiveID_UNDEF),
00027                                  backswID(oprimitiveID_UNDEF)
00028 {
00029 }
00030 
00031 OStatus
00032 ImageObserver::DoInit(const OSystemEvent& event)
00033 {
00034     NEW_ALL_SUBJECT_AND_OBSERVER;
00035     REGISTER_ALL_ENTRY;
00036     SET_ALL_READY_AND_NOTIFY_ENTRY;
00037 
00038     char design[orobotdesignNAME_MAX+1];
00039     OStatus result = OPENR::GetRobotDesign(design);
00040     if (result != oSUCCESS) {
00041         OSYSLOG1((osyslogERROR, "%s : %s %d",
00042                   "ImageObserver::DoInit()",
00043                   "OPENR::GetRobotDesign() FAILED", result));
00044     }
00045 
00046     if (!strcmp(design, "ERS-210") || !strcmp(design, "ERS-220")) {
00047         robot = ROBOT_ERS200;
00048     } else if (!strcmp(design, "ERS-7")) {
00049         robot = ROBOT_ERS7;
00050     } else {
00051         robot = ROBOT_UNDEF;
00052         OSYSLOG1((osyslogERROR,
00053                   "ImageObserver::DoInit() UNKNOWN ROBOT DESIGN"));
00054     }
00055 
00056     OpenPrimitive();
00057 
00058     return oSUCCESS;
00059 }
00060 
00061 OStatus
00062 ImageObserver::DoStart(const OSystemEvent& event)
00063 {
00064     imageObserverState = IOS_START;
00065 
00066     ENABLE_ALL_SUBJECT;
00067     ASSERT_READY_TO_ALL_OBSERVER;
00068 
00069     return oSUCCESS;
00070 }    
00071 
00072 OStatus
00073 ImageObserver::DoStop(const OSystemEvent& event)
00074 {
00075     imageObserverState = IOS_IDLE;
00076 
00077     DISABLE_ALL_SUBJECT;
00078     DEASSERT_READY_TO_ALL_OBSERVER;
00079 
00080     return oSUCCESS;
00081 }
00082 
00083 OStatus
00084 ImageObserver::DoDestroy(const OSystemEvent& event)
00085 {
00086     DELETE_ALL_SUBJECT_AND_OBSERVER;
00087     return oSUCCESS;
00088 }
00089 
00090 void
00091 ImageObserver::Notify(const ONotifyEvent& event)
00092 {
00093     static int counter = 0;
00094 
00095     OFbkImageVectorData* fbkImageVectorData 
00096         = (OFbkImageVectorData*)event.Data(0);
00097 
00098     if (imageObserverState == IOS_IDLE) {
00099         return; // do nothing
00100     }
00101 
00102     if (robot == ROBOT_ERS200) {
00103         OSensorValue val1, val2;
00104         OPENR::GetSensorValue(tinswID, &val1);
00105         OPENR::GetSensorValue(backswID, &val2);
00106         if (val1.value == oswitchON || val2.value == oswitchON) {
00107             while (ExistData(counter) == true) counter++;
00108             SaveData(counter, fbkImageVectorData);
00109         }
00110     } else if (robot == ROBOT_ERS7) {
00111         OSensorValue val1, valf, valm, valr;
00112         OPENR::GetSensorValue(tinswID,  &val1);
00113         OPENR::GetSensorValue(backf7ID, &valf);
00114         OPENR::GetSensorValue(backm7ID, &valm);
00115         OPENR::GetSensorValue(backr7ID, &valr);
00116         if (val1.value == oswitchON ||
00117             valf.value >= 12 || valm.value >= 14 || valr.value >= 14) {
00118             while (ExistData(counter) == true) counter++;
00119             SaveData(counter, fbkImageVectorData);
00120         }
00121     }
00122 
00123     observer[event.ObsIndex()]->AssertReady(event.SenderID());
00124 }
00125 
00126 void
00127 ImageObserver::OpenPrimitive()
00128 {
00129     OStatus result = OPENR::OpenPrimitive(FBK_LOCATOR, &fbkID);
00130     if (result != oSUCCESS) {
00131         OSYSLOG1((osyslogERROR, "%s : %s %d",
00132                   "ImageObserver::OpenPrimitive()",
00133                   "OPENR::OpenPrimitive() FAILED", result));
00134     }
00135 
00136     result = OPENR::OpenPrimitive(TINSW_LOCATOR, &tinswID);
00137     if (result != oSUCCESS) {
00138         OSYSLOG1((osyslogERROR, "%s : %s %d",
00139                   "ImageObserver::OpenPrimitive()",
00140                   "OPENR::OpenPrimitive() FAILED", result));
00141     }
00142 
00143     if (robot == ROBOT_ERS200) {
00144         result = OPENR::OpenPrimitive(BACKSW_LOCATOR, &backswID);
00145         if (result != oSUCCESS) {
00146             OSYSLOG1((osyslogERROR, "%s : %s %d",
00147                       "ImageObserver::OpenPrimitive()",
00148                       "OPENR::OpenPrimitive() FAILED", result));
00149         }
00150     } else if (robot == ROBOT_ERS7) {
00151         result = OPENR::OpenPrimitive(BACKF7_LOCATOR, &backf7ID);
00152         if (result != oSUCCESS) {
00153             OSYSLOG1((osyslogERROR, "%s : %s %d",
00154                       "ImageObserver::OpenPrimitive()",
00155                       "OPENR::OpenPrimitive() FAILED", result));
00156         }
00157         result = OPENR::OpenPrimitive(BACKM7_LOCATOR, &backm7ID);
00158         if (result != oSUCCESS) {
00159             OSYSLOG1((osyslogERROR, "%s : %s %d",
00160                       "ImageObserver::OpenPrimitive()",
00161                       "OPENR::OpenPrimitive() FAILED", result));
00162         }
00163         result = OPENR::OpenPrimitive(BACKR7_LOCATOR, &backr7ID);
00164         if (result != oSUCCESS) {
00165             OSYSLOG1((osyslogERROR, "%s : %s %d",
00166                       "ImageObserver::OpenPrimitive()",
00167                       "OPENR::OpenPrimitive() FAILED", result));
00168         }
00169     }
00170 }
00171 
00172 void
00173 ImageObserver::PrintTagInfo(OFbkImageVectorData* imageVec)
00174 {
00175     OFbkImageInfo* info = imageVec->GetInfo(ofbkimageLAYER_H);
00176     byte*          data = imageVec->GetData(ofbkimageLAYER_H);
00177     OFbkImage yImage(info, data, ofbkimageBAND_Y);
00178     
00179     OSYSPRINT(("FrameNumber %x FieldCounter %x ColorFreq ",
00180                imageVec->GetInfo(0)->frameNumber, yImage.FieldCounter()));
00181 
00182     OSYSPRINT(("%d ", yImage.ColorFrequency(ocdtCHANNEL0)));
00183     OSYSPRINT(("%d ", yImage.ColorFrequency(ocdtCHANNEL1)));
00184     OSYSPRINT(("%d ", yImage.ColorFrequency(ocdtCHANNEL2)));
00185     OSYSPRINT(("%d ", yImage.ColorFrequency(ocdtCHANNEL3)));
00186     OSYSPRINT(("%d ", yImage.ColorFrequency(ocdtCHANNEL4)));
00187     OSYSPRINT(("%d ", yImage.ColorFrequency(ocdtCHANNEL5)));
00188     OSYSPRINT(("%d ", yImage.ColorFrequency(ocdtCHANNEL6)));
00189     OSYSPRINT(("%d ", yImage.ColorFrequency(ocdtCHANNEL7)));
00190     OSYSPRINT(("\n"));
00191 }
00192 
00193 void
00194 ImageObserver::SaveRawData(char* path,
00195                            OFbkImageVectorData* imageVec, OFbkImageLayer layer)
00196 {
00197     OFbkImageInfo* info = imageVec->GetInfo(layer);
00198     byte*          data = imageVec->GetData(layer);
00199 
00200     size_t size;
00201     if (layer == ofbkimageLAYER_C) {
00202 
00203         size = info->width * info->height;
00204 
00205     } else { // ofbkimageLAYER_H or ofbkimageLAYER_M or ofbkimageLAYER_L
00206 
00207         if (info->type == odataFBK_YCrCb) {
00208             size = 3 * info->width * info->height;
00209         } else if (info->type == odataFBK_YCrCb_HPF) {
00210             size = 6 * info->width * info->height;
00211         }
00212 
00213     }
00214 
00215     FILE* fp = fopen(path, "w");
00216     if (fp == 0) {
00217         OSYSLOG1((osyslogERROR, "can't open %s", path));
00218         return;
00219     }
00220     fwrite(data, 1, size, fp);
00221     fclose(fp);
00222 }
00223 
00224 bool
00225 ImageObserver::ExistData(int serialNumber)
00226 {
00227     char name[128];
00228     struct stat st;
00229 
00230     sprintf(name, "/MS/OPEN-R/MW/DATA/P/RGBH%04d.BMP", serialNumber);
00231     if (stat(name, &st) == 0) return true;
00232 
00233     sprintf(name, "/MS/OPEN-R/MW/DATA/P/LAYH%04d.RAW", serialNumber);
00234     if (stat(name, &st) == 0) return true;
00235 
00236     sprintf(name, "/MS/OPEN-R/MW/DATA/P/LAYM%04d.RAW", serialNumber);
00237     if (stat(name, &st) == 0) return true;
00238 
00239     sprintf(name, "/MS/OPEN-R/MW/DATA/P/LAYL%04d.RAW", serialNumber);
00240     if (stat(name, &st) == 0) return true;
00241 
00242     return false;
00243 }
00244 
00245 void
00246 ImageObserver::SaveData(int serialNumber, OFbkImageVectorData* imageVec)
00247 {
00248     char name[128];
00249     BMP bmp;
00250 
00251     sprintf(name, "/MS/OPEN-R/MW/DATA/P/RGBH%04d.BMP", serialNumber);
00252     OSYSPRINT(("writing %s ...\n", name));
00253     bmp.SaveYCrCb2RGB(name, imageVec, ofbkimageLAYER_H);
00254 
00255     sprintf(name, "/MS/OPEN-R/MW/DATA/P/LAYH%04d.RAW", serialNumber);
00256     OSYSPRINT(("writing %s ...\n", name));
00257     SaveRawData(name, imageVec, ofbkimageLAYER_H);
00258 
00259     sprintf(name, "/MS/OPEN-R/MW/DATA/P/LAYM%04d.RAW", serialNumber);
00260     OSYSPRINT(("writing %s ...\n", name));
00261     SaveRawData(name, imageVec, ofbkimageLAYER_M);
00262 
00263     sprintf(name, "/MS/OPEN-R/MW/DATA/P/LAYL%04d.RAW", serialNumber);
00264     OSYSPRINT(("writing %s ...\n", name));
00265     SaveRawData(name, imageVec, ofbkimageLAYER_L);
00266 
00267     OSYSPRINT(("done.\n"));
00268 }

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