libimg.ob07 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. (*
  2. Copyright 2016, 2018, 2020, 2022 KolibriOS team
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. *)
  14. MODULE libimg;
  15. IMPORT sys := SYSTEM, KOSAPI;
  16. CONST
  17. FLIP_VERTICAL *= 1;
  18. FLIP_HORIZONTAL *= 2;
  19. ROTATE_90_CW *= 1;
  20. ROTATE_180 *= 2;
  21. ROTATE_270_CW *= 3;
  22. ROTATE_90_CCW *= ROTATE_270_CW;
  23. ROTATE_270_CCW *= ROTATE_90_CW;
  24. // scale type corresponding img_scale params
  25. LIBIMG_SCALE_INTEGER *= 1; // scale factor ; reserved 0
  26. LIBIMG_SCALE_TILE *= 2; // new width ; new height
  27. LIBIMG_SCALE_STRETCH *= 3; // new width ; new height
  28. LIBIMG_SCALE_FIT_RECT *= 4; // new width ; new height
  29. LIBIMG_SCALE_FIT_WIDTH *= 5; // new width ; new height
  30. LIBIMG_SCALE_FIT_HEIGHT *= 6; // new width ; new height
  31. LIBIMG_SCALE_FIT_MAX *= 7; // new width ; new height
  32. // interpolation algorithm
  33. LIBIMG_INTER_NONE *= 0; // use it with LIBIMG_SCALE_INTEGER, LIBIMG_SCALE_TILE, etc
  34. LIBIMG_INTER_BILINEAR *= 1;
  35. LIBIMG_INTER_DEFAULT *= LIBIMG_INTER_BILINEAR;
  36. // list of format id's
  37. LIBIMG_FORMAT_BMP *= 1;
  38. LIBIMG_FORMAT_ICO *= 2;
  39. LIBIMG_FORMAT_CUR *= 3;
  40. LIBIMG_FORMAT_GIF *= 4;
  41. LIBIMG_FORMAT_PNG *= 5;
  42. LIBIMG_FORMAT_JPEG *= 6;
  43. LIBIMG_FORMAT_TGA *= 7;
  44. LIBIMG_FORMAT_PCX *= 8;
  45. LIBIMG_FORMAT_XCF *= 9;
  46. LIBIMG_FORMAT_TIFF *= 10;
  47. LIBIMG_FORMAT_PNM *= 11;
  48. LIBIMG_FORMAT_WBMP *= 12;
  49. LIBIMG_FORMAT_XBM *= 13;
  50. LIBIMG_FORMAT_Z80 *= 14;
  51. // encode flags (byte 0x02 of common option)
  52. LIBIMG_ENCODE_STRICT_SPECIFIC *= 01H;
  53. LIBIMG_ENCODE_STRICT_BIT_DEPTH *= 02H;
  54. LIBIMG_ENCODE_DELETE_ALPHA *= 08H;
  55. LIBIMG_ENCODE_FLUSH_ALPHA *= 10H;
  56. // values for Image.Type
  57. // must be consecutive to allow fast switch on Image.Type in support functions
  58. bpp8i *= 1; // indexed
  59. bpp24 *= 2;
  60. bpp32 *= 3;
  61. bpp15 *= 4;
  62. bpp16 *= 5;
  63. bpp1 *= 6;
  64. bpp8g *= 7; // grayscale
  65. bpp2i *= 8;
  66. bpp4i *= 9;
  67. bpp8a *= 10; // grayscale with alpha channel; application layer only!!! kernel doesn't handle this image type, libimg can only create and destroy such images
  68. // bits in Image.Flags
  69. IsAnimated *= 1;
  70. TYPE
  71. Image* = RECORD
  72. Checksum *: INTEGER;
  73. Width *: INTEGER;
  74. Height *: INTEGER;
  75. Next *: INTEGER;
  76. Previous *: INTEGER;
  77. Type *: INTEGER; // one of bppN
  78. Data *: INTEGER;
  79. Palette *: INTEGER; // used iff Type eq bpp1, bpp2, bpp4 or bpp8i
  80. Extended *: INTEGER;
  81. Flags *: INTEGER; // bitfield
  82. Delay *: INTEGER // used iff IsAnimated is set in Flags
  83. END;
  84. ImageDecodeOptions* = RECORD
  85. UsedSize *: INTEGER; // if >=8, the field BackgroundColor is valid, and so on
  86. BackgroundColor *: INTEGER // used for transparent images as background
  87. END;
  88. FormatsTableEntry* = RECORD
  89. Format_id *: INTEGER;
  90. Is *: INTEGER;
  91. Decode *: INTEGER;
  92. Encode *: INTEGER;
  93. Capabilities *: INTEGER
  94. END;
  95. VAR
  96. img_is_img *: PROCEDURE (data, length: INTEGER): INTEGER;
  97. img_to_rgb2 *: PROCEDURE (img: INTEGER; out: INTEGER);
  98. (*
  99. ;;------------------------------------------------------------------------------------------------;;
  100. ;? decodes image data into RGB triplets and stores them where out points to ;;
  101. ;;------------------------------------------------------------------------------------------------;;
  102. ;> img = pointer to source image ;;
  103. ;> out = where to store RGB triplets ;;
  104. ;;================================================================================================;;
  105. *)
  106. img_to_rgb *: PROCEDURE (img: INTEGER): INTEGER;
  107. (*
  108. ;;------------------------------------------------------------------------------------------------;;
  109. ;? decodes image data into RGB triplets and returns pointer to memory area containing them ;;
  110. ;;------------------------------------------------------------------------------------------------;;
  111. ;> img = pointer to source image ;;
  112. ;;------------------------------------------------------------------------------------------------;;
  113. ;< 0 / pointer to rgb_data (array of [rgb] triplets) ;;
  114. ;;================================================================================================;;
  115. *)
  116. img_decode *: PROCEDURE (data, length, options: INTEGER): INTEGER;
  117. (*
  118. ;;------------------------------------------------------------------------------------------------;;
  119. ;? decodes loaded into memory graphic file ;;
  120. ;;------------------------------------------------------------------------------------------------;;
  121. ;> data = pointer to file in memory ;;
  122. ;> length = size in bytes of memory area pointed to by data ;;
  123. ;> options = 0 / pointer to the structure of additional options ;;
  124. ;;------------------------------------------------------------------------------------------------;;
  125. ;< 0 / pointer to image ;;
  126. ;;================================================================================================;;
  127. *)
  128. img_encode *: PROCEDURE (img: INTEGER; common, specific: INTEGER): INTEGER;
  129. (*
  130. ;;------------------------------------------------------------------------------------------------;;
  131. ;? encode image to some format ;;
  132. ;;------------------------------------------------------------------------------------------------;;
  133. ;> img = pointer to input image ;;
  134. ;> common = some most important options ;;
  135. ; 0x00 : byte : format id ;;
  136. ; 0x01 : byte : fast encoding (0) / best compression ratio (255) ;;
  137. ; 0 : store uncompressed data (if supported both by the format and libimg) ;;
  138. ; 1 - 255 : use compression, if supported ;;
  139. ; this option may be ignored if any format specific options are defined ;;
  140. ; i.e. the 0 here will be ignored if some compression algorithm is specified ;;
  141. ; 0x02 : byte : flags (bitfield) ;;
  142. ; 0x01 : return an error if format specific conditions cannot be met ;;
  143. ; 0x02 : preserve current bit depth. means 8bpp/16bpp/24bpp and so on ;;
  144. ; 0x04 : delete alpha channel, if any ;;
  145. ; 0x08 : flush alpha channel with 0xff, if any; add it if none ;;
  146. ; 0x03 : byte : reserved, must be 0 ;;
  147. ;> specific = 0 / pointer to the structure of format specific options ;;
  148. ; see <format_name>.inc for description ;;
  149. ;;------------------------------------------------------------------------------------------------;;
  150. ;< 0 / pointer to encoded data ;;
  151. ;;================================================================================================;;
  152. *)
  153. img_create *: PROCEDURE (width, height, _type: INTEGER): INTEGER;
  154. (*
  155. ;;------------------------------------------------------------------------------------------------;;
  156. ;? creates an Image structure and initializes some its fields ;;
  157. ;;------------------------------------------------------------------------------------------------;;
  158. ;> width = width of an image in pixels ;;
  159. ;> height = height of an image in pixels ;;
  160. ;> type = one of the bppN constants ;;
  161. ;;------------------------------------------------------------------------------------------------;;
  162. ;< 0 / pointer to image ;;
  163. ;;================================================================================================;;
  164. *)
  165. img_destroy *: PROCEDURE (img: INTEGER): BOOLEAN;
  166. (*
  167. ;;------------------------------------------------------------------------------------------------;;
  168. ;? frees memory occupied by an image and all the memory regions its fields point to ;;
  169. ;? follows Previous/Next pointers and deletes all the images in sequence ;;
  170. ;;------------------------------------------------------------------------------------------------;;
  171. ;> img = pointer to image ;;
  172. ;;------------------------------------------------------------------------------------------------;;
  173. ;< FALSE (fail) / TRUE (success) ;;
  174. ;;================================================================================================;;
  175. *)
  176. img_destroy_layer *: PROCEDURE (img: INTEGER): BOOLEAN;
  177. (*
  178. ;;------------------------------------------------------------------------------------------------;;
  179. ;? frees memory occupied by an image and all the memory regions its fields point to ;;
  180. ;? for image sequences deletes only one frame and fixes Previous/Next pointers ;;
  181. ;;------------------------------------------------------------------------------------------------;;
  182. ;> img = pointer to image ;;
  183. ;;------------------------------------------------------------------------------------------------;;
  184. ;< FALSE (fail) / TRUE (success) ;;
  185. ;;================================================================================================;;
  186. *)
  187. img_count *: PROCEDURE (img: INTEGER): INTEGER;
  188. (*
  189. ;;------------------------------------------------------------------------------------------------;;
  190. ;? Get number of images in the list (e.g. in animated GIF file) ;;
  191. ;;------------------------------------------------------------------------------------------------;;
  192. ;> img = pointer to image ;;
  193. ;;------------------------------------------------------------------------------------------------;;
  194. ;< -1 (fail) / >0 (ok) ;;
  195. ;;================================================================================================;;
  196. *)
  197. img_flip *: PROCEDURE (img: INTEGER; flip_kind: INTEGER): BOOLEAN;
  198. (*
  199. ;;------------------------------------------------------------------------------------------------;;
  200. ;? Flip all layers of image ;;
  201. ;;------------------------------------------------------------------------------------------------;;
  202. ;> img = pointer to image ;;
  203. ;> flip_kind = one of FLIP_* constants ;;
  204. ;;------------------------------------------------------------------------------------------------;;
  205. ;< FALSE / TRUE ;;
  206. ;;================================================================================================;;
  207. *)
  208. img_flip_layer *: PROCEDURE (img: INTEGER; flip_kind: INTEGER): BOOLEAN;
  209. (*
  210. ;;------------------------------------------------------------------------------------------------;;
  211. ;? Flip image layer ;;
  212. ;;------------------------------------------------------------------------------------------------;;
  213. ;> img = pointer to image ;;
  214. ;> flip_kind = one of FLIP_* constants ;;
  215. ;;------------------------------------------------------------------------------------------------;;
  216. ;< FALSE / TRUE ;;
  217. ;;================================================================================================;;
  218. *)
  219. img_rotate *: PROCEDURE (img: INTEGER; rotate_kind: INTEGER): BOOLEAN;
  220. (*
  221. ;;------------------------------------------------------------------------------------------------;;
  222. ;? Rotate all layers of image ;;
  223. ;;------------------------------------------------------------------------------------------------;;
  224. ;> img = pointer to image ;;
  225. ;> rotate_kind = one of ROTATE_* constants ;;
  226. ;;------------------------------------------------------------------------------------------------;;
  227. ;< FALSE / TRUE ;;
  228. ;;================================================================================================;;
  229. *)
  230. img_rotate_layer *: PROCEDURE (img: INTEGER; rotate_kind: INTEGER): BOOLEAN;
  231. (*
  232. ;;------------------------------------------------------------------------------------------------;;
  233. ;? Rotate image layer ;;
  234. ;;------------------------------------------------------------------------------------------------;;
  235. ;> img = pointer to image ;;
  236. ;> rotate_kind = one of ROTATE_* constants ;;
  237. ;;------------------------------------------------------------------------------------------------;;
  238. ;< FALSE / TRUE ;;
  239. ;;================================================================================================;;
  240. *)
  241. img_draw *: PROCEDURE (img: INTEGER; x, y, width, height, xpos, ypos: INTEGER);
  242. (*
  243. ;;------------------------------------------------------------------------------------------------;;
  244. ;? Draw image in the window ;;
  245. ;;------------------------------------------------------------------------------------------------;;
  246. ;> img = pointer to image ;;
  247. ;> x = x-coordinate in the window ;;
  248. ;> y = y-coordinate in the window ;;
  249. ;> width = maximum width to draw ;;
  250. ;> height = maximum height to draw ;;
  251. ;> xpos = offset in image by x-axis ;;
  252. ;> ypos = offset in image by y-axis ;;
  253. ;;================================================================================================;;
  254. *)
  255. img_scale *: PROCEDURE (src: INTEGER; crop_x, crop_y, crop_width, crop_height: INTEGER; dst: INTEGER; scale, inter, param1, param2: INTEGER ): INTEGER;
  256. (*
  257. ;;------------------------------------------------------------------------------------------------;;
  258. ;? scale _image ;;
  259. ;;------------------------------------------------------------------------------------------------;;
  260. ;> src = pointer to source image ;;
  261. ;> crop_x = left coord of cropping rect ;;
  262. ;> crop_y = top coord of cropping rect ;;
  263. ;> crop_width = width of cropping rect ;;
  264. ;> crop_height = height of cropping rect ;;
  265. ;> dst = pointer to resulting image / 0 ;;
  266. ;> scale = how to change width and height. see libimg.inc ;;
  267. ;> inter = interpolation algorithm ;;
  268. ;> param1 = see libimg.inc ;;
  269. ;> param2 = see libimg.inc ;;
  270. ;;------------------------------------------------------------------------------------------------;;
  271. ;< 0 / pointer to scaled image ;;
  272. ;;================================================================================================;;
  273. *)
  274. img_convert *: PROCEDURE (src, dst: INTEGER; dst_type, flags, param: INTEGER);
  275. (*
  276. ;;------------------------------------------------------------------------------------------------;;
  277. ;? scale _image ;;
  278. ;;------------------------------------------------------------------------------------------------;;
  279. ;> src = pointer to source image ;;
  280. ;> flags = see libimg.inc ;;
  281. ;> dst_type = the Image.Type of converted image ;;
  282. ;> dst = pointer to destination image, if any ;;
  283. ;;------------------------------------------------------------------------------------------------;;
  284. ;< 0 / pointer to converted image ;;
  285. ;;================================================================================================;;
  286. *)
  287. img_formats_table *: ARRAY 20 OF FormatsTableEntry;
  288. PROCEDURE GetImageStruct* (img: INTEGER; VAR ImageStruct: Image): BOOLEAN;
  289. BEGIN
  290. IF img # 0 THEN
  291. sys.MOVE(img, sys.ADR(ImageStruct), sys.SIZE(Image))
  292. END
  293. RETURN img # 0
  294. END GetImageStruct;
  295. PROCEDURE GetFormatsTable(ptr: INTEGER);
  296. VAR i: INTEGER; eot: BOOLEAN;
  297. BEGIN
  298. i := 0;
  299. REPEAT
  300. sys.MOVE(ptr, sys.ADR(img_formats_table[i]), sys.SIZE(FormatsTableEntry));
  301. ptr := ptr + sys.SIZE(FormatsTableEntry);
  302. eot := img_formats_table[i].Format_id = 0;
  303. INC(i)
  304. UNTIL eot OR (i = LEN(img_formats_table))
  305. END GetFormatsTable;
  306. PROCEDURE main;
  307. VAR Lib, formats_table_ptr: INTEGER;
  308. PROCEDURE GetProc(Lib, v: INTEGER; name: ARRAY OF CHAR);
  309. VAR a: INTEGER;
  310. BEGIN
  311. a := KOSAPI.GetProcAdr(name, Lib);
  312. ASSERT(a # 0);
  313. sys.PUT(v, a)
  314. END GetProc;
  315. BEGIN
  316. Lib := KOSAPI.LoadLib("/sys/lib/libimg.obj");
  317. ASSERT(Lib # 0);
  318. GetProc(Lib, sys.ADR(img_is_img) , "img_is_img");
  319. GetProc(Lib, sys.ADR(img_to_rgb) , "img_to_rgb");
  320. GetProc(Lib, sys.ADR(img_to_rgb2) , "img_to_rgb2");
  321. GetProc(Lib, sys.ADR(img_decode) , "img_decode");
  322. GetProc(Lib, sys.ADR(img_encode) , "img_encode");
  323. GetProc(Lib, sys.ADR(img_create) , "img_create");
  324. GetProc(Lib, sys.ADR(img_destroy) , "img_destroy");
  325. GetProc(Lib, sys.ADR(img_destroy_layer) , "img_destroy_layer");
  326. GetProc(Lib, sys.ADR(img_count) , "img_count");
  327. GetProc(Lib, sys.ADR(img_flip) , "img_flip");
  328. GetProc(Lib, sys.ADR(img_flip_layer) , "img_flip_layer");
  329. GetProc(Lib, sys.ADR(img_rotate) , "img_rotate");
  330. GetProc(Lib, sys.ADR(img_rotate_layer) , "img_rotate_layer");
  331. GetProc(Lib, sys.ADR(img_draw) , "img_draw");
  332. GetProc(Lib, sys.ADR(img_scale) , "img_scale");
  333. GetProc(Lib, sys.ADR(img_convert) , "img_convert");
  334. GetProc(Lib, sys.ADR(formats_table_ptr) , "img_formats_table");
  335. GetFormatsTable(formats_table_ptr)
  336. END main;
  337. BEGIN
  338. main
  339. END libimg.