• Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

src/processing/Dump.cpp

00001 #include <stdio.h>
00002 
00003 #include <FCam/Event.h>
00004 #include <FCam/processing/Dump.h>
00005 
00006 #include "../Debug.h"
00007 
00008 namespace FCam {
00009 
00010     Image loadDump(std::string filename) {
00011         FILE *fp = fopen(filename.c_str(), "rb");
00012 
00013         if (!fp) {
00014             error(Event::FileLoadError, "loadDump: %s: Cannot open file for reading.", filename.c_str());
00015             return Image();
00016         }
00017 
00018         // Read the header
00019         int header[5];
00020         if (fread(header, sizeof(int), 5, fp) != 5) {
00021             error(Event::FileLoadError, "loadDump: %s: Unexpected EOF in header.", filename.c_str());
00022             fclose(fp);
00023             return Image();                                               
00024         }
00025 
00026         // sanity check the header
00027         if (header[0] != 1 || // frames
00028             header[1] < 0 ||  // width
00029             header[2] < 0) {
00030             error(Event::FileLoadError, "loadDump: %s: Malformed header.", filename.c_str());
00031             fclose(fp);
00032             return Image();
00033         }
00034 
00035         ImageFormat type;
00036         // check the number of channels is correct, given the type
00037         if (header[4] == 2 && header[3] == 2) {
00038             type = FCam::UYVY;
00039         } else if (header[4] == 4 && header[3] == 1) {
00040             type = FCam::RAW;
00041         } else if (header[4] == 2 && header[3] == 3) {
00042             type = FCam::RGB24;
00043         } else {
00044             error(Event::FileLoadError, "loadDump: %s: Malformed header.", filename.c_str());
00045             fclose(fp);            
00046             return Image();
00047         }
00048 
00049         // Allocate an image
00050         // todo: Allow loading into a preallocated image
00051         Image im(header[1], header[2], type);
00052 
00053         for (size_t y = 0; y < im.height(); y++) {
00054             size_t count = fread(im(0, y), bytesPerPixel(type), im.width(), fp);
00055             if (count != im.width()) {
00056                 error(Event::FileLoadError, 
00057                       "loadDump: %s: Unexpected EOF in image data at line %d/%d.", 
00058                       filename.c_str(), y, im.height());
00059                 fclose(fp);
00060                 return Image();
00061             }
00062         }
00063         
00064         return im;
00065     }
00066     
00067     void saveDump(Frame f, std::string filename) {
00068         saveDump(f.image(), filename);
00069     }
00070     
00071     void saveDump(Image im, std::string filename) {
00072         dprintf(DBG_MINOR,"saveDump: Saving dump as %s.\n", filename.c_str());
00073         
00074         if (!im.valid()) {
00075             error(Event::FileSaveError, "saveDump: %s: Image to save not valid.", filename.c_str());
00076             return;
00077         }
00078         
00079         unsigned int frames = 1;
00080         unsigned int width = im.width();
00081         size_t widthBytes = width*im.bytesPerPixel();
00082         unsigned int height = im.height();
00083         unsigned int channels;
00084         unsigned int type;
00085 
00086         switch (im.type()) {
00087         case FCam::UYVY:
00088             type = 2;
00089             channels = 2;
00090             break;
00091         case FCam::RAW:
00092             type = 4;
00093             channels = 1; 
00094             break;
00095         case FCam::RGB24:
00096             type = 2;
00097             channels = 3;
00098             break;
00099         default:
00100             error(Event::FileSaveError, "saveDump: %s: Unknown image type.", filename.c_str());
00101             return;
00102         }
00103         dprintf(DBG_MINOR,"saveDump: %s: Header: %d %d %d %d %d. Bytes %d\n", filename.c_str(), 
00104                 frames, width, height, channels, type, widthBytes*height+20);
00105         
00106         FILE *fp = fopen(filename.c_str(), "wb");
00107         if (!fp) {
00108             error(Event::FileSaveError, "saveDump: %s: Cannot open file for writing.", filename.c_str());
00109             return;
00110         }
00111         
00112         size_t count;
00113         int header[5] = {frames, width, height, channels, type};
00114         count = fwrite(header, sizeof(int), 5, fp);
00115         if (count != 5) {
00116             error(Event::FileSaveError, "saveDump: %s: Error writing header (out of space?)", filename.c_str());
00117             fclose(fp);
00118             return;
00119         }
00120     
00121         for (unsigned int y=0; y < height; y++) {
00122             count = fwrite(im(0,y), sizeof(char), widthBytes, fp);
00123             if (count != widthBytes) {
00124                 error(Event::FileSaveError, "saveDump: %s: Error writing image data (out of space?)", filename.c_str());
00125                 fclose(fp);
00126                 return;
00127             }
00128         }
00129 
00130         dprintf(DBG_MINOR,"saveDump: %s: Done.\n", filename.c_str());
00131         fclose(fp);
00132     }
00133 
00134 }
00135 

Generated on Mon Aug 16 2010 14:25:45 for FCam by  doxygen 1.7.1