#include <G4RTJpegCoder.hh>
Public Member Functions | |
G4JpegCoder (u_char *colorR, u_char *colorG, u_char *colorB) | |
~G4JpegCoder (void) | |
void | GetJpegData (char **aJpegData, int &size) |
void | SetJpegProperty (const G4JpegProperty &aProperty) |
int | DoCoding (void) |
Protected Member Functions | |
void | CodeMCU () |
void | makeYCC (int ux, int uy) |
void | CodeHuffman (int cs) |
void | ForwardDCT (int *picData) |
void | Quantization (int cs) |
void | WriteHeader (void) |
void | WriteEOI (void) |
Protected Attributes | |
u_char * | mRgb [3] |
int | mYBlock [4][64] |
int | mCbBlock [64] |
int | mCrBlock [64] |
double | mCosT [8][8] |
int | mDCTData [64] |
int | mPreDC [3] |
G4JpegProperty | mProperty |
int | mNumVUnits |
int | mNumHUnits |
G4OutBitStream * | mOBSP |
Definition at line 51 of file G4RTJpegCoder.hh.
Definition at line 43 of file G4RTJpegCoder.cc.
References mCosT, mOBSP, mPreDC, mRgb, CLHEP::detail::n, and PaiDiv16.
00044 { 00045 mRgb[0] = colorR; 00046 mRgb[1] = colorG; 00047 mRgb[2] = colorB; 00048 00049 mPreDC[0] = mPreDC[1] = mPreDC[2] = 0; 00050 mOBSP = 0; 00051 00052 for(int n=0; n<8; n++) 00053 for(int im=0; im<8; im++) 00054 mCosT[n][im] = std::cos((2 * im + 1) * n * PaiDiv16); 00055 }
G4JpegCoder::~G4JpegCoder | ( | void | ) |
void G4JpegCoder::CodeHuffman | ( | int | cs | ) | [protected] |
Definition at line 187 of file G4RTJpegCoder.cc.
References G4HuffmanCodeTable::CodeT, mDCTData, mOBSP, mPreDC, CLHEP::detail::n, G4HuffmanCodeTable::numOfElement, G4OutBitStream::SetBits(), and G4HuffmanCodeTable::SizeT.
Referenced by CodeMCU().
00188 { 00189 const G4HuffmanCodeTable& dcT = cs ? CDcHuffmanT : YDcHuffmanT; 00190 const G4HuffmanCodeTable& acT = cs ? CAcHuffmanT : YAcHuffmanT; 00191 const int eobIdx = cs ? CEOBidx : YEOBidx; 00192 const int zrlIdx = cs ? CZRLidx : YZRLidx; 00193 00194 int diff = mDCTData[0] - mPreDC[cs]; 00195 mPreDC[cs] = mDCTData[0]; 00196 int absDiff = std::abs(diff); 00197 int dIdx = 0; 00198 00199 while(absDiff > 0){ 00200 absDiff >>= 1; 00201 dIdx++; 00202 } 00203 if(dIdx > dcT.numOfElement) 00204 throw(G4IndexError(dcT.numOfElement, dIdx, "CodeHuffman:DC")); 00205 mOBSP->SetBits((dcT.CodeT)[dIdx], (dcT.SizeT)[dIdx]); 00206 00207 if(dIdx){ 00208 if(diff < 0) 00209 diff--; 00210 mOBSP->SetBits(diff, dIdx); 00211 } 00212 00213 int run = 0; 00214 for(int n=1; n<64; n++){ 00215 int absCoefficient = std::abs( mDCTData[ Zigzag[n] ] ); 00216 if( absCoefficient ){ 00217 while( run > 15 ){ 00218 mOBSP->SetBits((acT.CodeT)[zrlIdx], (acT.SizeT)[zrlIdx]); 00219 run -= 16; 00220 } 00221 int is = 0; 00222 while( absCoefficient > 0 ){ 00223 absCoefficient >>= 1; 00224 is++; 00225 } 00226 int aIdx = run * 10 + is + (run == 15); 00227 if( aIdx >= acT.numOfElement ) 00228 throw( G4IndexError( acT.numOfElement, aIdx, "CodeHuffman:AC" ) 00229 ); 00230 mOBSP->SetBits( (acT.CodeT)[aIdx], (acT.SizeT)[aIdx] ); 00231 int v = mDCTData[ Zigzag[n] ]; 00232 if( v < 0 ) 00233 v--; 00234 mOBSP->SetBits( v, is ); 00235 run = 0; 00236 } 00237 else{ 00238 if(n == 63) 00239 mOBSP->SetBits( (acT.CodeT)[eobIdx], (acT.SizeT)[eobIdx] ); 00240 else 00241 run++; 00242 } 00243 } 00244 }
void G4JpegCoder::CodeMCU | ( | ) | [protected] |
Definition at line 116 of file G4RTJpegCoder.cc.
References CodeHuffman(), ForwardDCT(), mCbBlock, mCrBlock, mYBlock, CLHEP::detail::n, and Quantization().
Referenced by DoCoding().
00117 { 00118 for(int n=0; n<4; n++){ 00119 ForwardDCT(mYBlock[n]); 00120 Quantization(0); 00121 CodeHuffman(0); 00122 } 00123 ForwardDCT(mCbBlock); 00124 Quantization(1); 00125 CodeHuffman(1); 00126 00127 ForwardDCT(mCrBlock); 00128 Quantization(2); 00129 CodeHuffman(2); 00130 }
int G4JpegCoder::DoCoding | ( | void | ) |
Definition at line 75 of file G4RTJpegCoder.cc.
References CodeMCU(), M_NoError, M_RuntimeError, makeYCC(), mCbBlock, mCrBlock, mNumHUnits, mNumVUnits, mOBSP, mProperty, G4JpegProperty::nColumn, G4JpegProperty::nRow, WriteEOI(), and WriteHeader().
Referenced by G4RTJpegMaker::CreateFigureFile().
00076 { 00077 mNumVUnits = (mProperty.nRow / 16) + ((mProperty.nRow % 16) ? 1 : 0); 00078 mNumHUnits = (mProperty.nColumn / 16) + ((mProperty.nColumn % 16) ? 1 : 0); 00079 00080 int size = mProperty.nColumn * mProperty.nRow * 3; 00081 if(size < 10240) 00082 size = 10240; 00083 00084 try{ 00085 mOBSP = new G4OutBitStream(size); 00086 WriteHeader(); 00087 for(int yu=0; yu<mNumVUnits; yu++){ 00088 for(int xu=0; xu<mNumHUnits; xu++){ 00089 makeYCC(xu, yu); 00090 00091 //mRgb->YCrCb 00092 #ifdef GRAY 00093 for(int i=0; i<64; i++) 00094 mCbBlock[i] = mCrBlock[i] = 0; 00095 #endif 00096 CodeMCU(); 00097 } 00098 } 00099 WriteEOI(); 00100 return M_NoError; 00101 } 00102 00103 catch(G4MemoryError &me){ 00104 return M_RuntimeError; 00105 } 00106 catch(G4BufferError &be){ 00107 return M_RuntimeError; 00108 } 00109 catch(G4IndexError &ie){ 00110 return M_RuntimeError; 00111 } 00112 }
void G4JpegCoder::ForwardDCT | ( | int * | picData | ) | [protected] |
Definition at line 258 of file G4RTJpegCoder.cc.
References DisSqrt2, mCosT, and mDCTData.
Referenced by CodeMCU().
00259 { 00260 for( int v=0; v<8; v++ ){ 00261 double cv = v ? 1.0 : DisSqrt2; 00262 for( int u=0; u<8; u++ ){ 00263 double cu = u ? 1.0 : DisSqrt2; 00264 double sum = 0; 00265 00266 for( int y=0; y<8; y++ ) 00267 for( int x=0; x<8; x++ ) 00268 sum += picData[ y * 8 + x ] * mCosT[u][x] * mCosT[v][y]; 00269 mDCTData[ v * 8 + u ] = int( sum * cu * cv / 4 ); 00270 } 00271 } 00272 }
void G4JpegCoder::GetJpegData | ( | char ** | aJpegData, | |
int & | size | |||
) |
Definition at line 61 of file G4RTJpegCoder.cc.
References G4OutBitStream::GetStreamAddress(), G4OutBitStream::GetStreamSize(), and mOBSP.
Referenced by G4RTJpegMaker::CreateFigureFile().
00062 { 00063 if (mOBSP != 0){ 00064 *aJpegData = (char*)mOBSP->GetStreamAddress(); 00065 size = mOBSP->GetStreamSize(); 00066 } 00067 else{ 00068 *aJpegData = 0; 00069 size = 0; 00070 } 00071 00072 }
void G4JpegCoder::makeYCC | ( | int | ux, | |
int | uy | |||
) | [protected] |
Definition at line 133 of file G4RTJpegCoder.cc.
References mProperty, mRgb, mYBlock, G4JpegProperty::nColumn, and G4JpegProperty::nRow.
Referenced by DoCoding().
00134 { 00135 u_char rv, gv, bv; 00136 int tCrBlock[4][64]; 00137 int tCbBlock[4][64]; 00138 00139 for(int u=0; u<4; u++){ 00140 int *yp = mYBlock[u]; 00141 int *cbp = tCbBlock[u]; 00142 int *crp = tCrBlock[u]; 00143 00144 int sx = ux * 16 + ((u&1) ? 8 : 0); 00145 int ex = sx + 8; 00146 int sy = uy * 16 + ((u>1) ? 8 : 0); 00147 int ey = sy + 8; 00148 00149 for(int iv=sy; iv<ey; iv++){ 00150 int ii = iv < mProperty.nRow ? iv : mProperty.nRow - 1; 00151 for(int ih=sx; ih<ex; ih++){ 00152 int jj = ih < mProperty.nColumn ? ih : mProperty.nColumn - 1; 00153 int index = ii * mProperty.nColumn + jj; 00154 rv = mRgb[0][index]; 00155 gv = mRgb[1][index]; 00156 bv = mRgb[2][index]; 00157 00158 *yp++ = int((0.2990 * rv) + (0.5870 * gv) + (0.1140 * bv) - 128) 00159 ; 00160 *cbp++ = int(-(0.1687 * rv) - (0.3313 * gv) + (0.5000 * bv)); 00161 *crp++ = int((0.5000 * rv) - (0.4187 * gv) - (0.0813 * bv)); 00162 } // ih 00163 } //iv 00164 } //u 00165 00166 int n = 0; 00167 for(int b=0; b<4; b++){ 00168 switch(b){ 00169 case 0: n=0; break; 00170 case 1: n=4; break; 00171 case 2: n=32; break; 00172 case 3: n=36; 00173 } 00174 for(int y=0; y<8; y+=2){ 00175 for(int x=0; x<8; x+=2){ 00176 int idx = y * 8 + x; 00177 mCrBlock[n] = tCrBlock[b][idx]; 00178 mCbBlock[n] = tCbBlock[b][idx]; 00179 n++; 00180 } 00181 n += 4; 00182 } 00183 } 00184 }
void G4JpegCoder::Quantization | ( | int | cs | ) | [protected] |
Definition at line 248 of file G4RTJpegCoder.cc.
References mDCTData.
Referenced by CodeMCU().
00249 { 00250 int* qt = (int*)(cs ? CQuantumT : YQuantumT); 00251 for( int i=0; i<64; i++ ){ 00252 mDCTData[i] /= qt[i]; 00253 } 00254 }
void G4JpegCoder::SetJpegProperty | ( | const G4JpegProperty & | aProperty | ) |
Definition at line 368 of file G4RTJpegCoder.cc.
References G4JpegProperty::Dimension, G4JpegProperty::Format, G4JpegProperty::HThumbnail, G4JpegProperty::MajorRevisions, G4JpegProperty::MinorRevisions, mProperty, G4JpegProperty::SamplePrecision, and G4JpegProperty::VThumbnail.
Referenced by G4RTJpegMaker::CreateFigureFile().
00369 { 00370 mProperty = aProperty; 00371 mProperty.Dimension = 3; 00372 mProperty.SamplePrecision = 8; 00373 mProperty.Format = 1; 00374 mProperty.MajorRevisions = 1; 00375 mProperty.MinorRevisions = 2; 00376 mProperty.HThumbnail = 0; 00377 mProperty.VThumbnail = 0; 00378 }
void G4JpegCoder::WriteEOI | ( | void | ) | [protected] |
Definition at line 360 of file G4RTJpegCoder.cc.
References M_EOI, M_Marker, mOBSP, and G4OutBitStream::SetByte().
Referenced by DoCoding().
void G4JpegCoder::WriteHeader | ( | void | ) | [protected] |
Definition at line 276 of file G4RTJpegCoder.cc.
References G4JpegProperty::Comment, G4OutBitStream::CopyByte(), CSampleF, G4JpegProperty::Dimension, G4JpegProperty::HDensity, JFIF, JFIFLength, JFIFVersion, M_APP0, M_COM, M_DQT, M_Marker, M_SOF0, M_SOI, M_SOS, mOBSP, mProperty, G4JpegProperty::nColumn, G4JpegProperty::nRow, G4JpegProperty::SamplePrecision, G4OutBitStream::SetByte(), G4OutBitStream::SetWord(), G4JpegProperty::Units, G4JpegProperty::VDensity, and YSampleF.
Referenced by DoCoding().
00277 { 00278 int i = 0; //counter 00279 //SOI 00280 mOBSP->SetByte( M_Marker ); //FF 00281 mOBSP->SetByte( M_SOI ); //SOI 00282 00283 //APP0(JFIF Header) 00284 mOBSP->SetByte( M_Marker ); //FF 00285 mOBSP->SetByte( M_APP0 ); //APP0 00286 mOBSP->SetWord( JFIFLength ); //parameter 00287 mOBSP->CopyByte( (char*)JFIF, 5 ); //"JFIF\0" 00288 mOBSP->SetWord( JFIFVersion ); //Version 00289 mOBSP->SetByte( mProperty.Units ); 00290 mOBSP->SetWord( mProperty.HDensity ); 00291 mOBSP->SetWord( mProperty.VDensity ); 00292 mOBSP->SetByte( 0 ); 00293 mOBSP->SetByte( 0 ); 00294 00295 //comment 00296 if( mProperty.Comment != 0 ){ 00297 mOBSP->SetByte( M_Marker ); //FF 00298 mOBSP->SetByte( M_COM ); //comment 00299 int length = strlen( mProperty.Comment ) + 1; 00300 mOBSP->SetWord( length + 2 ); 00301 mOBSP->CopyByte( mProperty.Comment, length ); 00302 } 00303 00304 //DQT 00305 mOBSP->SetByte( M_Marker ); 00306 mOBSP->SetByte( M_DQT ); 00307 mOBSP->SetWord( 67 ); 00308 mOBSP->SetByte( 0 ); 00309 for( i=0; i<64; i++ ) 00310 mOBSP->SetByte( u_char( YQuantumT[Zigzag[i]] ) ); 00311 mOBSP->SetByte( M_Marker ); 00312 mOBSP->SetByte( M_DQT ); 00313 mOBSP->SetWord( 67 ); 00314 mOBSP->SetByte( 1 ); 00315 for( i=0; i<64; i++ ) 00316 mOBSP->SetByte( u_char( CQuantumT[Zigzag[i]] ) ); 00317 // DHT 00318 mOBSP->CopyByte( (char*)YDcDht, DcDhtLength ); 00319 mOBSP->CopyByte( (char*)CDcDht, DcDhtLength ); 00320 mOBSP->CopyByte( (char*)YAcDht, AcDhtLength ); 00321 mOBSP->CopyByte( (char*)CAcDht, AcDhtLength ); 00322 00323 // Frame Header 00324 mOBSP->SetByte( M_Marker ); // FF 00325 mOBSP->SetByte( M_SOF0 ); 00326 mOBSP->SetWord( 3 * mProperty.Dimension + 8 ); 00327 mOBSP->SetByte( mProperty.SamplePrecision ); 00328 mOBSP->SetWord( mProperty.nRow ); 00329 mOBSP->SetWord( mProperty.nColumn ); 00330 mOBSP->SetByte( mProperty.Dimension ); 00331 00332 mOBSP->SetByte( 0 ); 00333 mOBSP->SetByte( YSampleF ); 00334 mOBSP->SetByte( 0 ); 00335 00336 mOBSP->SetByte( 1 ); 00337 mOBSP->SetByte( CSampleF ); 00338 00339 mOBSP->SetByte( 1 ); 00340 mOBSP->SetByte( 2 ); 00341 mOBSP->SetByte( CSampleF ); 00342 mOBSP->SetByte( 1 ); 00343 00344 //Scan Header 00345 mOBSP->SetByte( M_Marker ); 00346 mOBSP->SetByte( M_SOS ); 00347 mOBSP->SetWord( 2 * mProperty.Dimension + 6 ); 00348 mOBSP->SetByte( mProperty.Dimension ); 00349 for( i=0; i<mProperty.Dimension; i++ ){ 00350 mOBSP->SetByte( i ); 00351 mOBSP->SetByte( i==0 ? 0 : 0x11 ); 00352 } 00353 mOBSP->SetByte( 0 ); //Ss 00354 mOBSP->SetByte( 63 ); //Se 00355 mOBSP->SetByte( 0 ); //Ah,Al 00356 }
int G4JpegCoder::mCbBlock[64] [protected] |
double G4JpegCoder::mCosT[8][8] [protected] |
int G4JpegCoder::mCrBlock[64] [protected] |
int G4JpegCoder::mDCTData[64] [protected] |
Definition at line 70 of file G4RTJpegCoder.hh.
Referenced by CodeHuffman(), ForwardDCT(), and Quantization().
int G4JpegCoder::mNumHUnits [protected] |
int G4JpegCoder::mNumVUnits [protected] |
G4OutBitStream* G4JpegCoder::mOBSP [protected] |
Definition at line 77 of file G4RTJpegCoder.hh.
Referenced by CodeHuffman(), DoCoding(), G4JpegCoder(), GetJpegData(), WriteEOI(), and WriteHeader().
int G4JpegCoder::mPreDC[3] [protected] |
G4JpegProperty G4JpegCoder::mProperty [protected] |
Definition at line 73 of file G4RTJpegCoder.hh.
Referenced by DoCoding(), makeYCC(), SetJpegProperty(), and WriteHeader().
u_char* G4JpegCoder::mRgb[3] [protected] |
int G4JpegCoder::mYBlock[4][64] [protected] |