OpenJpeg.pas 26 KB

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