📄 iej2000.pas
字号:
(*
Copyright (c) 1998-2007 by HiComponents. All rights reserved.
This software comes without express or implied warranty.
In no case shall the author be liable for any damage or unwanted behavior of any
computer hardware and/or software.
HiComponents grants you the right to include the compiled component
in your application, whether COMMERCIAL, SHAREWARE, or FREEWARE,
BUT YOU MAY NOT DISTRIBUTE THIS SOURCE CODE OR ITS COMPILED .DCU IN ANY FORM.
ImageEn, IEvolution and ImageEn ActiveX may not be included in any commercial,
shareware or freeware libraries or components.
email: support@hicomponents.com
http://www.hicomponents.com
*)
unit iej2000;
{$R-}
{$Q-}
{$HINTS OFF}
{$I ie.inc}
{$IFDEF IEINCLUDEJPEG2000}
interface
uses Windows, Graphics, classes, sysutils, ImageEnProc, ImageEnIO, hyiedefs, hyieutils;
var
__turboFloat: LongBool = False;
function J2KTryStreamJP2(Stream: TStream): boolean;
function J2KTryStreamJ2K(Stream: TStream): boolean;
procedure J2KReadStream(Stream: TStream; Bitmap: TIEBitmap; var IOParams: TIOParamsVals; var xProgress: TProgressRec; Preview: boolean);
procedure J2KWriteStream(Stream: TStream; Bitmap: TIEBitmap; var IOParams: TIOParamsVals; var xProgress: TProgressRec; format: integer);
implementation
uses IEView;
{$RANGECHECKS OFF}
{$Z4}
type
pjas_stream_t = pointer;
pjas_image_t = pointer;
pjas_matrix_t = pointer;
jas_image_cmptparm_t = record
tlx: dword; //* The x-coordinate of the top-left corner of the component. */
tly: dword; //* The y-coordinate of the top-left corner of the component. */
hstep: dword; //* The horizontal sampling period in units of the reference grid. */
vstep: dword; //* The vertical sampling period in units of the reference grid. */
width: dword; //* The width of the component in samples. */
height: dword; //* The height of the component in samples. */
prec: word; //* The precision of the component sample data. */
sgnd: integer; //* The signedness of the component sample data. */
end;
pjas_image_cmptparm_t = ^jas_image_cmptparm_t;
jas_image_cmptparm_t_array = array[0..8192] of jas_image_cmptparm_t;
pjas_image_cmptparm_t_array = ^jas_image_cmptparm_t_array;
/////// from jas
// initialization
procedure jas_init; external;
// streams
function jas_stream_fopen(filename, omode: pchar): pjas_stream_t; external;
procedure jas_stream_close(jstream: pjas_stream_t); external;
procedure jas_stream_flush(jstream: pjas_stream_t); external;
// Get the format of image data in a stream.
function jas_image_getfmt(jstream: pjas_stream_t): integer; external;
// Create an image from a stream in some specified format.
function jas_image_decode(jstream: pjas_stream_t; infmt: integer; inopts: pchar): pjas_image_t; external;
// Write an image to a stream in a specified format.
function jas_image_encode(image: pjas_image_t; jstream: pjas_stream_t; fmt: integer; optstr: pchar): integer; external;
// Delete a component from an image.
procedure jas_image_delcmpt(image: pjas_image_t; i: word); external;
// Read a rectangular region of an image component.
// The position and size of the rectangular region to be read is specified
// relative to the component's coordinate system.
function jas_image_readcmpt(image: pjas_image_t; cmptno: word; x, y, width, height: integer; data: pjas_matrix_t): integer; external;
// matrix (components)
function jas_matrix_create(numrows, numcols: integer): pjas_matrix_t; external;
procedure jas_matrix_destroy(matrix: pjas_matrix_t); external;
// Deallocate any resources associated with an image.
procedure jas_image_destroy(image: pjas_image_t); external;
// Clear the table of image formats.
procedure jas_image_clearfmts; external;
// Get the ID for the image format with the specified name.
function jas_image_strtofmt(s: pchar): integer; external;
// Get the name of the image format with the specified ID.
function jas_image_fmttostr(fmt: integer): pchar; external;
// Create an image.
function jas_image_create(numcmpts: word; cmptparms: pjas_image_cmptparm_t_array; colormodel: integer): pjas_image_t; external;
function jas_image_create0: pjas_image_t; external;
function jas_image_addcmpt(image: pjas_image_t; cmptno: word; cmptparm: pjas_image_cmptparm_t): integer; external;
function JAS_IMAGE_CT_COLOR(n: integer): integer;
begin
result := ((n) and $7FFF);
end;
function jas_image_getcmptbytype(image: pjas_image_t; ctype: integer): integer; external;
function jas_getdbglevel: integer; external;
function jas_setdbglevel(dbglevel: integer): integer; external;
procedure jas_image_writecmptsample(image: pjas_image_t; cmptno: integer; x, y: integer; v: dword); external;
procedure iejas_image_setcmpttype(image: pjas_image_t; cmptno: integer; ctype: integer); external;
//// end from jas
//// from xlib
// [IMAGE] Get the number of image components.
function iejas_image_numcmpts(image: pjas_image_t): integer; external;
// [IMAGE] The x-coordinate of the top-left corner of the image bounding box.
function iejas_image_getleft(image: pjas_image_t): integer; external;
// [IMAGE] The y-coordinate of the top-left corner of the image bounding box.
function iejas_image_gettop(image: pjas_image_t): integer; external;
// [IMAGE] The x-coordinate of the bottom-right corner of the image bounding box (plus one).
function iejas_image_getright(image: pjas_image_t): integer; external;
// [IMAGE] The y-coordinate of the bottom-right corner of the image bounding box (plus one).
function iejas_image_getbottom(image: pjas_image_t): integer; external;
// [MATRIX] get i,j value of the
function iejas_matrix_get(matrix: pjas_matrix_t; i, j: integer): integer; external;
// [IMAGE] Get the width of a component.
function iejas_image_cmptwidth(image: pjas_image_t; compno: integer): integer; external;
// [IMAGE] Get the height of a component.
function iejas_image_cmptheight(image: pjas_image_t; compno: integer): integer; external;
// [IMAGE] Get the precision of the sample data for a component.
function iejas_image_cmptprec(image: pjas_image_t; compno: integer): integer; external;
// [IMAGE] Get the width of the image in units of the image reference grid.
function iejas_image_width(image: pjas_image_t): integer; external;
// [IMAGE] Get the height of the image in units of the image reference grid.
function iejas_image_height(image: pjas_image_t): integer; external;
// [IMAGE] Get the color model used by the image.
function iejas_image_colorspace(image: pjas_image_t): integer; external;
procedure iejas_image_setcolorspace(image: pjas_image_t; colorspace: integer); external;
// [IMAGE] Get the x-coordinate of the top-left corner of a component.
function iejas_image_cmpttlx(image: pjas_image_t; cmptno: integer): integer; external;
// [IMAGE] Get the y-coordinate of the top-left corner of a component.
function iejas_image_cmpttly(image: pjas_image_t; cmptno: integer): integer; external;
// [IMAGE] Get the horizontal subsampling factor for a component
function iejas_image_cmpthstep(image: pjas_image_t; cmptno: integer): integer; external;
// [IMAGE] Get the vertical subsampling factor for a component.
function iejas_image_cmptvstep(image: pjas_image_t; cmptno: integer): integer; external;
///// end xlib
// color spaces
const
JAS_IMAGE_CS_UNKNOWN = 0; //
const
JAS_IMAGE_CS_GRAY = 1; // Standard Gray
const
JAS_IMAGE_CS_RGB = 2; // Standard RGB
const
JAS_IMAGE_CS_YCBCR = 3; // Standard YCC
// color models
const
JAS_IMAGE_CM_GRAY = 1; // No color model (i.e., grayscale).
const
JAS_IMAGE_CM_RGB = 2; // RGB color model.
const
JAS_IMAGE_CM_YCC = 3; // YCC color model.
// components
const
JAS_IMAGE_CT_RGB_R = 0;
const
JAS_IMAGE_CT_RGB_G = 1;
const
JAS_IMAGE_CT_RGB_B = 2;
const
JAS_IMAGE_CT_YCBCR_Y = 0;
const
JAS_IMAGE_CT_YCBCR_CB = 1;
const
JAS_IMAGE_CT_YCBCR_CR = 2;
const
JAS_IMAGE_CT_GRAY_Y = 0;
const
JPC_NUMAGGCTXS = 1;
const
JPC_NUMZCCTXS = 9;
const
JPC_NUMMAGCTXS = 3;
const
JPC_NUMSCCTXS = 5;
const
JPC_NUMUCTXS = 1;
const
JPC_AGGCTXNO = 0;
const
JPC_ZCCTXNO = (JPC_AGGCTXNO + JPC_NUMAGGCTXS);
const
JPC_MAGCTXNO = (JPC_ZCCTXNO + JPC_NUMZCCTXS);
const
JPC_SCCTXNO = (JPC_MAGCTXNO + JPC_NUMMAGCTXS);
const
JPC_UCTXNO = (JPC_SCCTXNO + JPC_NUMSCCTXS);
const
JPC_NUMCTXS = (JPC_UCTXNO + JPC_NUMUCTXS);
var
_jpc_zcctxnolut: array[0..4 * 256] of integer;
_jpc_spblut: array[0..256] of integer;
_jpc_scctxnolut: array[0..256] of integer;
_jpc_magctxnolut: array[0..4096] of integer;
_jpc_signmsedec: array[0..128] of integer;
_jpc_refnmsedec: array[0..128] of integer;
_jpc_signmsedec0: array[0..128] of integer;
_jpc_refnmsedec0: array[0..128] of integer;
_jpc_mqctxs: array[0..JPC_NUMCTXS] of integer;
function vctocc(i, co, cs: integer): integer;
begin
result := (i - co) div cs;
end;
procedure sdword(var v: dword);
var
p1, p2: pchar;
q: dword;
begin
q := v;
p2 := pchar(pointer(@q));
p1 := pchar(pointer(@v));
p1[0] := p2[3];
p1[1] := p2[2];
p1[2] := p2[1];
p1[3] := p2[0];
end;
procedure sword(var v: word);
var
p1, p2: pchar;
q: word;
begin
q := v;
p2 := pchar(pointer(@q));
p1 := pchar(pointer(@v));
p1[0] := p2[1];
p1[1] := p2[0];
end;
// return true if is a jp2 stream
function J2KTryStreamJP2(Stream: TStream): boolean;
var
base: int64;
dw1, dw2, dw3: dword;
begin
base := Stream.position;
result := false;
// try jp2 - try jp2 signature
Stream.read(dw1, 4); // LBox
Stream.read(dw2, 4); // TBox
Stream.read(dw3, 4); // DBox
sdword(dw1);
sdword(dw2);
sdword(dw3);
if (dw1 = 12) and (dw2 = $6A502020) and (dw3 = $0D0A870A) then
result := true;
//
Stream.position := base;
end;
// return true if is a jpc or j2k stream
function J2KTryStreamJ2K(Stream: TStream): boolean;
var
base: int64;
dw1, dw2, dw3: dword;
w1, w2: word;
begin
base := Stream.position;
result := false;
// try j2k, jpc -SOC, SIZ
Stream.read(w1, 2); // SOC
Stream.read(w2, 2); // SIZ
sword(w1);
sword(w2);
if (w1 = $FF4F) and (w2 = $FF51) then
result := true;
//
Stream.position := base;
end;
(*
// note xml field must be freed
type TIEJ2PInfo = record
// jP\32\32
signature:dword;
// jp2h->ihdr
version_major:byte;
version_minor:byte;
components:word;
height,width:dword;
bpc:byte; // can be 0 if components hasn't some bit depth
compression:byte;
UnkC:byte;
IPR:byte;
// jp2h->ihdr->resc
VRc:double; // samples/meter
HRc:double; // samples/meter
// jp2h->ihdr->resd
VRd:double; // samples/meter
HRd:double; // samples/meter
// XML boxes
xml:TList; // pointer list to zero terminated strings
end;
// create xml TList object (to free it and its elements)
// doesn't read 64 bits boxes
// save stream position
procedure IEJ2PReadInfo(stream:TStream; var info:TIEJ2PInfo);
//
function ReadBox(instream:TStream):boolean;
var
LBox:dword;
TBox:dword;
tmps:TMemoryStream;
VRN:word;
VRD:word;
HRN:word;
HRD:word;
VRE:shortint; // 8 bit signed
HRE:shortint; // 8 bit signed
bb:byte;
xmlitem:pchar;
i:integer;
begin
result:=true;
while instream.position<instream.size do begin
instream.Read(LBox,4); // Box length
sdword(LBox);
instream.Read(TBox,4); // Box type
sdword(TBox);
if (LBox=1) then
result:=false
else begin
if LBox=0 then
LBox:=instream.size-instream.position;
case TBox of
// jP\32\32 : JP2 Signature box
$6A502020: instream.read(info.signature,4);
// jp2h : JP2 Header box (superbox)
$6A703268:
begin
tmps:=TMemoryStream.Create;
tmps.CopyFrom(instream,LBox-8);
tmps.position:=0;
ReadBox(tmps);
tmps.free;
end;
// jp2h->ihdr : Image header box
$69686472:
begin
instream.read(info.height,4); sDWord(info.height);
instream.read(info.width,4); sDWord(info.width);
instream.read(info.components,2); sWord(info.components);
instream.read(info.bpc,1);
instream.read(info.compression,1);
instream.read(info.UnkC,1);
instream.read(info.IPR,1);
end;
// jp2h->res : Resolution box (superbox)
$72657320:
begin
tmps:=TMemoryStream.Create;
tmps.CopyFrom(instream,LBox-8);
tmps.position:=0;
ReadBox(tmps);
tmps.free;
end;
// jp2h->res->resc : Capture resolution box
$72657363:
begin
instream.read(VRN,16); sword(VRN);
instream.read(VRD,16); sword(VRD);
instream.read(HRN,16); sword(HRN);
instream.read(HRD,16); sword(HRD);
instream.read(bb,8); VRE:=not (bb-1);
instream.read(bb,8); HRE:=not (bb-1);
info.VRc:=(VRN/VRD)*IEPower(10,VRE);
info.HRc:=(HRN/HRD)*IEPower(10,HRE);
end;
// jp2h->res->resd : Default Display resolution box
$72657364:
begin
instream.read(VRN,16); sWord(VRN);
instream.read(VRD,16); sWord(VRD);
instream.read(HRN,16); sWord(HRN);
instream.read(HRD,16); sWord(HRD);
instream.read(VRE,8);
instream.read(HRE,8);
info.VRd:=(VRN/VRD)*IEPower(10,VRE);
info.HRd:=(HRN/HRD)*IEPower(10,HRE);
end;
// xml\032 : XML boxes
$786D6C20:
begin
i:=LBox-8+1;
getmem(xmlitem,i);
instream.read(xmlitem^,i-1);
xmlitem[i-1]:=#0;
info.xml.Add(xmlitem);
end;
// unknown box
else
instream.Seek(LBox-8,soFromCurrent); // bypass
end; // end case
end;
end; // end while
end;
//
var
lpos:integer;
begin
fillchar(info,sizeof(TIEJ2PInfo),0);
info.xml:=TList.Create;
lpos:=Stream.position;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -