Subversion Repositories Kolibri OS

Rev

Rev 7597 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

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