Subversion Repositories Kolibri OS

Rev

Rev 9016 | Rev 9454 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. //Asper
  2. #ifndef INCLUDE_LIBIMG_H
  3. #define INCLUDE_LIBIMG_H
  4.  
  5. #ifndef INCLUDE_KOLIBRI_H
  6. #include "../lib/kolibri.h"
  7. #endif
  8.  
  9. #ifndef INCLUDE_MEM_H
  10. #include "../lib/mem.h"
  11. #endif
  12.  
  13. #ifndef INCLUDE_DLL_H
  14. #include "../lib/dll.h"
  15. #endif
  16.  
  17. //library
  18. dword libimg = #alibimg;
  19. char alibimg[] = "/sys/lib/libimg.obj";
  20.  
  21. dword libimg_init   = #alibimg_init;
  22. dword img_decode    = #aimg_decode;
  23. dword img_destroy   = #aimg_destroy;
  24. dword img_draw      = #aimg_draw;
  25. dword img_create    = #aimg_create;
  26. dword img_encode    = #aimg_encode;
  27. dword img_convert   = #aimg_convert;
  28. dword img_from_file = #aimg_from_file;
  29. dword img_blend     = #aimg_blend;
  30. //dword img_is_img    = #aimg_is_img;
  31. //dword img_to_rgb2   = #aimg_to_rgb2;
  32. //dword img_scale     = #aimg_scale;
  33. dword img_flip      = #aimg_flip;
  34. dword img_rotate    = #aimg_rotate;
  35. dword img_to_rgb    = #aimg_to_rgb;
  36.  
  37. $DD 2 dup 0
  38.  
  39. //import
  40. char alibimg_init[]   = "lib_init";
  41. char aimg_decode[]    = "img_decode";
  42. char aimg_destroy[]   = "img_destroy";
  43. char aimg_draw[]      = "img_draw";
  44. char aimg_create[]    = "img_create";
  45. char aimg_encode[]    = "img_encode";
  46. char aimg_convert[]   = "img_convert";
  47. char aimg_from_file[] = "img_from_file";
  48. char aimg_blend[]     = "img_blend";
  49. //char aimg_is_img[]    = "img_is_img";
  50. //char aimg_to_rgb2[]   = "img_to_rgb2";
  51. //char aimg_scale[]     = "img_scale";
  52. char aimg_flip[]      = "img_flip";
  53. char aimg_rotate[]    = "img_rotate";
  54. char aimg_to_rgb[]    = "img_to_rgb";
  55.  
  56. #define LIBIMG_FORMAT_BMP       1
  57. #define LIBIMG_FORMAT_ICO       2
  58. #define LIBIMG_FORMAT_CUR       3
  59. #define LIBIMG_FORMAT_GIF       4
  60. #define LIBIMG_FORMAT_PNG       5
  61. #define LIBIMG_FORMAT_JPEG      6
  62. #define LIBIMG_FORMAT_TGA       7
  63. #define LIBIMG_FORMAT_PCX       8
  64. #define LIBIMG_FORMAT_XCF       9
  65. #define LIBIMG_FORMAT_TIFF     10
  66. #define LIBIMG_FORMAT_PNM      11
  67. #define LIBIMG_FORMAT_WBMP     12
  68. #define LIBIMG_FORMAT_XBM      13
  69. #define LIBIMG_FORMAT_Z80      14
  70.  
  71. // values for Image.Type
  72. // must be consecutive to allow fast switch on Image.Type in support functions
  73. #define IMAGE_BPP8i  1  // indexed
  74. #define IMAGE_BPP24  2
  75. #define IMAGE_BPP32  3
  76. #define IMAGE_BPP15  4
  77. #define IMAGE_BPP16  5
  78. #define IMAGE_BPP1   6
  79. #define IMAGE_BPP8g  7  // grayscale
  80. #define IMAGE_BPP2i  8
  81. #define IMAGE_BPP4i  9
  82. #define IMAGE_BPP8a 10  // grayscale with alpha channel; application layer only!!!
  83.                         // kernel doesn't handle this image type,
  84.                         // libimg can only create and destroy such images
  85.  
  86. #define FLIP_VERTICAL   0x01
  87. #define FLIP_HORIZONTAL 0x02
  88.  
  89. #define ROTATE_90_CW    0x01
  90. #define ROTATE_180      0x02
  91. #define ROTATE_270_CW   0x03
  92. #define ROTATE_90_CCW   ROTATE_270_CW
  93. #define ROTATE_270_CCW  ROTATE_90_CW
  94.  
  95. struct libimg_image
  96. {
  97.     dword checksum; // ((Width ROL 16) OR Height) XOR Data[0]        ; ignored so far
  98.     dword w;
  99.     dword h;
  100.     dword next;
  101.     dword previous;
  102.     dword type;     // one of Image.bppN
  103.     dword imgsrc;
  104.     dword palette;  // used iff Type eq Image.bpp1, Image.bpp2, Image.bpp4 or Image.bpp8i
  105.     dword extended;
  106.     dword flags;    // bitfield
  107.     dword delay;    // used iff Image.IsAnimated is set in Flags
  108.     dword image;
  109.     void load();
  110.     void convert_into();
  111.     void replace_color();
  112.     void replace_2colors();
  113.     void set_vars();
  114.     void draw();
  115. };
  116.  
  117. :void libimg_image::set_vars()
  118. {
  119.     $push edi
  120.     EDI = image;
  121.     //checksum = ESDWORD[EDI];
  122.     w = ESDWORD[EDI+4];
  123.     h = ESDWORD[EDI+8];
  124.     //next = ESDWORD[EDI+12];
  125.     //previous = ESDWORD[EDI+16];
  126.     type = ESDWORD[EDI+20];
  127.     imgsrc = ESDWORD[EDI+24];
  128.     //palette = ESDWORD[EDI+28];
  129.     //extended = ESDWORD[EDI+32];
  130.     //flags = ESDWORD[EDI+36];
  131.     //delay = ESDWORD[EDI+40];
  132.     $pop edi
  133. }
  134.  
  135. :void libimg_image::load(dword file_path)
  136. {
  137.     if (image) img_destroy stdcall(image);
  138.     img_from_file stdcall(file_path);
  139.     if (!EAX) {
  140.         notify("'Error: Image not loaded'E");
  141.     } else {
  142.         image = EAX;
  143.         set_vars();
  144.     }
  145. }
  146.  
  147. :void libimg_image::replace_color(dword old_color, new_color)
  148. {
  149.     EDX =  w * h * 4 + imgsrc;
  150.     ESI = old_color;
  151.     ECX = new_color;
  152.     FOR (EDI = imgsrc; EDI < EDX; EDI += 4) IF (DSDWORD[EDI]==ESI) DSDWORD[EDI] = ECX;
  153. }
  154.  
  155. :void libimg_image::replace_2colors(dword old_color1, new_color1, old_color2, new_color2)
  156. {
  157.     EDX =  w * h * 4 + imgsrc;
  158.     ESI = old_color1;
  159.     ECX = new_color1;
  160.     EBX = old_color2;
  161.     EAX = new_color2;
  162.     FOR (EDI = imgsrc; EDI < EDX; EDI += 4) {
  163.         IF (DSDWORD[EDI]==ESI) DSDWORD[EDI] = ECX;
  164.         ELSE IF (DSDWORD[EDI]==EBX) DSDWORD[EDI] = EAX;
  165.     }
  166. }
  167.  
  168. :void libimg_image::draw(dword _x, _y, _w, _h, _xoff, _yoff)
  169. {
  170.     if (image) img_draw stdcall(image, _x, _y, _w, _h, _xoff, _yoff);
  171. }
  172.  
  173. :void libimg_image::convert_into(dword _to)
  174. {
  175.     img_convert stdcall(image, 0, _to, 0, 0);
  176.     $push eax
  177.     img_destroy stdcall(image);
  178.     $pop eax
  179.     if (!EAX) {
  180.         notify("'LibImg convertation error!'E");
  181.     } else {
  182.         image = EAX;
  183.         set_vars();
  184.     }
  185. }
  186.  
  187. :dword create_image(dword type, dword width, dword height) {
  188.     img_create stdcall(width, height, type);
  189.     return EAX;
  190. }
  191.  
  192. // size - output parameter, error code / the size of encoded data
  193. :dword encode_image(dword image_ptr, dword options, dword specific_options, dword* size) {
  194.     img_encode stdcall(image_ptr, options, specific_options);
  195.     ESDWORD[size] = ECX;
  196.  
  197.     return EAX;
  198. }
  199.  
  200. :dword save_image(dword _image_pointer, _w, _h, _path)
  201. {
  202.     dword encoded_data=0;
  203.     dword encoded_size=0;
  204.     dword image_ptr = 0;
  205.  
  206.     image_ptr = create_image(IMAGE_BPP24, _w, _h);
  207.  
  208.     if (!image_ptr) {
  209.         return "Error creating image!";
  210.     }
  211.     else {
  212.         EDI = image_ptr;
  213.         memmov(EDI.libimg_image.imgsrc, _image_pointer, _w * _h * 3);
  214.  
  215.         encoded_data = encode_image(image_ptr, LIBIMG_FORMAT_PNG, 0, #encoded_size);
  216.  
  217.         img_destroy stdcall(image_ptr);
  218.  
  219.         if(!encoded_data) {
  220.             return "Error encoding image!";
  221.         }
  222.         else {
  223.             if (!CreateFile(encoded_size, encoded_data, _path)) {
  224.                 return 0;
  225.             }
  226.             else {
  227.                 return "'Error saving image file!\nNot enough space? Path wrong?\nFile system is not writable?..' -E";
  228.             }
  229.         }
  230.     }
  231. }
  232.  
  233.  
  234.  
  235. /////////////////////////////
  236. /*
  237. //  DRAW ICON PATTERN / TEMP
  238. */
  239. /////////////////////////////
  240.  
  241. :void DrawIcon32(dword x,y, _bg, icon_n) {
  242.     static dword bg;
  243.     static dword pure_img32;
  244.     if (!pure_img32) || (bg!=_bg) {
  245.         bg = _bg;
  246.         if (pure_img32) img_destroy stdcall(pure_img32);
  247.         img_from_file stdcall("/sys/icons32.png");
  248.         pure_img32 = EAX;
  249.         //now fill transparent with another color
  250.         EDX = ESDWORD[EAX+4] * ESDWORD[EAX+8] * 4 + ESDWORD[EAX+24];
  251.         for (ESI = ESDWORD[EAX+24]; ESI < EDX; ESI += 4) {
  252.             if (DSDWORD[ESI]==0x00000000) DSDWORD[ESI] = bg;
  253.         }
  254.     }
  255.     img_draw stdcall(pure_img32, x, y, 32, 32, 0, icon_n*32);
  256. }
  257.  
  258. :int DrawIcon16(dword x,y, _bg, icon_n) {
  259.     static dword bg;
  260.     static dword pure_img16;
  261.     dword bgshadow;
  262.     int size;
  263.     if (!pure_img16) || (bg!=_bg) {
  264.         bg = _bg;
  265.         bgshadow = MixColors(bg, 0, 220);
  266.         if (pure_img16) img_destroy stdcall(pure_img16);
  267.         img_from_file stdcall("/sys/icons16.png");
  268.         pure_img16 = EAX;
  269.         //now fill transparent with another color
  270.         EDX = ESDWORD[EAX+4] * ESDWORD[EAX+8] * 4 + ESDWORD[EAX+24];
  271.         for (ESI = ESDWORD[EAX+24]; ESI < EDX; ESI += 4) {
  272.             if (DSDWORD[ESI]==0xffFFFfff) DSDWORD[ESI] = bg;
  273.             if (DSDWORD[ESI]==0xffCACBD6) DSDWORD[ESI] = bgshadow;
  274.         }
  275.     }
  276.     size = ESDWORD[pure_img16+4]; //get image width
  277.     img_draw stdcall(pure_img16, x, y, size, size, 0, icon_n*size);
  278.     return size;
  279. }
  280.  
  281. #endif