?? rawnjl2.cc
字號:
#include <math.h>#include "bnstream.h"#include "bnopt.h"#include "mesgtext.h"#ifdef USEOWNLOG2#if USEOWNLOG2static inline double log2(double x) { return log(x)/log(2); }#endif#endif// Test of new lossless JPEG proposal ISO/IEC WD 14495static inline Uint32 Maximum(Uint32 a,Uint32 b) { return (a > b) ? a : b; }static inline Uint16 Maximum(Uint16 a,Uint16 b) { return (a > b) ? a : b; }static inline Uint32 Minimum(Uint32 a,Uint32 b) { return (a > b) ? b : a; }static inline Uint16 Minimum(Uint16 a,Uint16 b) { return (a > b) ? b : a; }static inline Uint32 Abs(Int32 x) { return (Uint32)((x < 0) ? -x : x); }static inline Uint16 Abs(Int16 x) { return (Uint16)((x < 0) ? -x : x); }static inline double Log(double x) { return (log2(x)); }static inline Uint32 Floor(double x) { return Uint32(floor(x)); }static inline Uint32 Ceiling(double x) { return Uint32(ceil(x)); }static inline Uint32 FloorDivision(Uint32 n,Uint32 d) { return Uint32(floor(double(n)/double(d))); }static inline Uint32 CeilingDivision(Uint32 n,Uint32 d) { return Uint32(ceil(double(n)/double(d))); }// Constant tables for run length codes ...static const Uint16 J[32] = { // Order of run length codes 0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,5,5,6,6,7,7,8,9,10,11,12,13,14,15};static const Uint16 J_rm[32] = { // Length of run length codes (ie. 1<<J[n]) 1u<<0,1u<<0,1u<<0,1u<<0, 1u<<1,1u<<1,1u<<1,1u<<1, 1u<<2,1u<<2,1u<<2,1u<<2, 1u<<3,1u<<3,1u<<3,1u<<3, 1u<<4,1u<<4, 1u<<5,1u<<5, 1u<<6,1u<<6, 1u<<7,1u<<7, 1u<<8, 1u<<9, 1u<<10, 1u<<11, 1u<<12, 1u<<13, 1u<<14, 1u<<15};static Uint32readRow(BinaryInputStream &in, Uint16 *buffer, Uint32 n,Uint16 bpp){ Uint32 count=0; if (bpp <= 8) { while (n-- && in) { unsigned char value; in >> value; *buffer++=Uint16(value); if (in || in.eof()) ++count; } } else { while (n-- && in) { in >> *buffer++; if (in || in.eof()) ++count; } } return (in || in.eof()) ? count : 0;}static BinaryOutputStream &writeRow(BinaryOutputStream &out, Uint16 *buffer, Uint32 n,Uint16 bpp){ if (bpp <= 8) { while (n-- && out) out << (unsigned char)(*buffer++); } else { while (n-- && out) out << *buffer++; } return out;}static Uint32 readBitByteOffset=0;static Int16 readBitCount=0;static unsigned char readBitByte=0;static unsigned char readForwardByte;static bool readHaveForwardByte=false;static BinaryInputStream &readBit(BinaryInputStream &in,Uint32 &bit){ // first bits are read from msb of byte Assert(readBitCount >= 0); if (readBitCount < 1) { ++readBitByteOffset; if (readHaveForwardByte) { readHaveForwardByte=false; readBitByte=readForwardByte; readBitCount=7; // skip the stuffed zero bit (otherwise would have been marker)(hence never 0xff) } else { in.read(&readBitByte,1); if (readBitByte == 0xff) { // could be marker segment or data 0xff with following stuffed zero bit Assert(readHaveForwardByte == false); //while (in.read(&readForwardByte,1) && readForwardByte == 0xff); // skip padding bytes (strings of 0xff) in.read(&readForwardByte,1); if (in) { if ((readForwardByte & 0x80) == 0) { // stuffed zero bit after valid 0xff readHaveForwardByte=true; // the valid 0xff is already in readBitByte readBitCount=8; } else { // marker segment // marker identifier is 0xff00+readForwardBytecerr << "readBitByte=" << hex << unsigned(readBitByte) << dec << endl;cerr << "readForwardByte=" << hex << unsigned(readForwardByte) << dec << endl; Assert(0); // for now } } else { readBitCount=0; // just in case ... will trigger assertion next time return in; // failed miserably (ie. can't be valid JPEG syntax if have 0xff as last byte in file) } } else readBitCount=8; } } bit=(readBitByte>>(--readBitCount)) & 1;//cerr << (bit ? "1" : "0"); return in;}static voiddumpReadBitPosition(void){ cerr << "dumpReadBitPosition: " << (readBitByteOffset-1+(readBitCount ? 0 : 1)) << "." << (readBitCount ? (8-readBitCount) : 0) << endl;}static Uint32 writeBitByteOffset=0;static Uint16 writeBitCount=0;static unsigned char writeBitByte=0;static BinaryOutputStream &writeBit(BinaryOutputStream &out,Uint32 bit){ // first bits are written into msb of byte Assert(writeBitCount<8); writeBitByte=writeBitByte<<1; if (bit) writeBitByte|=1;//cerr << (bit ? "1" : "0"); if (++writeBitCount >= 8) { ++writeBitByteOffset; out.write(&writeBitByte,1); // need to stuff with a following zero bit to distinguish from JPEG marker writeBitCount=(writeBitByte == 0xff) ? 1 : 0; writeBitByte=0; } return out;}static BinaryOutputStream &writeBitFlush(BinaryOutputStream &out){ Assert(writeBitCount<8); writeBitByte=writeBitByte<<(8-writeBitCount); out.write(&writeBitByte,1); writeBitByte=0; writeBitCount=0; return out;}static voiddumpWriteBitPosition(void){ cerr << "dumpWriteBitPosition: " << writeBitByteOffset << "." << writeBitCount << endl;}static BinaryOutputStream &write16BE(BinaryOutputStream &out,Uint16 word){ unsigned char byte; byte=word>>8; out.write(&byte,1); byte=word&0xff; out.write(&byte,1); return out;}static BinaryOutputStream &write8(BinaryOutputStream &out,unsigned char byte){ out.write(&byte,1); return out;}static BinaryInputStream &read16BE(BinaryInputStream &in,Uint16 &value){ unsigned char byte; in.read(&byte,1); value=Uint16(byte)<<8; in.read(&byte,1); value|=byte; return in;}static BinaryInputStream &read8(BinaryInputStream &in,unsigned char &byte){ in.read(&byte,1); return in;}// JPEG Syntax - Marker Segment stuff ....const Uint16 JPEG_MARKER_DNL = 0xffdc;const Uint16 JPEG_MARKER_EOI = 0xffd9;const Uint16 JPEG_MARKER_SOI = 0xffd8;const Uint16 JPEG_MARKER_SOS = 0xffda;// New for JPEG-LS (14495-1:1997)const Uint16 JPEG_MARKER_SOF55 = 0xfff7;const Uint16 JPEG_MARKER_LSE = 0xfff8;const unsigned char JPEG_LSE_ID_L1 = 0x01;const unsigned char JPEG_LSE_ID_L2 = 0x02;const unsigned char JPEG_LSE_ID_L3 = 0x03;const unsigned char JPEG_LSE_ID_L4 = 0x04;static BinaryOutputStream &writeSOI(BinaryOutputStream &out){ write16BE(out,JPEG_MARKER_SOI); return out;}static BinaryOutputStream &writeSOF55(BinaryOutputStream &out,Uint16 P,Uint16 ROWS,Uint16 COLUMNS){ write16BE(out,JPEG_MARKER_SOF55); write16BE(out,11); // length (inclusive of self) Assert(P < 256); write8(out,(unsigned char)P); // sample precision write16BE(out,ROWS); // Y - number of lines write16BE(out,COLUMNS); // X - number of samples per line write8(out,1); // one component per frame only write8(out,1); // component identifier is 1 write8(out,0x11); // no horizontal or vertical sampling factor write8(out,0); // no quantization table used in JPEG-LS return out;}static BinaryOutputStream &writeSOS(BinaryOutputStream &out,Uint16 NEAR){ write16BE(out,JPEG_MARKER_SOS); write16BE(out,8); // length (inclusive of self) write8(out,1); // one component per scan only write8(out,1); // select component 1 write8(out,0); // no mapping table Assert(NEAR < 256); write8(out,(unsigned char)NEAR); // in place of start of spectral selection write8(out,0); // ILV - interleave mode is 0 (none) write8(out,0); // not used in JPEG-LS return out;}static BinaryOutputStream &writeLSE1(BinaryOutputStream &out,Uint16 MAXVAL,Uint16 T1,Uint16 T2,Uint16 T3,Uint16 RESET){ write16BE(out,JPEG_MARKER_LSE); write16BE(out,13); write8(out,JPEG_LSE_ID_L1); write16BE(out,MAXVAL); write16BE(out,T1); write16BE(out,T2); write16BE(out,T3); write16BE(out,RESET); return out;}static BinaryOutputStream &writeEOI(BinaryOutputStream &out){ write16BE(out,JPEG_MARKER_EOI); return out;}static boolreadJPEGMarker(BinaryInputStream &in,Uint16 &marker){ return read16BE(in,marker) && (marker&0xff80) != 0;}static boolreadSOI(BinaryInputStream &in,Uint16 marker){ return marker == JPEG_MARKER_SOI;}static boolreadSOF55(BinaryInputStream &in,Uint16 marker,Uint16 &P,Uint32 &ROWS,Uint32 &COLUMNS){//cerr << "readSOF55:" << endl; Uint16 length; unsigned char precision; Uint16 rows; Uint16 columns; unsigned char ncomponents; unsigned char componentid; unsigned char hvsampling; unsigned char quanttable; return marker == JPEG_MARKER_SOF55 // && (cerr << "readSOF55: JPEG_MARKER_SOF55" << endl) && read16BE(in,length) && length == 11 // && (cerr << "readSOF55: length = " << dec << length << endl) && read8(in,precision) && (P=precision,true) // && (cerr << "readSOF55: P = " << dec << P << endl) && read16BE(in,rows) && (ROWS=rows,true) // && (cerr << "readSOF55: ROWS = " << dec << ROWS << endl) && read16BE(in,columns) && (COLUMNS=columns,true) // && (cerr << "readSOF55: COLUMNS = " << dec << COLUMNS << endl) && read8(in,ncomponents) && ncomponents == 1 // && (cerr << "readSOF55: ncomponents = " << dec << Uint16(ncomponents) << endl) && read8(in,componentid) // && (cerr << "readSOF55: componentid = " << dec << Uint16(componentid) << endl) && read8(in,hvsampling) // && (cerr << "readSOF55: hvsampling = " << dec << Uint16(hvsampling) << endl) && read8(in,quanttable) // && (cerr << "readSOF55: quanttable = " << dec << Uint16(quanttable) << endl) ;}static boolreadSOS(BinaryInputStream &in,Uint16 marker,Uint16 &NEAR){ Uint16 length; unsigned char ncomponents; unsigned char componentid; unsigned char mappingtable; unsigned char near; unsigned char ilv; unsigned char dummy; return marker == JPEG_MARKER_SOS && read16BE(in,length) && length == 8 && read8(in,ncomponents) && ncomponents == 1 && read8(in,componentid) && read8(in,mappingtable) && read8(in,near) && (NEAR=near,true) && read8(in,ilv) && read8(in,dummy);}static boolreadLSE1(BinaryInputStream &in,Uint16 marker,Uint32 &MAXVAL,Uint16 &T1,Uint16 &T2,Uint16 &T3,Uint16 &RESET){ Uint16 length; unsigned char id; Uint16 maxval; return marker == JPEG_MARKER_LSE && read16BE(in,length) && length > 2 && read8(in,id) && id == JPEG_LSE_ID_L1 && length == 13 && read16BE(in,maxval) && (MAXVAL=maxval,true) && read16BE(in,T1) && read16BE(in,T2) && read16BE(in,T3) && read16BE(in,RESET);}// JPEG-LS support routines ...static Uint16determineGolombParameter(Uint32 n,Uint32 a){ Uint16 k;//cerr << "\t\tdetermineGolombParameter: n = " << n << endl;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -