00001
00002 #include <stdio.h>
00003 #include <iostream>
00004
00005 #include "bmpvideosource.h"
00006 #include "iniparser.h"
00007
00008 using namespace std;
00009
00010 void BmpFactory::registerParameters(ParamSection *sec) {
00011 sec->addStringParam("bmpFile", &filename, "default%04d.bmp");
00012 sec->addBoolParam("useBmpFile", &use, true);
00013 sec->addIntParam("bmp.first", &first, -1);
00014 sec->addIntParam("bmp.last", &last, -1);
00015 };
00016
00017 VideoSource *BmpFactory::construct() {
00018 if (use) {
00019 BmpVideoSource *vs = new BmpVideoSource(filename, first, last);
00020 if (vs->initialize()) return vs;
00021 delete vs;
00022 }
00023 return 0;
00024 }
00025
00026 BmpVideoSource::BmpVideoSource(char *genericFilename, int first, int last)
00027 : genericFilename(genericFilename)
00028 {
00029 firstImageIndex= first;
00030 lastImageIndex = last;
00031 frameCnt = firstImageIndex;
00032 width=height=-1;
00033 }
00034
00035 bool BmpVideoSource::initialize()
00036 {
00037 char fn[256];
00038 FILE *f;
00039
00040 if (firstImageIndex>=0)
00041 frameCnt =firstImageIndex;
00042 else
00043 frameCnt = 0;
00044
00045 int start = frameCnt;
00046
00047 sprintf(fn, genericFilename, frameCnt);
00048 f=fopen(fn, "r");
00049
00050 while(!f)
00051 {
00052 sprintf(fn, genericFilename, ++frameCnt);
00053 f=fopen(fn, "r");
00054 if((start+frameCnt) > 1000)
00055 break;
00056 }
00057 if (f)
00058 {
00059 firstImageIndex = frameCnt;
00060 fclose(f);
00061 return true;
00062 }
00063
00064 return false;
00065 }
00066
00067 bool BmpVideoSource::getFrame(IplImage *dst)
00068 {
00069 char fn[256];
00070
00071 sprintf(fn, genericFilename, frameCnt);
00072
00073 IplImage *im = cvLoadImage(fn, (dst->nChannels == 3 ? 1 : 0));
00074
00075 if (im == 0)
00076 {
00077 if (frameCnt== firstImageIndex)
00078 {
00079 cout << "cannot open images from file";
00080 return false;
00081 }
00082 if (playing) {
00083 frameCnt++;
00084
00085 if (frameCnt == lastImageIndex)
00086 frameCnt = firstImageIndex;
00087 }
00088
00089
00090 frameCnt = firstImageIndex;
00091 return getFrame(dst);
00092 }
00093
00094
00095 width = im->width;
00096 height = im->height;
00097
00098 if ((im->width == dst->width) && (im->height == dst->height))
00099 cvCopy(im,dst);
00100 else
00101 cvResize(im, dst);
00102
00103 cvReleaseImage(&im);
00104
00105 if (playing) {
00106 frameCnt++;
00107
00108 if (frameCnt == lastImageIndex)
00109 frameCnt = firstImageIndex;
00110 }
00111
00112 return true;
00113 }
00114
00115 int BmpVideoSource::getId()
00116 {
00117 if (playing) return frameCnt-1;
00118 return frameCnt;
00119 }
00120
00121 BmpVideoSource::~BmpVideoSource() {
00122 }
00123
00124 void BmpVideoSource::start() {
00125 playing = true;
00126 }
00127
00128 void BmpVideoSource::stop() {
00129 playing = false;
00130 }
00131
00132
00133 void BmpVideoSource::getSize(int &w, int &h)
00134 {
00135 if (width<0 || height<0) {
00136 char fn[256];
00137 IplImage *im;
00138
00139 do
00140 {
00141 sprintf(fn, genericFilename, frameCnt);
00142 im = cvLoadImage(fn, 8);
00143 if(im || frameCnt > 1000)
00144 break;
00145 frameCnt++;
00146 }
00147 while (true);
00148
00149 assert(im);
00150 width = w = im->width;
00151 height = h = im->height;
00152 cvReleaseImage(&im);
00153 } else {
00154 w=width;
00155 h=height;
00156 }
00157 }