pic_io.h

00001 /* ***** BEGIN LICENSE BLOCK *****
00002 *
00003 * $Id: pic_io.h,v 1.2 2005/01/30 05:11:40 gabest Exp $ $Name:  $
00004 *
00005 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00006 *
00007 * The contents of this file are subject to the Mozilla Public License
00008 * Version 1.1 (the "License"); you may not use this file except in compliance
00009 * with the License. You may obtain a copy of the License at
00010 * http://www.mozilla.org/MPL/
00011 *
00012 * Software distributed under the License is distributed on an "AS IS" basis,
00013 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
00014 * the specific language governing rights and limitations under the License.
00015 *
00016 * The Original Code is BBC Research and Development code.
00017 *
00018 * The Initial Developer of the Original Code is the British Broadcasting
00019 * Corporation.
00020 * Portions created by the Initial Developer are Copyright (C) 2004.
00021 * All Rights Reserved.
00022 *
00023 * Contributor(s): Thomas Davies (Original Author),
00024 *                 Scott Robert Ladd,
00025 *                 Stuart Cunningham,
00026 *                 Tim Borer,
00027 *                 Anuradha Suraparaju
00028 *
00029 * Alternatively, the contents of this file may be used under the terms of
00030 * the GNU General Public License Version 2 (the "GPL"), or the GNU Lesser
00031 * Public License Version 2.1 (the "LGPL"), in which case the provisions of
00032 * the GPL or the LGPL are applicable instead of those above. If you wish to
00033 * allow use of your version of this file only under the terms of the either
00034 * the GPL or LGPL and not to allow others to use your version of this file
00035 * under the MPL, indicate your decision by deleting the provisions above
00036 * and replace them with the notice and other provisions required by the GPL
00037 * or LGPL. If you do not delete the provisions above, a recipient may use
00038 * your version of this file under the terms of any one of the MPL, the GPL
00039 * or the LGPL.
00040 * ***** END LICENSE BLOCK ***** */
00041 
00042 #ifndef _PIC_IO_H_
00043 #define _PIC_IO_H_
00044 
00045 #include <iostream>
00046 #include <fstream>
00047 #include <streambuf>
00048 
00049 #include <libdirac_common/common.h>
00050 #include <libdirac_common/frame.h>
00051 
00052 namespace dirac
00053 {
00055     //--------------------------------------//
00056     //-                                    -//
00057     //-Uncompressed picture file IO wrapper-//
00058     //-                                    -//
00059     //--------------------------------------//
00061 
00062     // Stream classes for writing/reading frames of uncompressed/decoded data
00063     // to stream. Streams currently supported are Memory based streams and
00064     // File based streams. These classes need further restructuring. 
00065     // Anu - 19-11-2004
00066 
00067     // Subclass these to provide functionality for different file formats and 
00068     // for streaming.
00069 
00070 
00072 
00073 
00077     class StreamPicOutput
00078     {
00079         public:
00080 
00082             StreamPicOutput();
00084 
00088             StreamPicOutput( const SeqParams& sp);
00089 
00091             virtual ~StreamPicOutput();
00092 
00094             virtual bool WriteNextFrame(const Frame& myframe);
00095 
00097             SeqParams& GetSeqParams() {return m_sparams;}
00098 
00099         protected:
00100 
00102             SeqParams m_sparams;
00104             std::ostream* m_op_pic_ptr;
00105 
00107             virtual bool WriteComponent(const PicArray& pic_data, 
00108                                         const CompSort& cs);
00109     };
00110 
00114     class MemoryStreamOutput : public StreamPicOutput
00115     {
00116         public:
00118             MemoryStreamOutput();
00119 
00121             ~MemoryStreamOutput();
00122 
00124             void SetSequenceParams ( SeqParams &sparams)
00125             { m_sparams = sparams; }
00126 
00128             void SetMembufReference (unsigned char *buf, int buf_size);
00129 
00131             bool End() const ;
00132 
00133         protected:
00135             MemoryStreamOutput(const MemoryStreamOutput&);
00137             MemoryStreamOutput & operator =(const MemoryStreamOutput&);
00138 
00139         protected:
00140 
00142             class OutputMemoryBuffer : public std::streambuf
00143             {
00144             public:
00146                 OutputMemoryBuffer () : 
00147                 m_op_buf(0), 
00148                 m_op_buf_size(0), 
00149                 m_op_idx(0)
00150                 {}
00151 
00153 
00157                 void SetMembufReference (unsigned char *buffer, int buffer_size)
00158                 {
00159                     m_op_buf = buffer;
00160                     m_op_buf_size = buffer_size;
00161                     m_op_idx = 0;
00162                 }
00163 
00164             protected:
00166                 unsigned char *m_op_buf;
00168                 int m_op_buf_size;
00170                 int m_op_idx;
00171 
00173                 virtual int overflow (int c)
00174                 {
00175                     if ( c != EOF)
00176                     {
00177                         if (m_op_idx == m_op_buf_size)
00178                             return EOF;
00179 
00180                         m_op_buf[m_op_idx] = (char)c;
00181                         m_op_idx++;
00182                     }
00183                     return c;
00184                 }
00185 
00187                 virtual std::streamsize xsputn (const char *s, 
00188                                             std::streamsize num)
00189                 {
00190                     std::streamsize bytes_left = m_op_buf_size - m_op_idx;
00191                     std::streamsize bytes_written = bytes_left > num 
00192                                                         ? num : bytes_left;
00193                     memcpy (&m_op_buf[m_op_idx], (unsigned char *)s, 
00194                             bytes_written);
00195                     m_op_idx += bytes_written;
00196                     return bytes_written;
00197                 }
00198 
00199             private:
00201                 OutputMemoryBuffer(const OutputMemoryBuffer&);
00203                 OutputMemoryBuffer& operator =(const OutputMemoryBuffer&);
00204             };
00205 
00207             OutputMemoryBuffer m_membuf;
00208     };
00209 
00213     class FileStreamOutput : public StreamPicOutput
00214     {
00215         public:
00216 
00218 
00224             FileStreamOutput (const char* output_name,
00225               const SeqParams& sp,
00226               bool write_header_only = false);
00227 
00229             virtual ~FileStreamOutput ();
00230 
00232             virtual bool WritePicHeader();
00233 
00234         protected:
00235 
00237             std::ofstream* m_op_head_ptr;
00238 
00240             virtual bool OpenHeader(const char* output_name);
00241 
00243             virtual bool OpenYUV(const char* output_name);
00244     };
00245 
00247 
00251     class StreamPicInput
00252     {
00253         public:
00254 
00256             StreamPicInput();
00258 
00263             StreamPicInput(std::istream *ip_pic_ptr, const SeqParams& sparams);
00264 
00266             virtual ~StreamPicInput();
00267 
00269             virtual void Skip( const int n) = 0;
00270 
00272             void SetPadding(const int xpd, const int ypd);
00273 
00275             virtual bool ReadNextFrame(Frame& myframe);
00276 
00278             const SeqParams& GetSeqParams() const {return m_sparams;}
00279 
00281             bool End() const ;
00282 
00283         protected:
00284 
00286             SeqParams m_sparams;
00287 
00289             std::istream* m_ip_pic_ptr;
00290 
00292             int m_xpad,m_ypad;
00293 
00295             virtual bool ReadComponent(PicArray& pic_data,const CompSort& cs);
00296     };
00297 
00301     class MemoryStreamInput : public StreamPicInput
00302     {
00303         public:
00305             MemoryStreamInput();
00306 
00308             ~MemoryStreamInput();
00309 
00311             void SetSequenceParams ( SeqParams &sparams)
00312             { m_sparams = sparams; }
00313 
00315 
00319             void SetMembufReference (unsigned char *buf, int buf_size);
00320 
00322             bool End() const ;
00323 
00325             virtual void Skip( const int n);
00326 
00327         protected:
00329             MemoryStreamInput(const MemoryStreamInput&);
00331             MemoryStreamInput & operator =(const MemoryStreamInput&);
00332 
00333         protected:
00335             class InputMemoryBuffer : public std::streambuf
00336             {
00337             public:
00339                 InputMemoryBuffer() : m_buffer(0), m_buffer_size(0)
00340                 {
00341                     setg ((char *)m_buffer, (char *)m_buffer, (char *)m_buffer);
00342                 }
00343 
00345                 ~InputMemoryBuffer(){}
00346 
00348 
00352                 void SetMembufReference (unsigned char *buffer, int buffer_size)
00353                 {
00354                     m_buffer = buffer;
00355                     m_buffer_size = buffer_size;
00356 
00357                     setg ((char *)m_buffer, (char *)m_buffer, 
00358                                 (char *)(m_buffer + buffer_size));
00359                 }
00360 
00361             private:
00363                 InputMemoryBuffer (const InputMemoryBuffer& inbuf);
00365                 InputMemoryBuffer& operator = (const InputMemoryBuffer& inbuf);
00366 
00368                 unsigned char *m_buffer;
00370                 int m_buffer_size;
00371             };
00372 
00374             InputMemoryBuffer m_membuf;
00375     };
00376 
00378 
00381     class FileStreamInput : public StreamPicInput
00382     {
00383         public:
00384 
00386 
00390             FileStreamInput (const char* input_name);
00391 
00393             virtual ~FileStreamInput ();
00394 
00396             virtual bool ReadPicHeader();
00397 
00399             virtual void Skip( const int n);
00400 
00401 
00402         protected:
00404             std::ifstream* m_ip_head_ptr;
00405     };
00406 
00407 } // namespace dirac
00408 
00409 #endif

Generated on Tue Dec 13 14:47:15 2005 for guliverkli by  doxygen 1.4.5