kviewshell
IW44Image.h
Go to the documentation of this file.00001 //C- -*- C++ -*- 00002 //C- ------------------------------------------------------------------- 00003 //C- DjVuLibre-3.5 00004 //C- Copyright (c) 2002 Leon Bottou and Yann Le Cun. 00005 //C- Copyright (c) 2001 AT&T 00006 //C- 00007 //C- This software is subject to, and may be distributed under, the 00008 //C- GNU General Public License, Version 2. The license should have 00009 //C- accompanied the software or you may obtain a copy of the license 00010 //C- from the Free Software Foundation at http://www.fsf.org . 00011 //C- 00012 //C- This program is distributed in the hope that it will be useful, 00013 //C- but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 //C- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 //C- GNU General Public License for more details. 00016 //C- 00017 //C- DjVuLibre-3.5 is derived from the DjVu(r) Reference Library 00018 //C- distributed by Lizardtech Software. On July 19th 2002, Lizardtech 00019 //C- Software authorized us to replace the original DjVu(r) Reference 00020 //C- Library notice by the following text (see doc/lizard2002.djvu): 00021 //C- 00022 //C- ------------------------------------------------------------------ 00023 //C- | DjVu (r) Reference Library (v. 3.5) 00024 //C- | Copyright (c) 1999-2001 LizardTech, Inc. All Rights Reserved. 00025 //C- | The DjVu Reference Library is protected by U.S. Pat. No. 00026 //C- | 6,058,214 and patents pending. 00027 //C- | 00028 //C- | This software is subject to, and may be distributed under, the 00029 //C- | GNU General Public License, Version 2. The license should have 00030 //C- | accompanied the software or you may obtain a copy of the license 00031 //C- | from the Free Software Foundation at http://www.fsf.org . 00032 //C- | 00033 //C- | The computer code originally released by LizardTech under this 00034 //C- | license and unmodified by other parties is deemed "the LIZARDTECH 00035 //C- | ORIGINAL CODE." Subject to any third party intellectual property 00036 //C- | claims, LizardTech grants recipient a worldwide, royalty-free, 00037 //C- | non-exclusive license to make, use, sell, or otherwise dispose of 00038 //C- | the LIZARDTECH ORIGINAL CODE or of programs derived from the 00039 //C- | LIZARDTECH ORIGINAL CODE in compliance with the terms of the GNU 00040 //C- | General Public License. This grant only confers the right to 00041 //C- | infringe patent claims underlying the LIZARDTECH ORIGINAL CODE to 00042 //C- | the extent such infringement is reasonably necessary to enable 00043 //C- | recipient to make, have made, practice, sell, or otherwise dispose 00044 //C- | of the LIZARDTECH ORIGINAL CODE (or portions thereof) and not to 00045 //C- | any greater extent that may be necessary to utilize further 00046 //C- | modifications or combinations. 00047 //C- | 00048 //C- | The LIZARDTECH ORIGINAL CODE is provided "AS IS" WITHOUT WARRANTY 00049 //C- | OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 00050 //C- | TO ANY WARRANTY OF NON-INFRINGEMENT, OR ANY IMPLIED WARRANTY OF 00051 //C- | MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. 00052 //C- +------------------------------------------------------------------ 00053 // 00054 // $Id: IW44Image.h,v 1.11 2003/11/07 22:08:22 leonb Exp $ 00055 // $Name: release_3_5_15 $ 00056 00057 #ifndef IW44IMAGE_H_ 00058 #define IW44IMAGE_H_ 00059 #ifdef HAVE_CONFIG_H 00060 #include "config.h" 00061 #endif 00062 #if NEED_GNUG_PRAGMAS 00063 # pragma interface 00064 #endif 00065 00066 00165 00166 00167 #include "GSmartPointer.h" 00168 #include "ZPCodec.h" 00169 00170 00171 #ifdef HAVE_NAMESPACES 00172 namespace DJVU { 00173 # ifdef NOT_DEFINED // Just to fool emacs c++ mode 00174 } 00175 #endif 00176 #endif 00177 00178 class GRect; 00179 class IFFByteStream; 00180 class ByteStream; 00181 class GBitmap; 00182 class GPixmap; 00183 00184 00185 00191 struct IWEncoderParms 00192 { 00197 int slices; 00202 int bytes; 00209 float decibels; 00211 IWEncoderParms(void); 00212 }; 00213 00214 00215 00226 class IW44Image : public GPEnabled 00227 { 00228 public: 00245 enum CRCBMode { 00246 CRCBnone, 00247 CRCBhalf, 00248 CRCBnormal, 00249 CRCBfull }; 00250 class Transform; 00251 class Map; 00252 class Block; 00253 class Codec; 00254 struct Alloc; 00255 struct PrimaryHeader; 00256 struct SecondaryHeader; 00257 struct TertiaryHeader; 00258 enum ImageType { 00259 GRAY=false, 00260 COLOR=true }; 00261 protected: 00262 IW44Image(void); 00263 public: 00269 static GP<IW44Image> create_decode(const ImageType itype=COLOR); 00275 static GP<IW44Image> create_encode(const ImageType itype=COLOR); 00276 // virtual destructor 00277 virtual ~IW44Image(); 00282 static GP<IW44Image> create_encode(const GBitmap &bm, const GP<GBitmap> mask=0); 00289 static GP<IW44Image> create_encode(const GPixmap &bm, const GP<GBitmap> mask=0, CRCBMode crcbmode=CRCBnormal); 00290 // ACCESS 00292 int get_width(void) const; 00294 int get_height(void) const; 00297 virtual GP<GBitmap> get_bitmap(void) {return 0;} 00303 virtual GP<GBitmap> get_bitmap(int subsample, const GRect &rect) {return 0;} 00306 virtual GP<GPixmap> get_pixmap(void) {return 0;} 00312 virtual GP<GPixmap> get_pixmap(int subsample, const GRect &rect) {return 0;} 00315 virtual unsigned int get_memory_usage(void) const = 0; 00319 virtual int get_percent_memory(void) const = 0; 00320 // CODER 00326 virtual int encode_chunk(GP<ByteStream> gbs, const IWEncoderParms &parms); 00332 virtual void encode_iff(IFFByteStream &iff, int nchunks, const IWEncoderParms *parms); 00333 // DECODER 00339 virtual int decode_chunk(GP<ByteStream> gbs) = 0; 00344 virtual void decode_iff(IFFByteStream &iff, int maxchunks=999) = 0; 00345 // MISCELLANEOUS 00350 virtual void close_codec(void) = 0; 00357 virtual int get_serial(void) = 0; 00362 virtual int parm_crcbdelay(const int parm) {return parm;} 00369 virtual void parm_dbfrac(float frac) = 0; 00370 protected: 00371 // Parameter 00372 float db_frac; 00373 // Data 00374 Map *ymap, *cbmap, *crmap; 00375 int cslice; 00376 int cserial; 00377 int cbytes; 00378 private: 00379 // Disable assignment semantic 00380 IW44Image(const IW44Image &ref); 00381 IW44Image& operator=(const IW44Image &ref); 00382 }; 00383 00384 #ifdef IW44IMAGE_IMPLIMENTATION 00385 00386 /*x IW44 encoded gray-level image. This class provided functions for managing 00387 a gray level image represented as a collection of IW44 wavelet 00388 coefficients. The coefficients are stored in a memory efficient data 00389 structure. Member function \Ref{get_bitmap} renders an arbitrary segment 00390 of the image into a \Ref{GBitmap}. Member functions \Ref{decode_iff} and 00391 \Ref{encode_iff} read and write DjVu IW44 files (see \Ref{IW44Image.h}). 00392 Both the copy constructor and the copy operator are declared as private 00393 members. It is therefore not possible to make multiple copies of instances 00394 of this class. */ 00395 00396 class IWBitmap : public IW44Image 00397 { 00398 public: 00399 friend class IW44Image; 00400 class Encode; 00401 protected: 00402 /*x Null constructor. Constructs an empty IWBitmap object. This object does 00403 not contain anything meaningful. You must call function \Ref{init}, 00404 \Ref{decode_iff} or \Ref{decode_chunk} to populate the wavelet 00405 coefficient data structure. */ 00406 IWBitmap(void); 00407 public: 00408 //x virtual destructor 00409 virtual ~IWBitmap(); 00410 //x ACCESS 00411 /*x Reconstructs the complete image. The reconstructed image 00412 is then returned as a GBitmap object. */ 00413 virtual GP<GBitmap> get_bitmap(void); 00414 /*x Reconstructs a segment of the image at a given scale. The subsampling 00415 ratio #subsample# must be a power of two between #1# and #32#. Argument 00416 #rect# specifies which segment of the subsampled image should be 00417 reconstructed. The reconstructed image is returned as a GBitmap object 00418 whose size is equal to the size of the rectangle #rect#. */ 00419 virtual GP<GBitmap> get_bitmap(int subsample, const GRect &rect); 00420 /*x Returns the amount of memory used by the wavelet coefficients. This 00421 amount of memory is expressed in bytes. */ 00422 virtual unsigned int get_memory_usage(void) const; 00423 /*x Returns the filling ratio of the internal data structure. Wavelet 00424 coefficients are stored in a sparse array. This function tells what 00425 percentage of bins have been effectively allocated. */ 00426 virtual int get_percent_memory(void) const; 00427 // DECODER 00428 /*x Decodes one data chunk from ByteStream #bs#. Successive calls to 00429 #decode_chunk# decode successive chunks. You must call #close_codec# 00430 after decoding the last chunk of a file. Note that function 00431 #get_bitmap# and #decode_chunk# may be called simultaneously from two 00432 execution threads. */ 00433 virtual int decode_chunk(GP<ByteStream> gbs); 00434 /*x Reads a DjVu IW44 file as a gray level image. This function enters a 00435 composite chunk (identifier #FORM:BM44#), and decodes a maximum of 00436 #maxchunks# data chunks (identifier #BM44#). Data for each chunk is 00437 processed using the function #decode_chunk#. */ 00438 virtual void decode_iff(IFFByteStream &iff, int maxchunks=999); 00439 // MISCELLANEOUS 00440 /*x Resets the encoder/decoder state. The first call to #decode_chunk# or 00441 #encode_chunk# initializes the coder for encoding or decoding. Function 00442 #close_codec# must be called after processing the last chunk in order to 00443 reset the coder and release the associated memory. */ 00444 virtual void close_codec(void); 00445 /*x Returns the chunk serial number. This function returns the serial 00446 number of the last chunk encoded with #encode_chunk# or decoded with 00447 #decode_chunk#. The first chunk always has serial number #1#. Successive 00448 chunks have increasing serial numbers. Value #0# is returned if this 00449 function is called before calling #encode_chunk# or #decode_chunk# or 00450 after calling #close_codec#. */ 00451 virtual int get_serial(void); 00452 /*x Sets the #dbfrac# parameter. This function can be called before 00453 encoding the first IW44 data chunk. Parameter #frac# modifies the 00454 decibel estimation algorithm in such a way that the decibel target only 00455 pertains to the average error of the fraction #frac# of the most 00456 misrepresented 32x32 pixel blocks. Setting arguments #frac# to #1.0# 00457 restores the normal behavior. */ 00458 virtual void parm_dbfrac(float frac); 00459 private: 00460 Codec *ycodec; 00461 }; 00462 00463 00464 /*x IW44 encoded color image. This class provided functions for managing a 00465 color image represented as a collection of IW44 wavelet coefficients. The 00466 coefficients are stored in a memory efficient data structure. Member 00467 function \Ref{get_pixmap} renders an arbitrary segment of the image into a 00468 \Ref{GPixmap}. Member functions \Ref{decode_iff} and \Ref{encode_iff} 00469 read and write DjVu IW44 files (see \Ref{IW44Image.h}). Both the copy 00470 constructor and the copy operator are declared as private members. It is 00471 therefore not possible to make multiple copies of instances of this 00472 class. */ 00473 00474 class IWPixmap : public IW44Image 00475 { 00476 public: 00477 friend class IW44Image; 00478 protected: 00479 class Encode; 00480 /*x Null constructor. Constructs an empty IWPixmap object. This object does 00481 not contain anything meaningful. You must call function \Ref{init}, 00482 \Ref{decode_iff} or \Ref{decode_chunk} to populate the wavelet 00483 coefficient data structure. */ 00484 IWPixmap(void); 00485 public: 00486 // virtual destructor 00487 virtual ~IWPixmap(); 00488 // ACCESS 00489 /*x Reconstructs the complete image. The reconstructed image 00490 is then returned as a GPixmap object. */ 00491 virtual GP<GPixmap> get_pixmap(void); 00492 /*x Reconstructs a segment of the image at a given scale. The subsampling 00493 ratio #subsample# must be a power of two between #1# and #32#. Argument 00494 #rect# specifies which segment of the subsampled image should be 00495 reconstructed. The reconstructed image is returned as a GPixmap object 00496 whose size is equal to the size of the rectangle #rect#. */ 00497 virtual GP<GPixmap> get_pixmap(int subsample, const GRect &rect); 00498 /*x Returns the amount of memory used by the wavelet coefficients. This 00499 amount of memory is expressed in bytes. */ 00500 virtual unsigned int get_memory_usage(void) const; 00501 /*x Returns the filling ratio of the internal data structure. Wavelet 00502 coefficients are stored in a sparse array. This function tells what 00503 percentage of bins have been effectively allocated. */ 00504 virtual int get_percent_memory(void) const; 00505 // DECODER 00506 /*x Decodes one data chunk from ByteStream #bs#. Successive calls to 00507 #decode_chunk# decode successive chunks. You must call #close_codec# 00508 after decoding the last chunk of a file. Note that function 00509 #get_bitmap# and #decode_chunk# may be called simultaneously from two 00510 execution threads. */ 00511 virtual int decode_chunk(GP<ByteStream> gbs); 00512 /*x Reads a DjVu IW44 file as a color image. This function enters a 00513 composite chunk (identifier #FORM:PM44# or #FORM:BM44#), and decodes a 00514 maximum of #maxchunks# data chunks (identifier #PM44# or #BM44#). Data 00515 for each chunk is processed using the function #decode_chunk#. */ 00516 virtual void decode_iff(IFFByteStream &iff, int maxchunks=999); 00517 // MISCELLANEOUS 00518 /*x Resets the encoder/decoder state. The first call to #decode_chunk# or 00519 #encode_chunk# initializes the coder for encoding or decoding. Function 00520 #close_codec# must be called after processing the last chunk in order to 00521 reset the coder and release the associated memory. */ 00522 virtual void close_codec(void); 00523 /*x Returns the chunk serial number. This function returns the serial 00524 number of the last chunk encoded with #encode_chunk# or decoded with 00525 #decode_chunk#. The first chunk always has serial number #1#. Successive 00526 chunks have increasing serial numbers. Value #0# is returned if this 00527 function is called before calling #encode_chunk# or #decode_chunk# or 00528 after calling #close_codec#. */ 00529 virtual int get_serial(void); 00530 /*x Sets the chrominance delay parameter. This function can be called 00531 before encoding the first IW44 data chunk. Parameter #parm# is an 00532 encoding delay which reduces the bitrate associated with the 00533 chrominance information. The default chrominance encoding delay is 10. */ 00534 virtual int parm_crcbdelay(const int parm); 00535 /*x Sets the #dbfrac# parameter. This function can be called before 00536 encoding the first IW44 data chunk. Parameter #frac# modifies the 00537 decibel estimation algorithm in such a way that the decibel target only 00538 pertains to the average error of the fraction #frac# of the most 00539 misrepresented 32x32 pixel blocks. Setting arguments #frac# to #1.0# 00540 restores the normal behavior. */ 00541 virtual void parm_dbfrac(float frac); 00542 protected: 00543 // Parameter 00544 int crcb_delay; 00545 int crcb_half; 00546 // Data 00547 private: 00548 Codec *ycodec, *cbcodec, *crcodec; 00549 }; 00550 00551 /*x IW44Transform. 00552 */ 00553 class IW44Image::Transform 00554 { 00555 public: 00556 class Decode; 00557 class Encode; 00558 protected: 00559 static void filter_begin(int w, int h); 00560 static void filter_end(void); 00561 }; 00562 00563 struct GPixel; 00564 class IW44Image::Transform::Decode : public IW44Image::Transform 00565 { 00566 public: 00567 // WAVELET TRANSFORM 00568 /*x Forward transform. */ 00569 static void backward(short *p, int w, int h, int rowsize, int begin, int end); 00570 00571 // COLOR TRANSFORM 00572 /*x Converts YCbCr to RGB. */ 00573 static void YCbCr_to_RGB(GPixel *p, int w, int h, int rowsize); 00574 }; 00575 00576 //--------------------------------------------------------------- 00577 // *** Class IW44Image::Block [declaration] 00578 // Represents a block of 32x32 coefficients after zigzagging and scaling 00579 00580 00581 class IW44Image::Block // DJVU_CLASS 00582 { 00583 public: 00584 // creating 00585 Block(void); 00586 // accessing scaled coefficients 00587 short get(int n) const; 00588 void set(int n, int val, IW44Image::Map *map); 00589 // converting from liftblock 00590 void read_liftblock(const short *coeff, IW44Image::Map *map); 00591 void write_liftblock(short *coeff, int bmin=0, int bmax=64) const; 00592 // sparse array access 00593 const short* data(int n) const; 00594 short* data(int n, IW44Image::Map *map); 00595 void zero(int n); 00596 // sparse representation 00597 private: 00598 short **(pdata[4]); 00599 }; 00600 00601 //--------------------------------------------------------------- 00602 // *** Class IW44Image::Map [declaration] 00603 // Represents all the blocks of an image 00604 00605 class IW44Image::Map // DJVU_CLASS 00606 { 00607 public: 00608 class Encode; 00609 00610 // construction 00611 Map(int w, int h); 00612 ~Map(); 00613 // image access 00614 void image(signed char *img8, int rowsize, 00615 int pixsep=1, int fast=0); 00616 void image(int subsample, const GRect &rect, 00617 signed char *img8, int rowsize, 00618 int pixsep=1, int fast=0); 00619 // array of blocks 00620 IW44Image::Block *blocks; 00621 // geometry 00622 int iw, ih; 00623 int bw, bh; 00624 int nb; 00625 // coefficient allocation stuff 00626 short *alloc(int n); 00627 short **allocp(int n); 00628 IW44Image::Alloc *chain; 00629 int top; 00630 // statistics 00631 int get_bucket_count(void) const; 00632 unsigned int get_memory_usage(void) const; 00633 }; 00634 00636 // ENCODING/DECODING WAVELET COEFFICIENTS 00637 // USING HIERARCHICAL SET DIFFERENCE 00639 00640 00641 //----------------------------------------------- 00642 // Class IW44Image::Codec [declaration+implementation] 00643 // Maintains information shared while encoding or decoding 00644 00645 class IW44Image::Codec 00646 { 00647 public: 00648 class Decode; 00649 class Encode; 00650 00651 protected: 00652 // Construction 00653 Codec(IW44Image::Map &map); 00654 public: 00655 virtual ~Codec(); 00656 // Coding 00657 int finish_code_slice(ZPCodec &zp); 00658 virtual int code_slice(ZPCodec &zp) = 0; 00659 // Data 00660 IW44Image::Map ↦ // working map 00661 // status 00662 int curband; // current band 00663 int curbit; // current bitplane 00664 // quantization tables 00665 int quant_hi[10]; // quantization for bands 1 to 9 00666 int quant_lo[16]; // quantization for band 0. 00667 // bucket state 00668 char coeffstate[256]; 00669 char bucketstate[16]; 00670 enum { ZERO = 1, // this coeff never hits this bit 00671 ACTIVE = 2, // this coeff is already active 00672 NEW = 4, // this coeff is becoming active 00673 UNK = 8 }; // this coeff may become active 00674 // coding context 00675 BitContext ctxStart [32]; 00676 BitContext ctxBucket[10][8]; 00677 BitContext ctxMant; 00678 BitContext ctxRoot; 00679 // helper 00680 int is_null_slice(int bit, int band); 00681 int decode_prepare(int fbucket, int nbucket, IW44Image::Block &blk); 00682 void decode_buckets(ZPCodec &zp, int bit, int band, 00683 IW44Image::Block &blk, int fbucket, int nbucket); 00684 }; 00685 00687 // DEFINITION OF CHUNK HEADERS 00689 00690 00691 struct IW44Image::PrimaryHeader { 00692 unsigned char serial; 00693 unsigned char slices; 00694 void encode(GP<ByteStream> gbs); 00695 void decode(GP<ByteStream> gbs); 00696 }; 00697 00698 struct IW44Image::SecondaryHeader { 00699 unsigned char major; 00700 unsigned char minor; 00701 void encode(GP<ByteStream> gbs); 00702 void decode(GP<ByteStream> gbs); 00703 }; 00704 00705 struct IW44Image::TertiaryHeader { 00706 unsigned char xhi, xlo; 00707 unsigned char yhi, ylo; 00708 unsigned char crcbdelay; 00709 void encode(GP<ByteStream> gbs); 00710 void decode(GP<ByteStream> gbs, int major=1, int minor=2); 00711 }; 00712 00713 inline const short* 00714 IW44Image::Block::data(int n) const 00715 { 00716 if (! pdata[n>>4]) 00717 return 0; 00718 return pdata[n>>4][n&15]; 00719 } 00720 00721 inline short* 00722 IW44Image::Block::data(int n, IW44Image::Map *map) 00723 { 00724 if (! pdata[n>>4]) 00725 pdata[n>>4] = map->allocp(16); 00726 if (! pdata[n>>4][n &15]) 00727 pdata[n>>4][n &15] = map->alloc(16); 00728 return pdata[n>>4][n&15]; 00729 } 00730 00731 inline short 00732 IW44Image::Block::get(int n) const 00733 { 00734 int n1 = (n>>4); 00735 const short *d = data(n1); 00736 if (! d) 00737 return 0; 00738 return d[n&15]; 00739 } 00740 00741 inline void 00742 IW44Image::Block::set(int n, int val, IW44Image::Map *map) 00743 { 00744 int n1 = (n>>4); 00745 short* d = data(n1, map); 00746 d[n&15] = val; 00747 } 00748 00749 #endif /* IW44IMAGE_IMPLIMENTATION */ 00750 00752 00753 00754 #ifdef HAVE_NAMESPACES 00755 } 00756 # ifndef NOT_USING_DJVU_NAMESPACE 00757 using namespace DJVU; 00758 # endif 00759 #endif 00760 #endif 00761