Subversion Repositories Kolibri OS

Rev

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