00001
00003 #include "CameraThread.h"
00004
00005 #include <FCam/N900.h>
00006
00007 #include <vector>
00008 #include <iostream>
00009
00010 #include "OverlayWidget.h"
00011 #include "InfoWidget.h"
00012
00013 using namespace std;
00014
00015 namespace Plat = FCam::N900;
00016
00017 void CameraThread::run() {
00018 int i = 0;
00019
00020
00021
00022
00023 FCam::AsyncFileWriter writer;
00024 Plat::Sensor sensor;
00025 Plat::Lens lens;
00026 Plat::Flash flash;
00027
00028
00029
00030 sensor.attach(&flash);
00031 sensor.attach(&lens);
00032
00033
00034 FCam::AutoFocus autoFocus(&lens);
00035
00036
00037 FCam::Shot viewfinder;
00038 viewfinder.exposure = 40000;
00039 viewfinder.gain = 1.0f;
00040
00041 viewfinder.frameTime = 40000;
00042
00043 viewfinder.image = overlay->framebuffer();
00044
00045 viewfinder.histogram.regions = 1;
00046 viewfinder.histogram.region[0] = FCam::Rect(0, 0, 640, 480);
00047 viewfinder.sharpness.enabled = true;
00048 viewfinder.sharpness.size = FCam::Size(16, 12);
00049
00050
00051
00052
00053
00054
00055 FCam::Shot photo;
00056 photo.image = FCam::Image(2592, 1968, FCam::RAW, FCam::Image::AutoAllocate);
00057
00058 bool takeSnapshot = false;
00059 bool halfDepress = false;
00060 bool fullDepress = false;
00061
00062
00063 sensor.stream(viewfinder);
00064
00065 while (keepGoing) {
00066
00067 FCam::Event e;
00068 while (getNextEvent(&e)) {
00069 cout << e.description() << endl;
00070 switch(e.type) {
00071 case FCam::Event::FocusPressed:
00072 if (autoFocus.idle()) autoFocus.startSweep();
00073 halfDepress = true;
00074 break;
00075 case FCam::Event::FocusReleased:
00076 halfDepress = false;
00077 break;
00078 case FCam::Event::ShutterPressed:
00079 takeSnapshot = true;
00080 fullDepress = true;
00081 break;
00082 case FCam::Event::ShutterReleased:
00083 fullDepress = false;
00084 };
00085 }
00086
00087
00088 if (takeSnapshot && autoFocus.idle() && writer.savesPending() < 8) {
00089
00090 photo.exposure = viewfinder.exposure;
00091 photo.gain = viewfinder.gain;
00092 photo.actions.clear();
00093
00094
00095 if (photo.gain > 2) {
00096 FCam::Flash::FireAction fire(&flash);
00097 fire.time = photo.exposure - flash.minDuration();
00098 fire.duration = flash.minDuration();
00099 fire.brightness = flash.maxBrightness();
00100 photo.actions.insert(&fire);
00101
00102 printf("Using a gain of %f, so I'll fire the flash\n", photo.gain);
00103 photo.gain /= 2;
00104
00105
00106 sensor.capture(photo);
00107 } else {
00108 printf("Using a gain of %f, so not firing the flash\n", photo.gain);
00109 sensor.capture(photo);
00110 }
00111
00112 takeSnapshot = false;
00113 }
00114
00115
00116 FCam::Frame::Ptr f;
00117 do {
00118 f = sensor.getFrame();
00119
00120 if (f->shot().id == photo.id) {
00121
00122
00123
00124
00125 if (!f->image.valid()) {
00126 printf("ERROR: Photo dropped!\n");
00127 continue;
00128 } else {
00129 printf("Got a 5MP frame\n");
00130 }
00131
00132
00133 char fname[256];
00134 snprintf(fname, 255, "/home/user/MyDocs/photo_%d_%d.dng",
00135 f->exposureStartTime.s(), f->exposureStartTime.us());
00136 writer.saveDNG(f, fname);
00137
00138
00139 snprintf(fname, 255, "/home/user/MyDocs/photo_%d_%d.jpg",
00140 f->exposureStartTime.s(), f->exposureStartTime.us());
00141 writer.saveJPEG(f, fname, 90);
00142 } else {
00143
00144
00145 autoFocus.update(f);
00146 autoExpose(&viewfinder, f);
00147 sensor.stream(viewfinder);
00148 }
00149 } while (sensor.framesPending());
00150
00151
00152 if (f->shot().id == viewfinder.id && viewfinder.image.valid()) {
00153
00154 info->setFrame(f);
00155
00156
00157 overlay->update();
00158 info->update();
00159 }
00160
00161 i++;
00162 }
00163 }