Subversion Repositories Kolibri OS

Rev

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_flip      = #aimg_flip;
  31. dword img_rotate    = #aimg_rotate;
  32. dword img_scale     = #aimg_scale;
  33. //dword img_is_img    = #aimg_is_img;
  34. //dword img_to_rgb    = #aimg_to_rgb;
  35. //dword img_to_rgb2   = #aimg_to_rgb2;
  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_flip[]      = "img_flip";
  50. char aimg_rotate[]    = "img_rotate";
  51. char aimg_scale[]     = "img_scale";
  52. //char aimg_is_img[]    = "img_is_img";
  53. //char aimg_to_rgb[]    = "img_to_rgb";
  54. //char aimg_to_rgb2[]   = "img_to_rgb2";
  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. //flip and rotate
  87. #define FLIP_VERTICAL   0x01
  88. #define FLIP_HORIZONTAL 0x02
  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. //scale type                    //corresponding img.scale params
  96. #define LIBIMG_SCALE_NONE       0     //do not scale
  97. #define LIBIMG_SCALE_INTEGER    1     //scale factor, reserved 0
  98. #define LIBIMG_SCALE_TILE       2     //new width, new height
  99. #define LIBIMG_SCALE_STRETCH    3     //new width, new height
  100. #define LIBIMG_SCALE_FIT_BOTH   LIBIMG_SCALE_STRETCH
  101. #define LIBIMG_SCALE_FIT_MIN    4     //new width, new height
  102. #define LIBIMG_SCALE_FIT_RECT   LIBIMG_SCALE_FIT_MIN
  103. #define LIBIMG_SCALE_FIT_WIDTH  5     //new width, new height
  104. #define LIBIMG_SCALE_FIT_HEIGHT 6     //new width, new height
  105. #define LIBIMG_SCALE_FIT_MAX    7     //new width, new height
  106.  
  107. //interpolation algorithm
  108. #define LIBIMG_INTER_NONE       0     //use it with LIBIMG_SCALE_INTEGER, LIBIMG_SCALE_TILE, etc
  109. #define LIBIMG_INTER_BILINEAR   1
  110. #define LIBIMG_INTER_DEFAULT    LIBIMG_INTER_BILINEAR
  111.  
  112. /*
  113. // error codes
  114. LIBIMG_ERROR_OUT_OF_MEMORY      = 1
  115. LIBIMG_ERROR_FORMAT             = 2
  116. LIBIMG_ERROR_CONDITIONS         = 3
  117. LIBIMG_ERROR_BIT_DEPTH          = 4
  118. LIBIMG_ERROR_ENCODER            = 5
  119. LIBIMG_ERROR_SRC_TYPE           = 6
  120. LIBIMG_ERROR_SCALE              = 7
  121. LIBIMG_ERROR_INTER              = 8
  122. LIBIMG_ERROR_NOT_INPLEMENTED    = 9
  123. LIBIMG_ERROR_INVALID_INPUT      = 10
  124. */
  125.  
  126. struct libimg_image
  127. {
  128.     dword checksum;  // ((Width ROL 16) OR Height) XOR Data[0]        ; ignored so far
  129.     dword w;
  130.     dword h;
  131.     dword next;
  132.     dword previous;
  133.     dword type;     // one of Image.bppN
  134.     dword imgsrc;
  135.     dword palette;  // used iff Type eq Image.bpp1, Image.bpp2, Image.bpp4 or Image.bpp8i
  136.     dword extended;
  137.     dword flags;    // bitfield
  138.     dword delay;    // used iff Image.IsAnimated is set in Flags
  139.     dword image;
  140.     void load();
  141.     void convert_into();
  142.     void replace_color();
  143.     void replace_2colors();
  144.     void set_vars();
  145.     void draw();
  146. };
  147.  
  148. :void libimg_image::set_vars()
  149. {
  150.     $push edi
  151.     EDI = image;
  152.     //checksum = ESDWORD[EDI];
  153.     w = ESDWORD[EDI+4];
  154.     h = ESDWORD[EDI+8];
  155.     //next = ESDWORD[EDI+12];
  156.     //previous = ESDWORD[EDI+16];
  157.     type = ESDWORD[EDI+20];
  158.     imgsrc = ESDWORD[EDI+24];
  159.     //palette = ESDWORD[EDI+28];
  160.     //extended = ESDWORD[EDI+32];
  161.     //flags = ESDWORD[EDI+36];
  162.     //delay = ESDWORD[EDI+40];
  163.     $pop edi
  164. }
  165.  
  166. :void libimg_image::load(dword file_path)
  167. {
  168.     if (image) img_destroy stdcall(image);
  169.     img_from_file stdcall(file_path);
  170.     if (!EAX) {
  171.         notify("'Error: Image not loaded'E");
  172.     } else {
  173.         image = EAX;
  174.         set_vars();
  175.     }
  176. }
  177.  
  178. :void libimg_image::replace_color(dword old_color, new_color)
  179. {
  180.     EDX =  w * h * 4 + imgsrc;
  181.     ESI = old_color;
  182.     ECX = new_color;
  183.     FOR (EDI = imgsrc; EDI < EDX; EDI += 4) IF (DSDWORD[EDI]==ESI) DSDWORD[EDI] = ECX;
  184. }
  185.  
  186. :void libimg_image::replace_2colors(dword old_color1, new_color1, old_color2, new_color2)
  187. {
  188.     EDX =  w * h * 4 + imgsrc;
  189.     ESI = old_color1;
  190.     ECX = new_color1;
  191.     EBX = old_color2;
  192.     EAX = new_color2;
  193.     FOR (EDI = imgsrc; EDI < EDX; EDI += 4) {
  194.         IF (DSDWORD[EDI]==ESI) DSDWORD[EDI] = ECX;
  195.         ELSE IF (DSDWORD[EDI]==EBX) DSDWORD[EDI] = EAX;
  196.     }
  197. }
  198.  
  199. :void libimg_image::draw(dword _x, _y, _w, _h, _xoff, _yoff)
  200. {
  201.     if (image) img_draw stdcall(image, _x, _y, _w, _h, _xoff, _yoff);
  202. }
  203.  
  204. :void libimg_image::convert_into(dword _to)
  205. {
  206.     img_convert stdcall(image, 0, _to, 0, 0);
  207.     if (!EAX) {
  208.         notify("'LibImg convertation error!'E");
  209.     } else {
  210.         $push eax
  211.         img_destroy stdcall(image);
  212.         $pop eax
  213.         image = EAX;
  214.         set_vars();
  215.     }
  216. }
  217.  
  218. :dword save_image(dword _image_pointer, _w, _h, _path)
  219. {
  220.     dword encoded_data=0;
  221.     dword encoded_size=0;
  222.     dword image_ptr = 0;
  223.  
  224.     img_create stdcall(_w, _h, IMAGE_BPP24);
  225.     image_ptr = EAX;
  226.  
  227.     if (!image_ptr) {
  228.         return "Error creating image!";
  229.     }
  230.     else {
  231.         EDI = image_ptr;
  232.         memmov(EDI.libimg_image.imgsrc, _image_pointer, _w * _h * 3);
  233.  
  234.         img_encode stdcall(image_ptr, LIBIMG_FORMAT_PNG, 0);
  235.         encoded_data = EAX;
  236.         encoded_size = ECX;
  237.  
  238.         img_destroy stdcall(image_ptr);
  239.  
  240.         if(!encoded_data) {
  241.             return "Error encoding image!";
  242.         }
  243.         else {
  244.             if (!CreateFile(encoded_size, encoded_data, _path)) {
  245.                 return 0;
  246.             }
  247.             else {
  248.                 return "'Error saving image file!\nNot enough space? Path wrong?\nFile system is not writable?..' -E";
  249.             }
  250.         }
  251.     }
  252. }
  253.  
  254. #endif