00001 // /// // Mx clientSDK 00002 // ///// //// Mx Crossplatform Client Library 00003 // /// XXX XXX /// 00004 // /// XXX XXX /// $RCSfile: mxcpcJPEGSequenceWriter.cpp,v $ 00005 // /// XXX /// $Revision: 1.2 $ 00006 // /// XXX XXX /// $Date: 2005/12/14 10:38:52 $ 00007 // //// XXX XXX //// $Author: cvs-kai $ 00008 // //// //// 00009 // //// M O B O T I X //////////////////////////////////////////////// 00010 // //// Security Vision Systems ////////////////////////////////////////////// 00011 // // 00012 // Copyright (C) 2005 - 2006, MOBOTIX AG, Germany // 00013 // This software is made available under the BSD licence. Please refer // 00014 // to the file LICENCE.TXT contained in this distribution for details. // 00015 // // 00016 // ///////////////////////////////////////////////////////////////////// 00017 00018 00019 00020 #include <mxcpcJPEGSequenceWriter.h> 00021 00022 #include <cstdio> 00023 #include <cstring> 00024 #include <memory> 00025 00026 00027 00028 mxcpcJPEGSequenceWriter::mxcpcJPEGSequenceWriter(const char *filename_prefix) { 00029 00030 int prefix_len; 00031 00032 prefix_len = std::strlen(filename_prefix); 00033 00034 FileNamePrefix = 0; 00035 FileName = 0; 00036 00037 try { 00038 00039 FileNamePrefix = new char[prefix_len + 1]; 00040 FileName = new char[prefix_len + 20 + 1]; 00041 // so we can append a sequence number... 00042 00043 } catch(std::bad_alloc) { 00044 00045 delete[] FileNamePrefix; 00046 delete[] FileName; 00047 00048 throw; 00049 } 00050 00051 std::strcpy(FileNamePrefix, filename_prefix); 00052 00053 OutFile = 0; 00054 FrameNum = 0; 00055 00056 ErrorEncountered = false; 00057 } 00058 00059 00060 mxcpcJPEGSequenceWriter::~mxcpcJPEGSequenceWriter() { 00061 00062 delete[] FileNamePrefix; 00063 delete[] FileName; 00064 00065 if(OutFile) std::fclose(OutFile); 00066 } 00067 00068 00069 void mxcpcJPEGSequenceWriter::beginFrame(void) { 00070 00071 if(OutFile) { // being paranoid 00072 00073 std::fclose(OutFile); 00074 OutFile = 0; 00075 00076 ErrorEncountered = true; 00077 } 00078 00079 std::sprintf(FileName, "%s%06d.jpg", FileNamePrefix, FrameNum); 00080 00081 OutFile = std::fopen(FileName, "w"); 00082 if(!OutFile) ErrorEncountered = true; 00083 } 00084 00085 00086 void mxcpcJPEGSequenceWriter::endFrame(void) { 00087 00088 if(OutFile) std::fclose(OutFile); 00089 00090 OutFile = 0; 00091 00092 FrameNum++; 00093 } 00094 00095 00096 void mxcpcJPEGSequenceWriter::receiveJPEGBytes(const mxcpc::u8 *buffer, 00097 int num) { 00098 00099 if(!OutFile) { 00100 00101 ErrorEncountered = true; 00102 return; 00103 } 00104 00105 if(num < 1) { 00106 00107 ErrorEncountered = true; 00108 00109 return; 00110 } 00111 00112 if(fwrite(buffer, num, 1, OutFile) != 1) { 00113 00114 std::fclose(OutFile); 00115 OutFile = 0; 00116 00117 ErrorEncountered = true; 00118 } 00119 }