00001 // 00002 // (C) Jan de Vaan 2007-2010, all rights reserved. See the accompanying "License.txt" for licensed use. 00003 // 00004 #ifndef CHARLS_STREAMS 00005 #define CHARLS_STREAMS 00006 00007 #include "dcmtk/ofstd/ofvector.h" 00008 #include "dcmtk/ofstd/ofbmanip.h" 00009 #include "util.h" 00010 00011 00012 00013 // This file defines JPEG-LS streams: The header and the actual pixel data. Header markers have fixed length, the pixeldata not. 00014 00015 00016 00017 class JpegSegment; 00018 00019 enum JPEGLS_ColorXForm 00020 { 00021 // default (RGB) 00022 COLORXFORM_NONE = 0, 00023 00024 // Color transforms as defined by HP 00025 COLORXFORM_HP1, 00026 COLORXFORM_HP2, 00027 COLORXFORM_HP3, 00028 00029 // Defined by HP but not supported by CharLS 00030 COLORXFORM_RGB_AS_YUV_LOSSY, 00031 COLORXFORM_MATRIX 00032 }; 00033 00034 // 00035 // JLSOutputStream: minimal implementation to write JPEG header streams 00036 // 00037 class JLSOutputStream 00038 { 00039 friend class JpegMarkerSegment; 00040 friend class JpegImageDataSegment; 00041 00042 public: 00043 JLSOutputStream(); 00044 virtual ~JLSOutputStream(); 00045 00046 void Init(Size size, LONG bitsPerSample, LONG ccomp); 00047 void AddScan(const void* compareData, const JlsParameters* pparams); 00048 void AddLSE(const JlsCustomParameters* pcustom); 00049 void AddColorTransform(int i); 00050 size_t GetBytesWritten() 00051 { return _cbyteOffset; } 00052 00053 size_t GetLength() 00054 { return _cbyteLength - _cbyteOffset; } 00055 00056 size_t Write(BYTE* pdata, size_t cbyteLength); 00057 00058 void EnableCompare(bool bCompare) 00059 { _bCompare = bCompare; } 00060 private: 00061 BYTE* GetPos() const 00062 { return _pdata + _cbyteOffset; } 00063 00064 void WriteByte(BYTE val) 00065 { 00066 ASSERT(!_bCompare || _pdata[_cbyteOffset] == val); 00067 00068 _pdata[_cbyteOffset++] = val; 00069 } 00070 00071 void WriteBytes(const OFVector<BYTE>& rgbyte) 00072 { 00073 for (size_t i = 0; i < rgbyte.size(); ++i) 00074 { 00075 WriteByte(rgbyte[i]); 00076 } 00077 } 00078 00079 void WriteWord(USHORT val) 00080 { 00081 WriteByte(BYTE(val / 0x100)); 00082 WriteByte(BYTE(val % 0x100)); 00083 } 00084 00085 00086 void Seek(size_t byteCount) 00087 { _cbyteOffset += byteCount; } 00088 00089 bool _bCompare; 00090 00091 private: 00092 BYTE* _pdata; 00093 size_t _cbyteOffset; 00094 size_t _cbyteLength; 00095 LONG _icompLast; 00096 OFVector<JpegSegment*> _segments; 00097 }; 00098 00099 00100 00101 struct Presets : public JlsCustomParameters 00102 { 00103 public: 00104 Presets() 00105 { 00106 MAXVAL = 0; 00107 T1 = 0; 00108 T2 = 0; 00109 T3 = 0; 00110 RESET = 0; 00111 } 00112 }; 00113 00114 00115 // 00116 // JLSInputStream: minimal implementation to read JPEG header streams 00117 // 00118 class JLSInputStream 00119 { 00120 public: 00121 JLSInputStream(const BYTE* pdata, LONG cbyteLength); 00122 00123 size_t GetBytesRead() 00124 { return _cbyteOffset; } 00125 00126 const JlsParameters& GetMetadata() const 00127 { return _info; } 00128 00129 const JlsCustomParameters& GetCustomPreset() const 00130 { return _info.custom; } 00131 00132 void Read(void* pvoid, LONG cbyteAvailable); 00133 void ReadHeader(); 00134 00135 void EnableCompare(bool bCompare) 00136 { _bCompare = bCompare; } 00137 00138 void SetInfo(JlsParameters* info) { _info = *info; } 00139 00140 void SetRect(JlsRect rect) { _rect = rect; } 00141 00142 private: 00143 void ReadPixels(void* pvoid, LONG cbyteAvailable); 00144 void ReadScan(void*); 00145 void ReadStartOfScan(); 00146 void ReadPresetParameters(); 00147 void ReadComment(); 00148 void ReadStartOfFrame(); 00149 BYTE ReadByte(); 00150 int ReadWord(); 00151 void ReadNBytes(OFVector<char>& dst, int byteCount); 00152 00153 // JFIF 00154 void ReadJfif(); 00155 // Color Transform Application Markers & Code Stream (HP extension) 00156 void ReadColorSpace(); 00157 void ReadColorXForm(); 00158 00159 private: 00160 const BYTE* _pdata; 00161 size_t _cbyteOffset; 00162 size_t _cbyteLength; 00163 bool _bCompare; 00164 JlsParameters _info; 00165 JlsRect _rect; 00166 }; 00167 00168 00169 00170 00171 #endif