Geant4.10
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Member Functions | Protected Member Functions | Protected Attributes
G4JpegCoder Class Reference

#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_charmRgb [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
 
G4OutBitStreammOBSP
 

Detailed Description

Definition at line 51 of file G4RTJpegCoder.hh.

Constructor & Destructor Documentation

G4JpegCoder::G4JpegCoder ( u_char colorR,
u_char colorG,
u_char colorB 
)

Definition at line 43 of file G4RTJpegCoder.cc.

References mCosT, mOBSP, mPreDC, mRgb, n, and PaiDiv16.

44 {
45  mRgb[0] = colorR;
46  mRgb[1] = colorG;
47  mRgb[2] = colorB;
48 
49  mPreDC[0] = mPreDC[1] = mPreDC[2] = 0;
50  mOBSP = 0;
51 
52  for(int n=0; n<8; n++)
53  for(int im=0; im<8; im++)
54  mCosT[n][im] = std::cos((2 * im + 1) * n * PaiDiv16);
55 }
const double PaiDiv16
Definition: G4RTJpeg.hh:48
const G4int n
G4OutBitStream * mOBSP
double mCosT[8][8]
u_char * mRgb[3]
G4JpegCoder::~G4JpegCoder ( void  )

Definition at line 57 of file G4RTJpegCoder.cc.

58 {}

Member Function Documentation

void G4JpegCoder::CodeHuffman ( int  cs)
protected

Definition at line 187 of file G4RTJpegCoder.cc.

References G4HuffmanCodeTable::CodeT, mDCTData, mOBSP, mPreDC, n, G4HuffmanCodeTable::numOfElement, G4OutBitStream::SetBits(), G4HuffmanCodeTable::SizeT, and test::v.

Referenced by CodeMCU().

188 {
189  const G4HuffmanCodeTable& dcT = cs ? CDcHuffmanT : YDcHuffmanT;
190  const G4HuffmanCodeTable& acT = cs ? CAcHuffmanT : YAcHuffmanT;
191  const int eobIdx = cs ? CEOBidx : YEOBidx;
192  const int zrlIdx = cs ? CZRLidx : YZRLidx;
193 
194  int diff = mDCTData[0] - mPreDC[cs];
195  mPreDC[cs] = mDCTData[0];
196  int absDiff = std::abs(diff);
197  int dIdx = 0;
198 
199  while(absDiff > 0){
200  absDiff >>= 1;
201  dIdx++;
202  }
203  if(dIdx > dcT.numOfElement)
204  throw(G4IndexError(dcT.numOfElement, dIdx, "CodeHuffman:DC"));
205  mOBSP->SetBits((dcT.CodeT)[dIdx], (dcT.SizeT)[dIdx]);
206 
207  if(dIdx){
208  if(diff < 0)
209  diff--;
210  mOBSP->SetBits(diff, dIdx);
211  }
212 
213  int run = 0;
214  for(int n=1; n<64; n++){
215  int absCoefficient = std::abs( mDCTData[ Zigzag[n] ] );
216  if( absCoefficient ){
217  while( run > 15 ){
218  mOBSP->SetBits((acT.CodeT)[zrlIdx], (acT.SizeT)[zrlIdx]);
219  run -= 16;
220  }
221  int is = 0;
222  while( absCoefficient > 0 ){
223  absCoefficient >>= 1;
224  is++;
225  }
226  int aIdx = run * 10 + is + (run == 15);
227  if( aIdx >= acT.numOfElement )
228  throw( G4IndexError( acT.numOfElement, aIdx, "CodeHuffman:AC" )
229  );
230  mOBSP->SetBits( (acT.CodeT)[aIdx], (acT.SizeT)[aIdx] );
231  int v = mDCTData[ Zigzag[n] ];
232  if( v < 0 )
233  v--;
234  mOBSP->SetBits( v, is );
235  run = 0;
236  }
237  else{
238  if(n == 63)
239  mOBSP->SetBits( (acT.CodeT)[eobIdx], (acT.SizeT)[eobIdx] );
240  else
241  run++;
242  }
243  }
244 }
int mDCTData[64]
const G4int n
G4OutBitStream * mOBSP
void SetBits(int v, int numBits)
void G4JpegCoder::CodeMCU ( void  )
protected

