OpenJpeg.pas 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  1. (*
  2. * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
  3. * Copyright (c) 2002-2007, Professor Benoit Macq
  4. * Copyright (c) 2001-2003, David Janssens
  5. * Copyright (c) 2002-2003, Yannick Verschueren
  6. * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
  7. * Copyright (c) 2005, Herve Drolon, FreeImage Team
  8. * Copyright (c) 2006-2007, Parvatha Elangovan
  9. * All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
  21. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  24. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. * POSSIBILITY OF SUCH DAMAGE.
  31. *)
  32. {
  33. PasOpenJpeg
  34. Free JPEG 2000 library for Delphi and Free Pascal
  35. Headers translated to Object Pascal and C code precompiled
  36. by Marek Mauder (http://galfar.vevb.net)
  37. for Vampyre Imaging Library (http://imaginglib.sourceforge.net).
  38. Supported compilers and platforms:
  39. Delphi: Windows 32b
  40. FPC: Windows 32b, Linux 32+64b, OSX 32b
  41. OpenJpeg Homepage: http://www.openjpeg.org
  42. PasOpenJpeg Homepage: http://galfar.vevb.net/openjpeg
  43. Current Version: 1.05 (OpenJpeg 1.3 SVN revision 611 with CDEF/PCLR patch)
  44. History:
  45. v1.05 (2010-08-12):
  46. - added palette support
  47. - added CMYK support
  48. v1.04 (2010-06-08):
  49. - added few Pascal-looking type aliases
  50. v1.03 (2009-06-04):
  51. - added Mac OSX x86 support
  52. v1.02 (2009-01-30):
  53. - removed linking to stdc++ lib in LINUX/UNIX
  54. v1.01 (2008-12-27):
  55. - Delphi 2009 compatibility checks
  56. v1.00 (2008-03-01):
  57. - CDEF patch for OpenJpeg, added component types
  58. }
  59. unit OpenJpeg;
  60. {$IFDEF FPC}
  61. { Free Pascal settings }
  62. {$MODE OBJFPC}
  63. {$PACKRECORDS 8}
  64. {$PACKENUM 4}
  65. {$ELSE}
  66. { Delphi settings }
  67. {$DEFINE DCC}
  68. {$ALIGN 8}
  69. {$MINENUMSIZE 4}
  70. {$IFDEF CPUX64}
  71. {$MESSAGE FATAL 'Not supported' }
  72. {$ENDIF}
  73. {$ENDIF}
  74. interface
  75. const
  76. OPENJPEG_VERSION = '1.3.0';
  77. type
  78. Bool = ByteBool;
  79. Char = AnsiChar;
  80. { Constant Definitions }
  81. const
  82. { Maximum allowed size for filenames }
  83. OPJ_PATH_LEN = 4096;
  84. { Number of maximum resolution level authorized }
  85. J2K_MAXRLVLS = 33;
  86. { Number of maximum sub-band linked to number of resolution level }
  87. J2K_MAXBANDS = 3 * J2K_MAXRLVLS - 2;
  88. JPWL_MAX_NO_TILESPECS = 16;
  89. JPWL_MAX_NO_PACKSPECS = 16;
  90. JPWL_MAX_NO_MARKERS = 512;
  91. JPWL_PRIVATEINDEX_NAME = 'jpwl_index_privatefilename';
  92. JPWL_EXPECTED_COMPONENTS = 3;
  93. JPWL_MAXIMUM_TILES = 8192;
  94. JPWL_MAXIMUM_HAMMING = 2;
  95. JPWL_MAXIMUM_EPB_ROOM = 65450;
  96. { Enum Definitions }
  97. type
  98. { Rsiz capabilities }
  99. OPJ_RSIZ_CAPABILITIES = (
  100. STD_RSIZ = 0, { Standard JPEG2000 profile }
  101. CINEMA2K = 3, { Profile name for a 2K image }
  102. CINEMA4K = 4 { Profile name for a 4K image }
  103. );
  104. { Digital cinema operation mode }
  105. OPJ_CINEMA_MODE = (
  106. OFF = 0, { Not Digital Cinema }
  107. CINEMA2K_24 = 1, { 2K Digital Cinema at 24 fps }
  108. CINEMA2K_48 = 2, { 2K Digital Cinema at 48 fps }
  109. CINEMA4K_24 = 3 { 4K Digital Cinema at 24 fps }
  110. );
  111. { Progression order }
  112. OPJ_PROG_ORDER = (
  113. PROG_UNKNOWN = -1, { place-holder }
  114. LRCP = 0, { layer-resolution-component-precinct order }
  115. RLCP = 1, { resolution-layer-component-precinct order }
  116. RPCL = 2, { resolution-precinct-component-layer order }
  117. PCRL = 3, { precinct-component-resolution-layer order }
  118. CPRL = 4 { component-precinct-resolution-layer order }
  119. );
  120. { Supported image color spaces }
  121. OPJ_COLOR_SPACE = (
  122. CLRSPC_UNKNOWN = -1, { place-holder }
  123. CLRSPC_SRGB = 1, { sRGB }
  124. CLRSPC_GRAY = 2, { grayscale }
  125. CLRSPC_SYCC = 3, { YUV }
  126. CLRSPC_CMYK = 4 { CMYK }
  127. );
  128. TOpjColorSpace = OPJ_COLOR_SPACE;
  129. { Supported image component types - added by patch }
  130. OPJ_COMPONENT_TYPE = (
  131. COMPTYPE_UNKNOWN = 0, { unknown component type, cdef box not present }
  132. COMPTYPE_R = 1, { red component of sRGB image }
  133. COMPTYPE_G = 2, { green component of sRGB image }
  134. COMPTYPE_B = 3, { blue component of sRGB image }
  135. COMPTYPE_L = 4, { luminance component of YUV and grayscale images }
  136. COMPTYPE_CB = 5, { Cb component of YUV image }
  137. COMPTYPE_CR = 6, { Cr component of YUV image }
  138. COMPTYPE_OPACITY = 7, { opacity/alpha channel }
  139. COMPTYPE_C = 8, { C component of CMYK image }
  140. COMPTYPE_M = 9, { M component of CMYK image }
  141. COMPTYPE_Y = 10, { Y component of CMYK image }
  142. COMPTYPE_K = 11 { K component of CMYK image }
  143. );
  144. TOpjComponentType = OPJ_COMPONENT_TYPE;
  145. { Supported codec }
  146. OPJ_CODEC_FORMAT = (
  147. CODEC_UNKNOWN = -1, { place-holder }
  148. CODEC_J2K = 0, { JPEG-2000 codestream : read/write }
  149. CODEC_JPT = 1, { JPT-stream (JPEG 2000, JPIP) : read only }
  150. CODEC_JP2 = 2 { JPEG-2000 file format : read/write }
  151. );
  152. { Limit decoding to certain portions of the codestream. }
  153. OPJ_LIMIT_DECODING = (
  154. NO_LIMITATION = 0, { No limitation for the decoding. The entire codestream will de decoded }
  155. LIMIT_TO_MAIN_HEADER = 1, { The decoding is limited to the Main Header }
  156. DECODE_ALL_BUT_PACKETS = 2 { Decode everything except the JPEG 2000 packets }
  157. );
  158. { Event Manager Type Definitions }
  159. { Callback function prototype for events }
  160. opj_msg_callback = procedure(msg: PAnsiChar; client_data: Pointer); cdecl;
  161. { Message handler object }
  162. opj_event_mgr = record
  163. error_handler: opj_msg_callback; { Error message callback if available, NULL otherwise }
  164. warning_handler: opj_msg_callback; { Warning message callback if available, NULL otherwise }
  165. info_handler: opj_msg_callback; { Debug message callback if available, NULL otherwise }
  166. end;
  167. opj_event_mgr_t = opj_event_mgr;
  168. popj_event_mgr_t = ^opj_event_mgr_t;
  169. { Codec Type Definitions }
  170. { Progression order changes }
  171. opj_poc = record
  172. resno0, compno0: Integer;
  173. layno1, resno1, compno1: Integer;
  174. layno0, precno0, precno1: Integer;
  175. prg1, prg: OPJ_PROG_ORDER;
  176. progorder: array[0..4] of Char;
  177. tile: Integer;
  178. tx0, tx1, ty0, ty1: Integer;
  179. layS, resS, compS, prcS: Integer;
  180. layE, resE, compE, prcE: Integer;
  181. txS, txE, tyS, tyE, dx, dy: Integer;
  182. lay_t, res_t, comp_t, prc_t, tx0_t, ty0_t: Integer;
  183. end;
  184. opj_poc_t = opj_poc;
  185. { Compression parameters }
  186. opj_cparameters = record
  187. tile_size_on: Bool;
  188. cp_tx0: Integer;
  189. cp_ty0: Integer;
  190. cp_tdx: Integer;
  191. cp_tdy: Integer;
  192. cp_disto_alloc: Integer;
  193. cp_fixed_alloc: Integer;
  194. cp_fixed_quality: Integer;
  195. cp_matrice: PInteger;
  196. cp_comment: PAnsiChar;
  197. csty: Integer;
  198. prog_order: OPJ_PROG_ORDER;
  199. POC: array[0..31] of opj_poc_t;
  200. numpocs: Integer;
  201. tcp_numlayers: Integer;
  202. tcp_rates: array[0..99] of Single;
  203. tcp_distoratio: array[0..99] of Single;
  204. numresolution: Integer;
  205. cblockw_init: Integer;
  206. cblockh_init: Integer;
  207. mode: Integer;
  208. irreversible: Integer;
  209. roi_compno: Integer;
  210. roi_shift: Integer;
  211. res_spec: Integer;
  212. prcw_init: array[0..J2K_MAXRLVLS - 1] of Integer;
  213. prch_init: array[0..J2K_MAXRLVLS - 1] of Integer;
  214. infile: array[0..OPJ_PATH_LEN - 1] of Char;
  215. outfile: array[0..OPJ_PATH_LEN - 1] of Char;
  216. index_on: Integer;
  217. index: array[0..OPJ_PATH_LEN - 1] of Char;
  218. image_offset_x0: Integer;
  219. image_offset_y0: Integer;
  220. subsampling_dx: Integer;
  221. subsampling_dy: Integer;
  222. decod_format: Integer;
  223. cod_format: Integer;
  224. jpwl_epc_on: Bool;
  225. jpwl_hprot_MH: Integer;
  226. jpwl_hprot_TPH_tileno: array[0..JPWL_MAX_NO_TILESPECS - 1] of Integer;
  227. jpwl_hprot_TPH: array[0..JPWL_MAX_NO_TILESPECS - 1] of Integer;
  228. jpwl_pprot_tileno: array[0..JPWL_MAX_NO_PACKSPECS - 1] of Integer;
  229. jpwl_pprot_packno: array[0..JPWL_MAX_NO_PACKSPECS - 1] of Integer;
  230. jpwl_pprot: array[0..JPWL_MAX_NO_PACKSPECS - 1] of Integer;
  231. jpwl_sens_size: Integer;
  232. jpwl_sens_addr: Integer;
  233. jpwl_sens_range: Integer;
  234. jpwl_sens_MH: Integer;
  235. jpwl_sens_TPH_tileno: array[0..JPWL_MAX_NO_TILESPECS - 1] of Integer;
  236. jpwl_sens_TPH: array[0..JPWL_MAX_NO_TILESPECS - 1] of Integer;
  237. cp_cinema: OPJ_CINEMA_MODE;
  238. max_comp_size: Integer;
  239. cp_rsiz: OPJ_RSIZ_CAPABILITIES;
  240. tp_on: Byte;
  241. tp_flag: Byte;
  242. tcp_mct: Byte;
  243. end;
  244. opj_cparameters_t = opj_cparameters;
  245. popj_cparameters_t = ^opj_cparameters_t;
  246. TOpjCParameters = opj_cparameters_t;
  247. { Decompression parameters }
  248. opj_dparameters = record
  249. cp_reduce: Integer;
  250. cp_layer: Integer;
  251. infile: array[0..OPJ_PATH_LEN - 1] of Char;
  252. outfile: array[0..OPJ_PATH_LEN - 1] of Char;
  253. decod_format: Integer;
  254. cod_format: Integer;
  255. jpwl_correct: Bool;
  256. jpwl_exp_comps: Integer;
  257. jpwl_max_tiles: Integer;
  258. cp_limit_decoding: OPJ_LIMIT_DECODING;
  259. end;
  260. opj_dparameters_t = opj_dparameters;
  261. popj_dparameters_t = ^opj_dparameters_t;
  262. TOpjDParameters = opj_dparameters_t;
  263. { Routines that are to be used by both halves of the library are declared
  264. to receive a Pointer to this structure. There are no actual instances of
  265. opj_common_struct_t, only of opj_cinfo_t and opj_dinfo_t. }
  266. opj_common_struct = record
  267. event_mgr: popj_event_mgr_t; { Pointer to the event manager }
  268. client_data: Pointer; { Available for use by application }
  269. is_decompressor: Bool; { So common code can tell which is which }
  270. codec_format: OPJ_CODEC_FORMAT; { selected codec }
  271. j2k_handle: Pointer; { Pointer to the J2K codec }
  272. jp2_handle: Pointer; { Pointer to the JP2 codec }
  273. mj2_handle: Pointer;
  274. end;
  275. opj_common_struct_t = opj_common_struct;
  276. opj_common_ptr = ^opj_common_struct_t;
  277. { Compression context info }
  278. opj_cinfo = record
  279. event_mgr: popj_event_mgr_t;
  280. client_data: Pointer;
  281. is_decompressor: Bool;
  282. codec_format: OPJ_CODEC_FORMAT;
  283. j2k_handle: Pointer;
  284. jp2_handle: Pointer;
  285. mj2_handle: Pointer;
  286. end;
  287. opj_cinfo_t = opj_cinfo;
  288. popj_cinfo_t = ^opj_cinfo_t;
  289. TOpjCInfo = opj_cinfo_t;
  290. POpjCInfo = popj_cinfo_t;
  291. { Decompression context info }
  292. opj_dinfo = record
  293. event_mgr: popj_event_mgr_t;
  294. client_data: Pointer;
  295. is_decompressor: Bool;
  296. codec_format: OPJ_CODEC_FORMAT;
  297. j2k_handle: Pointer;
  298. jp2_handle: Pointer;
  299. mj2_handle: Pointer;
  300. end;
  301. opj_dinfo_t = opj_dinfo;
  302. popj_dinfo_t = ^opj_dinfo_t;
  303. TOpjDInfo = opj_dinfo_t;
  304. POpjDInfo = popj_dinfo_t;
  305. { I/O Stream Types Definitions }
  306. const
  307. { Stream open flags }
  308. { The stream was opened for reading }
  309. OPJ_STREAM_READ = $0001;
  310. { The stream was opened for writing }
  311. OPJ_STREAM_WRITE = $0002;
  312. type
  313. { Byte input-output stream (CIO) }
  314. opj_cio = record
  315. cinfo: opj_common_ptr; { codec context }
  316. openmode: Integer; { open mode (read/write) either OPJ_STREAM_READ or OPJ_STREAM_WRITE }
  317. buffer: PAnsiChar; { Pointer to the start of the buffer }
  318. length: Integer; { buffer size in bytes }
  319. start: PAnsiChar; { Pointer to the start of the stream }
  320. end_: PAnsiChar; { Pointer to the end of the stream }
  321. bp: PAnsiChar; { Pointer to the current position }
  322. end;
  323. opj_cio_t = opj_cio;
  324. popj_cio_t = ^opj_cio_t;
  325. TOpjCio = opj_cio_t;
  326. POpjCio = popj_cio_t;
  327. { Image Type Definitions }
  328. { Defines a single image component }
  329. opj_image_comp = record
  330. dx: Integer; { XRsiz: horizontal separation of a sample of ith component with respect to the reference grid }
  331. dy: Integer; { YRsiz: vertical separation of a sample of ith component with respect to the reference grid }
  332. w: Integer; { data width }
  333. h: Integer; { data height }
  334. x0: Integer; { x component offset compared to the whole image }
  335. y0: Integer; { y component offset compared to the whole image }
  336. prec: Integer; { precision }
  337. bpp: Integer; { image depth in bits }
  338. sgnd: Integer; { signed (1) / unsigned (0) }
  339. resno_decoded: Integer; { number of decoded resolution }
  340. factor: Integer; { number of division by 2 of the out image compared to the original size of image }
  341. comp_type: OPJ_COMPONENT_TYPE; { type of this component: color channel, opacity, ... }
  342. data: PIntegerArray; { image component data }
  343. end;
  344. opj_image_comp_t = opj_image_comp;
  345. popj_image_comp_t = ^opj_image_comp_t;
  346. opj_image_comp_array = array[0..255] of opj_image_comp_t;
  347. popj_image_comp_array = ^opj_image_comp_array;
  348. TOpjImageComp = opj_image_comp_t;
  349. POpjImageComp = popj_image_comp_t;
  350. { Defines image palette - added by patch }
  351. opj_image_palette = record
  352. hascmap: Integer; { set to one if the original image had a component mapping box }
  353. haspalette: Integer; { set to one if the original image had a palette color box }
  354. numchans: Integer; { number of channels the palette has }
  355. numentrs: Integer; { number of entries the palette has }
  356. sizentr: Integer; { size of one entry for one channel (in bytes) }
  357. paldata: PByte; { byte pointer to the palette data }
  358. end;
  359. opj_image_palette_t = opj_image_palette;
  360. popj_image_palette_t = ^opj_image_palette_t;
  361. { Defines image data and characteristics }
  362. opj_image = record
  363. x0: Integer; { XOsiz: horizontal offset from the origin of the reference grid to the left side of the image area }
  364. y0: Integer; { YOsiz: vertical offset from the origin of the reference grid to the top side of the image area }
  365. x1: Integer; { Xsiz: width of the reference grid }
  366. y1: Integer; { Ysiz: height of the reference grid }
  367. numcomps: Integer; { number of components in the image }
  368. color_space: OPJ_COLOR_SPACE; { color space: sRGB, Greyscale or YUV }
  369. comps: popj_image_comp_array; { image components }
  370. palette: popj_image_palette_t; { palette structure }
  371. end;
  372. opj_image_t = opj_image;
  373. popj_image_t = ^opj_image_t;
  374. TOpjImage = opj_image_t;
  375. POpjImage = popj_image_t;
  376. { Component parameters structure used by the opj_image_create function }
  377. opj_image_comptparm = record
  378. dx: Integer; { XRsiz: horizontal separation of a sample of ith component with respect to the reference grid }
  379. dy: Integer; { YRsiz: vertical separation of a sample of ith component with respect to the reference grid }
  380. w: Integer; { data width }
  381. h: Integer; { data height }
  382. x0: Integer; { x component offset compared to the whole image }
  383. y0: Integer; { y component offset compared to the whole image }
  384. prec: Integer; { precision }
  385. bpp: Integer; { image depth in bits }
  386. sgnd: Integer; { signed (1) / unsigned (0) }
  387. comp_type: OPJ_COMPONENT_TYPE; { type of this component: color channel, opacity, ... }
  388. end;
  389. opj_image_cmptparm_t = opj_image_comptparm;
  390. popj_image_cmptparm_t = ^opj_image_cmptparm_t;
  391. opj_image_cmptparm_array = array[0..255] of opj_image_cmptparm_t;
  392. popj_image_cmptparm_array = ^opj_image_cmptparm_array;
  393. TOpjImageCompParam = opj_image_cmptparm_t;
  394. { OpenJpeg Version Functions Definitions }
  395. function opj_version: PAnsiChar; cdecl; external;
  396. { Image Functions Definitions }
  397. { Create an image
  398. @param numcmpts number of components
  399. @param cmptparms components parameters
  400. @param clrspc image color space
  401. @return returns a new image structure if successful, returns NULL otherwise }
  402. function opj_image_create(numcmpts: Integer; cmptparms: popj_image_cmptparm_t;
  403. clrspc: OPJ_COLOR_SPACE): popj_image_t; cdecl; external;
  404. { Deallocate any resources associated with an image
  405. @param image image to be destroyed }
  406. procedure opj_image_destroy(image: popj_image_t); cdecl; external;
  407. { Stream Functions Definitions }
  408. { Open and allocate a memory stream for read / write.
  409. On reading, the user must provide a buffer containing encoded data. The buffer
  410. will be wrapped by the returned CIO handle.
  411. On writing, buffer parameters must be set to 0: a buffer will be allocated
  412. by the library to contain encoded data.
  413. @param cinfo Codec context info
  414. @param buffer Reading: buffer address. Writing: NULL
  415. @param length Reading: buffer length. Writing: 0
  416. @return Returns a CIO handle if successful, returns NULL otherwise }
  417. function opj_cio_open(cinfo: opj_common_ptr; buffer: PByte;
  418. length: Integer): popj_cio_t; cdecl; external;
  419. { Close and free a CIO handle
  420. @param cio CIO handle to free }
  421. procedure opj_cio_close(cio: popj_cio_t); cdecl; external;
  422. { Get position in byte stream
  423. @param cio CIO handle
  424. @return Returns the position in bytes }
  425. function cio_tell(cio: popj_cio_t): Integer; cdecl; external;
  426. { Set position in byte stream
  427. @param cio CIO handle
  428. @param pos Position, in number of bytes, from the beginning of the stream }
  429. procedure cio_seek(cio: popj_cio_t; pos: Integer); cdecl; external;
  430. { Event Manager Functions Definitions }
  431. function opj_set_event_mgr(cinfo: opj_common_ptr; event_mgr: popj_event_mgr_t;
  432. context: Pointer): popj_event_mgr_t; cdecl; external;
  433. { Codec Functions Definitions }
  434. { Creates a J2K/JPT/JP2 decompression structure
  435. @param format Decoder to select
  436. @return Returns a handle to a decompressor if successful, returns NULL otherwise }
  437. function opj_create_decompress(format: OPJ_CODEC_FORMAT): popj_dinfo_t; cdecl; external;
  438. { Destroy a decompressor handle
  439. @param dinfo decompressor handle to destroy }
  440. procedure opj_destroy_decompress(dinfo: popj_dinfo_t); cdecl; external;
  441. { Set decoding parameters to default values
  442. @param parameters Decompression parameters }
  443. procedure opj_set_default_decoder_parameters(parameters: popj_dparameters_t); cdecl; external ;
  444. { Setup the decoder decoding parameters using user parameters.
  445. Decoding parameters are returned in j2k->cp.
  446. @param dinfo decompressor handle
  447. @param parameters decompression parameters }
  448. procedure opj_setup_decoder(dinfo: popj_dinfo_t; parameters: popj_dparameters_t); cdecl; external;
  449. { Decode an image from a JPEG-2000 codestream
  450. @param dinfo decompressor handle
  451. @param cio Input buffer stream
  452. @return Returns a decoded image if successful, returns NULL otherwise }
  453. function opj_decode(dinfo: popj_dinfo_t; cio: popj_cio_t): popj_image_t; cdecl; external;
  454. { Creates a J2K/JP2 compression structure
  455. @param format Coder to select
  456. @return Returns a handle to a compressor if successful, returns NULL otherwise }
  457. function opj_create_compress(format: OPJ_CODEC_FORMAT): popj_cinfo_t; cdecl; external;
  458. { Destroy a compressor handle
  459. @param cinfo compressor handle to destroy }
  460. procedure opj_destroy_compress(cinfo: popj_cinfo_t); cdecl; external;
  461. { Set encoding parameters to default values, that means :
  462. <ul>
  463. <li>Lossless
  464. <li>1 tile
  465. <li>Size of precinct : 2^15 x 2^15 (means 1 precinct)
  466. <li>Size of code-block : 64 x 64
  467. <li>Number of resolutions: 6
  468. <li>No SOP marker in the codestream
  469. <li>No EPH marker in the codestream
  470. <li>No sub-sampling in x or y direction
  471. <li>No mode switch activated
  472. <li>Progression order: LRCP
  473. <li>No index file
  474. <li>No ROI upshifted
  475. <li>No offset of the origin of the image
  476. <li>No offset of the origin of the tiles
  477. <li>Reversible DWT 5-3
  478. </ul>
  479. @param parameters Compression parameters }
  480. procedure opj_set_default_encoder_parameters(parameters: popj_cparameters_t); cdecl; external;
  481. { Setup the encoder parameters using the current image and using user parameters.
  482. @param cinfo compressor handle
  483. @param parameters compression parameters
  484. @param image input filled image }
  485. procedure opj_setup_encoder(cinfo: popj_cinfo_t; parameters: popj_cparameters_t;
  486. image: popj_image_t); cdecl; external;
  487. { Encode an image into a JPEG-2000 codestream
  488. @param cinfo compressor handle
  489. @param cio Output buffer stream
  490. @param image Image to encode
  491. @param index Name of the index file if required, NULL otherwise
  492. @return Returns true if successful, returns false otherwise }
  493. function opj_encode(cinfo: popj_cinfo_t; cio: popj_cio_t; image: popj_image_t;
  494. index: PAnsiChar): Bool; cdecl; external;
  495. implementation
  496. {$IF Defined(MSWINDOWS)}
  497. {$IF Defined(DCC)}
  498. { Delphi Win32 }
  499. { Link object files created with C++ Builder.}
  500. {$L J2KObjects\pi.obj}
  501. {$L J2KObjects\openjpeg.obj}
  502. {$L J2KObjects\j2k_lib.obj}
  503. {$L J2KObjects\event.obj}
  504. {$L J2KObjects\cio.obj}
  505. {$L J2KObjects\image.obj}
  506. {$L J2KObjects\j2k.obj}
  507. {$L J2KObjects\jp2.obj}
  508. {$L J2KObjects\jpt.obj}
  509. {$L J2KObjects\mqc.obj}
  510. {$L J2KObjects\raw.obj}
  511. {$L J2KObjects\bio.obj}
  512. {$L J2KObjects\tgt.obj}
  513. {$L J2KObjects\tcd.obj}
  514. {$L J2KObjects\t1.obj}
  515. {$L J2KObjects\dwt.obj}
  516. {$L J2KObjects\t2.obj}
  517. {$L J2KObjects\mct.obj}
  518. const
  519. { MS C Runtime library needed for importing std C functions.}
  520. MSCRuntimeLib = 'msvcrt.dll';
  521. var
  522. { Some unresolved external constants.}
  523. __turboFloat: LongBool = False;
  524. _max_dble: Double = 1.7e308;
  525. _streams: Pointer;
  526. { Internal OpenJpeg functions external declarations.
  527. Delphi yells 'unsatisfied external declaration' if they are not referenced here.}
  528. procedure mqc_create; cdecl; external;
  529. procedure raw_create; cdecl; external;
  530. procedure bio_create; cdecl; external;
  531. procedure opj_image_create0; cdecl; external;
  532. procedure opj_event_msg; cdecl; external;
  533. procedure opj_clock; cdecl; external;
  534. procedure cio_read; cdecl; external;
  535. procedure cio_write; cdecl; external;
  536. procedure cio_skip; cdecl; external;
  537. procedure bio_read; cdecl; external;
  538. procedure bio_write; cdecl; external;
  539. procedure cio_numbytesleft; cdecl; external;
  540. procedure cio_getbp; cdecl; external;
  541. procedure j2k_destroy_compress; cdecl; external;
  542. procedure tgt_create; cdecl; external;
  543. procedure tgt_destroy; cdecl; external;
  544. procedure mqc_bypass_enc; cdecl; external;
  545. procedure mqc_encode; cdecl; external;
  546. procedure mqc_decode; cdecl; external;
  547. procedure raw_decode; cdecl; external;
  548. procedure mqc_resetstates; cdecl; external;
  549. procedure mqc_setstate; cdecl; external;
  550. procedure mqc_init_enc; cdecl; external;
  551. procedure mqc_segmark_enc; cdecl; external;
  552. procedure mqc_flush; cdecl; external;
  553. procedure mqc_bypass_init_enc; cdecl; external;
  554. procedure mqc_numbytes; cdecl; external;
  555. procedure mqc_reset_enc; cdecl; external;
  556. procedure mqc_erterm_enc; cdecl; external;
  557. procedure mqc_init_dec; cdecl; external;
  558. procedure raw_init_dec; cdecl; external;
  559. procedure mqc_destroy; cdecl; external;
  560. procedure mqc_restart_init_enc; cdecl; external;
  561. procedure raw_destroy; cdecl; external;
  562. procedure tgt_reset; cdecl; external;
  563. procedure tgt_setvalue; cdecl; external;
  564. procedure bio_init_enc; cdecl; external;
  565. procedure bio_flush; cdecl; external;
  566. procedure bio_numbytes; cdecl; external;
  567. procedure bio_destroy; cdecl; external;
  568. procedure bio_init_dec; cdecl; external;
  569. procedure pi_create_encode; cdecl; external;
  570. procedure pi_initialise_encode; cdecl; external;
  571. procedure pi_create_decode; cdecl; external;
  572. procedure pi_next; cdecl; external;
  573. procedure pi_destroy; cdecl; external;
  574. procedure tgt_encode; cdecl; external;
  575. procedure tgt_decode; cdecl; external;
  576. procedure bio_inalign; cdecl; external;
  577. procedure _llmul; cdecl;
  578. asm
  579. { from Delphi's System.pas __llmul }
  580. push edx
  581. push eax
  582. mov eax, [esp+16]
  583. mul dword ptr [esp]
  584. mov ecx, eax
  585. mov eax, [esp+4]
  586. mul dword ptr [esp+12]
  587. add ecx, eax
  588. mov eax, [esp]
  589. mul dword ptr [esp+12]
  590. add edx, ecx
  591. pop ecx
  592. pop ecx
  593. ret 8
  594. end;
  595. function pow(const Base, Exponent: Double): Double; cdecl;
  596. begin
  597. if Exponent = 0.0 then
  598. Result := 1.0
  599. else if (Base = 0.0) and (Exponent > 0.0) then
  600. Result := 0.0
  601. else
  602. Result := Exp(Exponent * Ln(Base));
  603. end;
  604. { C library imports }
  605. function malloc(size: Cardinal): Pointer; cdecl; external MSCRuntimeLib{$IFDEF BCB} name '_malloc'{$ENDIF};
  606. function calloc(nelem, elsize: Cardinal): Pointer; cdecl; external MSCRuntimeLib{$IFDEF BCB} name '_calloc'{$ENDIF};
  607. procedure free(ptr: Pointer); cdecl; external MSCRuntimeLib{$IFDEF BCB} name '_free'{$ENDIF};
  608. function realloc(ptr: Pointer; size: Cardinal): Pointer; cdecl; external MSCRuntimeLib{$IFDEF BCB} name '_realloc'{$ENDIF};
  609. function memset(s: Pointer; c, n: Cardinal): Pointer; cdecl; external MSCRuntimeLib{$IFDEF BCB} name '_memset'{$ENDIF};
  610. function memcpy(s1, s2: Pointer; n: Cardinal): Pointer; cdecl; external MSCRuntimeLib{$IFDEF BCB} name '_memcpy'{$ENDIF};
  611. function floor(const x: Double): Double; cdecl; external MSCRuntimeLib{$IFDEF BCB} name '_floor'{$ENDIF};
  612. function ceil(const num: Double): Double; cdecl; external MSCRuntimeLib{$IFDEF BCB} name '_ceil'{$ENDIF};
  613. function printf(format: PAnsiChar): Integer; cdecl; varargs; external MSCRuntimeLib{$IFDEF BCB} name '_printf'{$ENDIF};
  614. function fprintf(f: Pointer; format: PAnsiChar): Integer; cdecl; varargs; external MSCRuntimeLib{$IFDEF BCB} name '_fprintf'{$ENDIF};
  615. function vsprintf(s, format: PAnsiChar): Integer; cdecl; varargs; external MSCRuntimeLib{$IFDEF BCB} name '_vsprintf'{$ENDIF};
  616. function _ftol(x: Single): LongInt; cdecl; external MSCRuntimeLib{$IFDEF BCB} name '__ftol'{$ENDIF};
  617. function strcpy(s1, s2: PAnsiChar): PAnsiChar; cdecl; external MSCRuntimeLib{$IFDEF BCB} name '_strcpy'{$ENDIF};
  618. function wcscpy(s1, s2: PAnsiChar): PAnsiChar; cdecl; external MSCRuntimeLib{$IFDEF BCB} name '_wstrcpy'{$ENDIF};
  619. function strncpy(s1, s2: PAnsiChar; maxlen: Integer): PAnsiChar; cdecl; external MSCRuntimeLib{$IFDEF BCB} name '_strncpy'{$ENDIF};
  620. function strlen(s: PAnsiChar): Integer; cdecl; external MSCRuntimeLib{$IFDEF BCB} name '_strlen'{$ENDIF};
  621. {$ELSEIF Defined(FPC)}
  622. { Free Pascal Win32 }
  623. { Link OpenJpeg static library and C runtime library.}
  624. {$LINKLIB libopenjpegwin32.a}
  625. {$LINKLIB libcrtdll.a}
  626. {$IFEND}
  627. {$ELSEIF Defined(LINUX)}
  628. {$IF Defined(FPC)}
  629. { Free Pascal Linux }
  630. { Link C runtime library.}
  631. {$LINKLIB c}
  632. {$LINKLIB m}
  633. {$IF Defined(CPU86)}
  634. { Free Pascal Linux x86 }
  635. { Link OpenJpeg static library.}
  636. {$LINKLIB libopenjpeglinx86.a}
  637. {$ELSEIF Defined(CPUX86_64)}
  638. { Free Pascal Linux x86_64 }
  639. { Link OpenJpeg static library.}
  640. {$LINKLIB libopenjpeglinx86_64.a}
  641. {$ELSE}
  642. No support for this CPU architecture.
  643. {$IFEND}
  644. {$ELSE}
  645. No support for this compiler
  646. {$IFEND}
  647. {$ELSEIF Defined(DARWIN)}
  648. {$IF Defined(FPC)}
  649. { Free Pascal MacOSX }
  650. { Link C runtime library.}
  651. {$LINKLIB c}
  652. {$IF Defined(CPU86)}
  653. { Free Pascal MacOSX x86 }
  654. { Link OpenJpeg static library.}
  655. {$LINKLIB libopenjpegosxx86.a}
  656. {$ELSE}
  657. No support for this CPU architecture.
  658. {$IFEND}
  659. {$ELSE}
  660. No support for this compiler
  661. {$IFEND}
  662. {$ELSE}
  663. No suppor for this OS
  664. {$IFEND}
  665. end.