00001
00002 #ifndef INCLUDED_CameraSource_h_
00003 #define INCLUDED_CameraSource_h_
00004
00005 #include "local/DataSource.h"
00006 #include "Shared/plistCollections.h"
00007 #include <iostream>
00008 #include <stdexcept>
00009 #include <map>
00010 #include <cmath>
00011
00012 #define TARGET_API_MAC_CARBON 1
00013 #include <Carbon/Carbon.h>
00014 #include <QuickTime/QuickTime.h>
00015
00016 extern bool checkQTThreadInit();
00017
00018 class CameraSource : public DataSource, public virtual plist::Dictionary {
00019 public:
00020
00021 CameraSource(SeqGrabComponent grabber, const std::string& srcName, const std::string& devName, const std::string& inputName, int inputIdx)
00022 throw(std::pair<OSErr,const char*>)
00023 : DataSource(), layer(0), sg(grabber), sgChan(NULL), gworld(NULL), name(srcName), deviceName(devName), devInputName(inputName), devInputIdx(inputIdx),
00024 frame(0), skipped(0), queuedFrames(0), grabbing(false),
00025 lastTime(0), duration(0), chanTimeScale(), chanTimeBase(), drawSeq(NULL), callbackerr(noErr),
00026 gworldBuf(NULL), imgbuf(NULL), imgbufSize(0), imgbufUsed(0)
00027 {
00028 initCamera();
00029
00030
00031 setLoadSavePolicy(FIXED,SYNC);
00032 addEntry("Layer",layer,"Controls the resolution layer at which the image should be processed.\n"
00033 "0 indicates \"automatic\" mode (picks layer closest to image's resolution), positive numbers indicate the resolution layer directly.\n"
00034 "Negative values are relative to the number of layers marked available by the vision setup, so that typically -1 would correspond to the \"double\" layer, and -2 would correspond to the \"full\" layer.");
00035 }
00036
00037
00038 ~CameraSource();
00039
00040
00041 virtual const std::string& getName() const { return name; }
00042
00043 virtual const std::string& getDeviceName() const { return deviceName; }
00044
00045 virtual const std::string& getInputName() const { return devInputName; }
00046
00047 virtual unsigned int nextTimestamp() { return static_cast<unsigned int>(std::ceil(lastTime+duration)); }
00048 virtual const std::string& nextName() { return name; }
00049
00050 virtual void setDataSourceThread(LoadDataThread* ldt);
00051 virtual void setDataSourceFramerate(float fps);
00052
00053 virtual unsigned int getData(const char *& payload, unsigned int& payloadSize, unsigned int& timestamp, std::string& name);
00054
00055 plist::Primitive<int> layer;
00056
00057 protected:
00058 void initCamera();
00059
00060
00061 static std::string p2c(unsigned char pascalStr[]) {
00062 unsigned char len = *pascalStr++;
00063 return std::string(reinterpret_cast<char*>(pascalStr),len);
00064 }
00065
00066 static void dumpLiteral(OSType t);
00067 static ComponentResult setVideoChannelBounds(SGChannel videoChannel, const Rect *scaledVideoBounds);
00068 static pascal ComponentResult compressCompleteBottleProc(SGChannel c, UInt8 *queuedFrameCount, SGCompressInfo *ci, TimeRecord *t, long refCon);
00069 static pascal OSErr grabDataProc(SGChannel c, Ptr p, long len, long *offset, long chRefCon, TimeValue time, short writeType, long refCon);
00070
00071
00072 void imgFrom2vuy(const unsigned char * s, short srcWidth, short srcHeight, short depth, long dataSize);
00073
00074 void imgFromyuv2(const unsigned char * s, short srcWidth, short srcHeight, short depth, long dataSize);
00075
00076 SeqGrabComponent sg;
00077 SGChannel sgChan;
00078 GWorldPtr gworld;
00079 std::string name;
00080 const std::string deviceName;
00081 const std::string devInputName;
00082 int devInputIdx;
00083
00084 unsigned int frame;
00085 unsigned int skipped;
00086 unsigned int queuedFrames;
00087 bool grabbing;
00088 TimeValue lastTime;
00089 UInt32 duration;
00090 TimeScale chanTimeScale;
00091 TimeBase chanTimeBase;
00092 ImageSequence drawSeq;
00093 OSErr callbackerr;
00094
00095 char * gworldBuf;
00096
00097 char * imgbuf;
00098 size_t imgbufSize;
00099 size_t imgbufUsed;
00100
00101 private:
00102 CameraSource(const CameraSource&);
00103 CameraSource& operator=(const CameraSource&);
00104 };
00105
00106 #endif