Definition at line 116 of file G4RTJpegCoder.cc.

References CodeHuffman(), ForwardDCT(), mCbBlock, mCrBlock, mYBlock, n, and Quantization().

Referenced by DoCoding().

117 {
118  for(int n=0; n<4; n++){
119  ForwardDCT(mYBlock[n]);
120  Quantization(0);
121  CodeHuffman(0);
122  }
124  Quantization(1);
125  CodeHuffman(1);
126 
128  Quantization(2);
129  CodeHuffman(2);
130 }
void ForwardDCT(int *picData)
void CodeHuffman(int cs)
void Quantization(int cs)
int mCbBlock[64]
const G4int n
int mYBlock[4][64]
int mCrBlock[64]
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().

76 {
77  mNumVUnits = (mProperty.nRow / 16) + ((mProperty.nRow % 16) ? 1 : 0);
78  mNumHUnits = (mProperty.nColumn / 16) + ((mProperty.nColumn % 16) ? 1 : 0);
79 
80  int size = mProperty.nColumn * mProperty.nRow * 3;
81  if(size < 10240)
82  size = 10240;
83 
84  try{
85  mOBSP = new G4OutBitStream(size);
86  WriteHeader();
87  for(int yu=0; yu<mNumVUnits; yu++){
88  for(int xu=0; xu<mNumHUnits; xu++){
89  makeYCC(xu, yu);
90 
91  //mRgb->YCrCb
92  #ifdef GRAY
93  for(int i=0; i<64; i++)
94  mCbBlock[i] = mCrBlock[i] = 0;
95  #endif
96  CodeMCU();
97  }
98  }
99  WriteEOI();
100  return M_NoError;
101  }
102 
103  catch(G4MemoryError &me){
104  return M_RuntimeError;
105  }
106  catch(G4BufferError &be){
107  return M_RuntimeError;
108  }
109  catch(G4IndexError &ie){
110  return M_RuntimeError;
111  }
112 }
void WriteEOI(void)
void WriteHeader(void)
int mCbBlock[64]
G4JpegProperty mProperty
G4OutBitStream * mOBSP
void makeYCC(int ux, int uy)
int mCrBlock[64]
void G4JpegCoder::ForwardDCT ( int picData)
protected

Definition at line 258 of file G4RTJpegCoder.cc.

References DisSqrt2, int(), mCosT, mDCTData, test::v, and test::x.

Referenced by CodeMCU().

259 {
260  for( int v=0; v<8; v++ ){
261  double cv = v ? 1.0 : DisSqrt2;
262  for( int u=0; u<8; u++ ){
263  double cu = u ? 1.0 : DisSqrt2;
264  double sum = 0;
265 
266  for( int y=0; y<8; y++ )
267  for( int x=0; x<8; x++ )
268  sum += picData[ y * 8 + x ] * mCosT[u][x] * mCosT[v][y];
269  mDCTData[ v * 8 + u ] = int( sum * cu * cv / 4 );
270  }
271  }
272 }
int mDCTData[64]
typedef int(XMLCALL *XML_NotStandaloneHandler)(void *userData)
const double DisSqrt2
Definition: G4RTJpeg.hh:47
double mCosT[8][8]
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().

62 {
63  if (mOBSP != 0){
64  *aJpegData = (char*)mOBSP->GetStreamAddress();
65  size = mOBSP->GetStreamSize();
66  }
67  else{
68  *aJpegData = 0;
69  size = 0;
70  }
71 
72 }
G4OutBitStream * mOBSP
int GetStreamSize(void)
u_char * GetStreamAddress(void)
void G4JpegCoder::makeYCC ( int  ux,
int  uy 
)
protected

Definition at line 133 of file G4RTJpegCoder.cc.

References test::b, int(), mCbBlock, mCrBlock, mProperty, mRgb, mYBlock, n, G4JpegProperty::nColumn, G4JpegProperty::nRow, and test::x.

Referenced by DoCoding().

