OpenJpeg.pas 28 KB

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