Subversion Repositories Kolibri OS

Rev

Rev 9594 | Rev 9643 | 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. dword resize        = #aresize;
  37.  
  38. $DD 2 dup 0
  39.  
  40. //import
  41. char alibimg_init[]   = "lib_init";
  42. char aimg_decode[]    = "img_decode";
  43. char aimg_destroy[]   = "img_destroy";
  44. char aimg_draw[]      = "img_draw";
  45. char aimg_create[]    = "img_create";
  46. char aimg_encode[]    = "img_encode";
  47. char aimg_convert[]   = "img_convert";
  48. char aimg_from_file[] = "img_from_file";
  49. char aimg_blend[]     = "img_blend";
  50. //char aimg_is_img[]    = "img_is_img";
  51. //char aimg_to_rgb2[]   = "img_to_rgb2";
  52. //char aimg_scale[]     = "img_scale";
  53. char aimg_flip[]      = "img_flip";
  54. char aimg_rotate[]    = "img_rotate";
  55. char aimg_to_rgb[]    = "img_to_rgb";
  56. char aresize[]        = "img_resize_data";
  57.  
  58. #define LIBIMG_FORMAT_BMP       1
  59. #define LIBIMG_FORMAT_ICO       2
  60. #define LIBIMG_FORMAT_CUR       3
  61. #define LIBIMG_FORMAT_GIF       4
  62. #define LIBIMG_FORMAT_PNG       5
  63. #define LIBIMG_FORMAT_JPEG      6
  64. #define LIBIMG_FORMAT_TGA       7
  65. #define LIBIMG_FORMAT_PCX       8
  66. #define LIBIMG_FORMAT_XCF       9
  67. #define LIBIMG_FORMAT_TIFF     10
  68. #define LIBIMG_FORMAT_PNM      11
  69. #define LIBIMG_FORMAT_WBMP     12
  70. #define LIBIMG_FORMAT_XBM      13
  71. #define LIBIMG_FORMAT_Z80      14
  72.  
  73. // values for Image.Type
  74. // must be consecutive to allow fast switch on Image.Type in support functions
  75. #define IMAGE_BPP8i  1  // indexed
  76. #define IMAGE_BPP24  2
  77. #define IMAGE_BPP32  3
  78. #define IMAGE_BPP15  4
  79. #define IMAGE_BPP16  5
  80. #define IMAGE_BPP1   6
  81. #define IMAGE_BPP8g  7  // grayscale
  82. #define IMAGE_BPP2i  8
  83. #define IMAGE_BPP4i  9
  84. #define IMAGE_BPP8a 10  // grayscale with alpha channel; application layer only!!!
  85.                         // kernel doesn't handle this image type,
  86.                         // libimg can only create and destroy such images
  87.  
  88. #define FLIP_VERTICAL   0x01
  89. #define FLIP_HORIZONTAL 0x02
  90.  
  91. #define ROTATE_90_CW    0x01
  92. #define ROTATE_180      0x02
  93. #define ROTATE_270_CW   0x03
  94. #define ROTATE_90_CCW   ROTATE_270_CW
  95. #define ROTATE_270_CCW  ROTATE_90_CW
  96.  
  97. struct libimg_image
  98. {
  99.     dword checksum; // ((Width ROL 16) OR Height) XOR Data[0]        ; ignored so far
  100.     dword w;
  101.     dword h;
  102.     dword next;
  103.     dword previous;
  104.     dword type;     // one of Image.bppN
  105.     dword imgsrc;
  106.     dword palette;  // used iff Type eq Image.bpp1, Image.bpp2, Image.bpp4 or Image.bpp8i
  107.     dword extended;
  108.     dword flags;    // bitfield
  109.     dword delay;    // used iff Image.IsAnimated is set in Flags
  110.     dword image;
  111.     void load();
  112.     void convert_into();
  113.     void replace_color();
  114.     void replace_2colors();
  115.     void set_vars();
  116.     void draw();
  117. };
  118.  
  119. :void libimg_image::set_vars()
  120. {
  121.     $push edi
  122.     EDI = image;
  123.     //checksum = ESDWORD[EDI];
  124.     w = ESDWORD[EDI+4];
  125.     h = ESDWORD[EDI+8];
  126.     //next = ESDWORD[EDI+12];
  127.     //previous = ESDWORD[EDI+16];
  128.     type = ESDWORD[EDI+20];
  129.     imgsrc = ESDWORD[EDI+24];
  130.     //palette = ESDWORD[EDI+28];
  131.     //extended = ESDWORD[EDI+32];
  132.     //flags = ESDWORD[EDI+36];
  133.     //delay = ESDWORD[EDI+40];
  134.     $pop edi
  135. }
  136.  
  137. :void libimg_image::load(dword file_path)
  138. {
  139.     if (image) img_destroy stdcall(image);
  140.     img_from_file stdcall(file_path);
  141.     if (!EAX) {
  142.         notify("'Error: Image not loaded'E");
  143.     } else {
  144.         image = EAX;
  145.         set_vars();
  146.     }
  147. }
  148.  
  149. :void libimg_image::replace_color(dword old_color, new_color)
  150. {
  151.     EDX =  w * h * 4 + imgsrc;
  152.     ESI = old_color;
  153.     ECX = new_color;
  154.     FOR (EDI = imgsrc; EDI < EDX; EDI += 4) IF (DSDWORD[EDI]==ESI) DSDWORD[EDI] = ECX;
  155. }
  156.  
  157. :void libimg_image::replace_2colors(dword old_color1, new_color1, old_color2, new_color2)
  158. {
  159.     EDX =  w * h * 4 + imgsrc;
  160.     ESI = old_color1;
  161.     ECX = new_color1;
  162.     EBX = old_color2;
  163.     EAX = new_color2;
  164.     FOR (EDI = imgsrc; EDI < EDX; EDI += 4) {
  165.         IF (DSDWORD[EDI]==ESI) DSDWORD[EDI] = ECX;
  166.         ELSE IF (DSDWORD[EDI]==EBX) DSDWORD[EDI] = EAX;
  167.     }
  168. }
  169.  
  170. :void libimg_image::draw(dword _x, _y, _w, _h, _xoff, _yoff)
  171. {
  172.     if (image) img_draw stdcall(image, _x, _y, _w, _h, _xoff, _yoff);
  173. }
  174.  
  175. :void libimg_image::convert_into(dword _to)
  176. {
  177.     img_convert stdcall(image, 0, _to, 0, 0);
  178.     if (!EAX) {
  179.         notify("'LibImg convertation error!'E");
  180.     } else {
  181.         $push eax
  182.         img_destroy stdcall(image);
  183.         $pop eax
  184.         image = EAX;
  185.         set_vars();
  186.     }
  187. }
  188.  
  189. // size - output parameter, error code / the size of encoded data
  190. :dword encode_image(dword image_ptr, dword options, dword specific_options, dword* size) {
  191.     img_encode stdcall(image_ptr, options, specific_options);
  192.     ESDWORD[size] = ECX;
  193.  
  194.     return EAX;
  195. }
  196.  
  197. :dword save_image(dword _image_pointer, _w, _h, _path)
  198. {
  199.     dword encoded_data=0;
  200.     dword encoded_size=0;
  201.     dword image_ptr = 0;
  202.  
  203.     img_create stdcall(_w, _h, IMAGE_BPP24);
  204.     image_ptr = EAX;
  205.  
  206.     if (!image_ptr) {
  207.         return "Error creating image!";
  208.     }
  209.     else {
  210.         EDI = image_ptr;
  211.         memmov(EDI.libimg_image.imgsrc, _image_pointer, _w * _h * 3);
  212.  
  213.         encoded_data = encode_image(image_ptr, LIBIMG_FORMAT_PNG, 0, #encoded_size);
  214.  
  215.         img_destroy stdcall(image_ptr);
  216.  
  217.         if(!encoded_data) {
  218.             return "Error encoding image!";
  219.         }
  220.         else {
  221.             if (!CreateFile(encoded_size, encoded_data, _path)) {
  222.                 return 0;
  223.             }
  224.             else {
  225.                 return "'Error saving image file!\nNot enough space? Path wrong?\nFile system is not writable?..' -E";
  226.             }
  227.         }
  228.     }
  229. }
  230.  
  231. #endif