134 {
135  u_char rv, gv, bv;
136  int tCrBlock[4][64];
137  int tCbBlock[4][64];
138 
139  for(int u=0; u<4; u++){
140  int *yp = mYBlock[u];
141  int *cbp = tCbBlock[u];
142  int *crp = tCrBlock[u];
143 
144  int sx = ux * 16 + ((u&1) ? 8 : 0);
145  int ex = sx + 8;
146  int sy = uy * 16 + ((u>1) ? 8 : 0);
147  int ey = sy + 8;
148 
149  for(int iv=sy; iv<ey; iv++){
150  int ii = iv < mProperty.nRow ? iv : mProperty.nRow - 1;
151  for(int ih=sx; ih<ex; ih++){
152  int jj = ih < mProperty.nColumn ? ih : mProperty.nColumn - 1;
153  int index = ii * mProperty.nColumn + jj;
154  rv = mRgb[0][index];
155  gv = mRgb[1][index];
156  bv = mRgb[2][index];
157 
158  *yp++ = int((0.2990 * rv) + (0.5870 * gv) + (0.1140 * bv) - 128)
159 ;
160  *cbp++ = int(-(0.1687 * rv) - (0.3313 * gv) + (0.5000 * bv));
161  *crp++ = int((0.5000 * rv) - (0.4187 * gv) - (0.0813 * bv));
162  } // ih
163  } //iv
164  } //u
165 
166  int n = 0;
167  for(int b=0; b<4; b++){
168  switch(b){
169  case 0: n=0; break;
170  case 1: n=4; break;
171  case 2: n=32; break;
172  case 3: n=36;
173  }
174  for(int y=0; y<8; y+=2){
175  for(int x=0; x<8; x+=2){
176  int idx = y * 8 + x;
177  mCrBlock[n] = tCrBlock[b][idx];
178  mCbBlock[n] = tCbBlock[b][idx];
179  n++;
180  }
181  n += 4;
182  }
183  }
184 }
typedef int(XMLCALL *XML_NotStandaloneHandler)(void *userData)
int mCbBlock[64]
const G4int n
G4JpegProperty mProperty
int mYBlock[4][64]
unsigned char u_char
Definition: G4RTJpeg.hh:40
int mCrBlock[64]
u_char * mRgb[3]
void G4JpegCoder::Quantization ( int  cs)
protected

Definition at line 248 of file G4RTJpegCoder.cc.

References mDCTData.

Referenced by CodeMCU().

249 {
250  int* qt = (int*)(cs ? CQuantumT : YQuantumT);
251  for( int i=0; i<64; i++ ){
252  mDCTData[i] /= qt[i];
253  }
254 }
int mDCTData[64]
void G4JpegCoder::SetJpegProperty ( const G4JpegProperty aProperty)
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().

361 {
362  mOBSP->SetByte( M_Marker );
363  mOBSP->SetByte( M_EOI );
364 }
void SetByte(u_char dat)
G4OutBitStream * mOBSP
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().

277 {
278  int i = 0; //counter
279  //SOI
280  mOBSP->SetByte( M_Marker ); //FF
281  mOBSP->SetByte( M_SOI ); //SOI
282 
283  //APP0(JFIF Header)
284  mOBSP->SetByte( M_Marker ); //FF
285  mOBSP->SetByte( M_APP0 ); //APP0
286  mOBSP->SetWord( JFIFLength ); //parameter
287  mOBSP->CopyByte( (char*)JFIF, 5 ); //"JFIF\0"
288  mOBSP->SetWord( JFIFVersion ); //Version
292  mOBSP->SetByte( 0 );
293  mOBSP->SetByte( 0 );
294 
295  //comment
296  if( mProperty.Comment != 0 ){
297  mOBSP->SetByte( M_Marker ); //FF
298  mOBSP->SetByte( M_COM ); //comment
299  int length = strlen( mProperty.Comment ) + 1;
300  mOBSP->SetWord( length + 2 );
301  mOBSP->CopyByte( mProperty.Comment, length );
302  }
303 
304  //DQT
305  mOBSP->SetByte( M_Marker );
306  mOBSP->SetByte( M_DQT );
307  mOBSP->SetWord( 67 );
308  mOBSP->SetByte( 0 );
309  for( i=0; i<64; i++ )
310  mOBSP->SetByte( u_char( YQuantumT[Zigzag[i]] ) );
311  mOBSP->SetByte( M_Marker );
312  mOBSP->SetByte( M_DQT );
313  mOBSP->SetWord( 67 );
314  mOBSP->SetByte( 1 );
315  for( i=0; i<64; i++ )
316  mOBSP->SetByte( u_char( CQuantumT[Zigzag[i]] ) );
317  // DHT
318  mOBSP->CopyByte( (char*)YDcDht, DcDhtLength );
319  mOBSP->CopyByte( (char*)CDcDht, DcDhtLength );
320  mOBSP->CopyByte( (char*)YAcDht, AcDhtLength );
321  mOBSP->CopyByte( (char*)CAcDht, AcDhtLength );
322 
323  // Frame Header
324  mOBSP->SetByte( M_Marker ); // FF
325  mOBSP->SetByte( M_SOF0 );
326  mOBSP->SetWord( 3 * mProperty.Dimension + 8 );
331 
332  mOBSP->SetByte( 0 );
333  mOBSP->SetByte( YSampleF );
334  mOBSP->SetByte( 0 );
335 
336  mOBSP->SetByte( 1 );
337  mOBSP->SetByte( CSampleF );
338 
339  mOBSP->SetByte( 1 );
340  mOBSP->SetByte( 2 );
341  mOBSP->SetByte( CSampleF );
342  mOBSP->SetByte( 1 );
343 
344  //Scan Header
345  mOBSP->SetByte( M_Marker );
346  mOBSP->SetByte( M_SOS );
347  mOBSP->SetWord( 2 * mProperty.Dimension + 6 );
349  for( i=0; i<mProperty.Dimension; i++ ){
350  mOBSP->SetByte( i );
351  mOBSP->SetByte( i==0 ? 0 : 0x11 );
352  }
353  mOBSP->SetByte( 0 ); //Ss
354  mOBSP->SetByte( 63 ); //Se
355  mOBSP->SetByte( 0 ); //Ah,Al
356 }
const u_char YSampleF
void SetWord(u_int dat)
const u_int JFIFVersion
void SetByte(u_char dat)
int SamplePrecision
Definition: G4RTJpeg.hh:160
G4JpegProperty mProperty
G4OutBitStream * mOBSP
unsigned char u_char
Definition: G4RTJpeg.hh:40
void CopyByte(const char *src, int n)
const u_char CSampleF
const u_int JFIFLength
const char * Comment
Definition: G4RTJpeg.hh:161
const char JFIF[]
Definition: G4RTJpeg.hh:43

Field Documentation

int G4JpegCoder::mCbBlock[64]
protected

Definition at line 67 of file G4RTJpegCoder.hh.

Referenced by CodeMCU(), DoCoding(), and makeYCC().

double G4JpegCoder::mCosT[8][8]
protected

Definition at line 69 of file G4RTJpegCoder.hh.

Referenced by ForwardDCT(), and G4JpegCoder().

int G4JpegCoder::mCrBlock[64]
protected

Definition at line 68 of file G4RTJpegCoder.hh.

Referenced by CodeMCU(), DoCoding(), and makeYCC().

int G4JpegCoder::mDCTData[64]
protected

Definition at line 70 of file G4RTJpegCoder.hh.

Referenced by CodeHuffman(), ForwardDCT(), and Quantization().

int G4JpegCoder::mNumHUnits
protected

Definition at line 75 of file G4RTJpegCoder.hh.

Referenced by DoCoding().

int G4JpegCoder::mNumVUnits
protected

Definition at line 74 of file G4RTJpegCoder.hh.

Referenced by DoCoding().

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

Definition at line 71 of file G4RTJpegCoder.hh.

Referenced by CodeHuffman(), and G4JpegCoder().

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

Definition at line 65 of file G4RTJpegCoder.hh.

Referenced by G4JpegCoder(), and makeYCC().

int G4JpegCoder::mYBlock[4][64]
protected

Definition at line 66 of file G4RTJpegCoder.hh.

Referenced by CodeMCU(), and makeYCC().


The documentation for this class was generated from the following files: