Subversion Repositories Kolibri OS

Rev

Rev 7893 | Rev 8013 | 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. #ifndef INCLUDE_LIBIO_H
  18. #include "../lib/obj/libio.h"
  19. #endif
  20.  
  21. //library
  22. dword libimg = #alibimg;
  23. char alibimg[] = "/sys/lib/libimg.obj";
  24.        
  25. dword libimg_init = #alibimg_init;
  26. dword img_is_img  = #aimg_is_img;
  27. dword img_to_rgb2 = #aimg_to_rgb2;
  28. dword img_decode  = #aimg_decode;
  29. dword img_destroy = #aimg_destroy;
  30. dword img_draw    = #aimg_draw;
  31. dword img_create  = #aimg_create;
  32. dword img_encode  = #aimg_encode;
  33. dword img_convert = #aimg_convert;
  34.  
  35. //dword img_flip    = #aimg_flip;
  36. //dword img_rotate  = #aimg_rotate;
  37. $DD 2 dup 0
  38.  
  39. //import  libimg                     , \
  40. char alibimg_init[] = "lib_init";
  41. char aimg_is_img[]  = "img_is_img";
  42. char aimg_to_rgb2[] = "img_to_rgb2";
  43. char aimg_decode[]  = "img_decode";
  44. char aimg_destroy[] = "img_destroy";
  45. char aimg_draw[]    = "img_draw";
  46. char aimg_create[]  = "img_create";
  47. char aimg_encode[]  = "img_encode";
  48. char aimg_convert[] = "img_convert";
  49. //char aimg_flip[]    = "img_flip";
  50. //char aimg_rotate[]  = "img_rotate ";
  51.  
  52. //invoke  img.scale, ebx, 0, 0, [ebx + Image.Width], [ebx + Image.Height], 0, LIBIMG_SCALE_TYPE_STRETCH, LIBIMG_SCALE_ALG_BILINEAR, edx, ecx
  53.  
  54. #define LIBIMG_FORMAT_BMP       1
  55. #define LIBIMG_FORMAT_ICO       2
  56. #define LIBIMG_FORMAT_CUR       3
  57. #define LIBIMG_FORMAT_GIF       4
  58. #define LIBIMG_FORMAT_PNG       5
  59. #define LIBIMG_FORMAT_JPEG      6
  60. #define LIBIMG_FORMAT_TGA       7
  61. #define LIBIMG_FORMAT_PCX       8
  62. #define LIBIMG_FORMAT_XCF       9
  63. #define LIBIMG_FORMAT_TIFF     10
  64. #define LIBIMG_FORMAT_PNM      11
  65. #define LIBIMG_FORMAT_WBMP     12
  66. #define LIBIMG_FORMAT_XBM      13
  67. #define LIBIMG_FORMAT_Z80      14
  68.  
  69. struct _Image
  70. {
  71.    dword Checksum; // ((Width ROL 16) OR Height) XOR Data[0]        ; ignored so far
  72.    dword Width;
  73.    dword Height;
  74.    dword Next;
  75.    dword Previous;
  76.    dword Type;     // one of Image.bppN
  77.    dword Data;
  78.    dword Palette;  // used iff Type eq Image.bpp1, Image.bpp2, Image.bpp4 or Image.bpp8i
  79.    dword Extended;
  80.    dword Flags;    // bitfield
  81.    dword Delay;    // used iff Image.IsAnimated is set in Flags
  82. };
  83.  
  84. // values for Image.Type
  85. // must be consecutive to allow fast switch on Image.Type in support functions
  86. #define Image_bpp8i  1  // indexed
  87. #define Image_bpp24  2
  88. #define Image_bpp32  3
  89. #define Image_bpp15  4
  90. #define Image_bpp16  5
  91. #define Image_bpp1   6
  92. #define Image_bpp8g  7  // grayscale
  93. #define Image_bpp2i  8
  94. #define Image_bpp4i  9
  95. #define Image_bpp8a 10  // grayscale with alpha channel; application layer only!!!
  96.                         // kernel doesn't handle this image type,
  97.                         // libimg can only create and destroy such images
  98.  
  99.  
  100. :dword load_image(dword filename)
  101. {
  102.         //align 4
  103.         dword img_data=0;
  104.         dword img_data_len=0;
  105.         dword fh=0;
  106.         dword image=0;
  107.  
  108.         byte tmp_buf[40];
  109.         $and     img_data, 0
  110.         //$mov     eax, filename
  111.         //$push    eax        
  112.         //invoke  file.open, eax, O_READ
  113.         file_open stdcall (filename, O_READ);
  114.         $or      eax, eax
  115.         $jnz      loc05  
  116.         $stc
  117.         return 0;
  118.     @loc05:    
  119.         $mov     fh, eax
  120.         //invoke  file.size
  121.         file_size stdcall (filename);
  122.         $mov     img_data_len, ebx
  123.         //stdcall mem.Alloc, ebx
  124.         mem_Alloc(EBX);
  125.        
  126.         $test    eax, eax
  127.         $jz      error_close
  128.         $mov     img_data, eax
  129.         //invoke  file.read, [fh], eax, [img_data_len]
  130.         file_read stdcall (fh, EAX, img_data_len);
  131.         $cmp     eax, -1
  132.         $jz      error_close
  133.         $cmp     eax, img_data_len
  134.         $jnz     error_close
  135.         //invoke  file.close, [fh]
  136.         file_close stdcall (fh);
  137.         $inc     eax
  138.         $jz      error_
  139. //; img.decode checks for img.is_img
  140. //;       //invoke  img.is_img, [img_data], [img_data_len]
  141. //;       $or      eax, eax
  142. //;       $jz      exit
  143.         //invoke  img.decode, [img_data], [img_data_len], 0
  144.         EAX=img_data;
  145.         img_decode stdcall (EAX, img_data_len,0);
  146.         $or      eax, eax
  147.         $jz      error_
  148.         $cmp     image, 0
  149.         $pushf
  150.         $mov     image, eax
  151.         //call    init_frame
  152.         $popf
  153.         //call    update_image_sizes
  154.         mem_Free(img_data);//free_img_data(img_data);
  155.         $clc
  156.         return image;
  157.  
  158. @error_free:
  159.         //invoke  img.destroy, [image]
  160.         img_destroy stdcall (image);
  161.         $jmp     error_
  162.  
  163. @error_pop:
  164.         $pop     eax
  165.         $jmp     error_
  166. @error_close:
  167.         //invoke  file.close, [fh]
  168.         file_close stdcall (fh);
  169. @error_:
  170.         mem_Free(img_data);
  171.         $stc
  172.         return 0;
  173. }
  174.  
  175. :dword create_image(dword type, dword width, dword height) {
  176.     img_create stdcall(width, height, type);
  177.     return EAX;
  178. }
  179.  
  180. // size - output parameter, error code / the size of encoded data
  181. :dword encode_image(dword image_ptr, dword options, dword specific_options, dword* size) {
  182.     img_encode stdcall(image_ptr, options, specific_options);
  183.     ESDWORD[size] = ECX;
  184.    
  185.     return EAX;
  186. }
  187.  
  188. :void DrawLibImage(dword image_pointer,x,y,w,h,offx,offy) {
  189.     img_draw stdcall (
  190.         image_pointer,
  191.         x,
  192.         y,
  193.         w,
  194.         h,
  195.         offx,
  196.         offy
  197.     );  
  198. }
  199.  
  200. //NOTICE: DO NOT FORGET TO INIT libio AND libimg!!!
  201. #ifdef LANG_RUS
  202. #define TEXT_FILE_SAVED_AS "'” ©« á®åà ­¥­ ª ª "
  203. #else
  204. #define TEXT_FILE_SAVED_AS "'File saved as "
  205. #endif
  206. :void save_image(dword _image_pointer, _w, _h, _path)
  207. {
  208.     char save_success_message[4096+200];
  209.     dword encoded_data=0;
  210.     dword encoded_size=0;
  211.     dword image_ptr = 0;
  212.    
  213.     image_ptr = create_image(Image_bpp24, _w, _h);
  214.  
  215.     if (image_ptr == 0) {
  216.         notify("'Error saving file, probably not enought memory!' -E");
  217.     }
  218.     else {
  219.         EDI = image_ptr;
  220.         memmov(EDI._Image.Data, _image_pointer, _w * _h * 3);
  221.  
  222.         encoded_data = encode_image(image_ptr, LIBIMG_FORMAT_PNG, 0, #encoded_size);
  223.  
  224.         img_destroy stdcall(image_ptr);
  225.  
  226.         if(encoded_data == 0) {
  227.             notify("'Error saving file, incorrect data!' -E");
  228.         }
  229.         else {
  230.             if (CreateFile(encoded_size, encoded_data, _path) == 0) {
  231.                 strcpy(#save_success_message, TEXT_FILE_SAVED_AS);
  232.                 strcat(#save_success_message, _path);
  233.                 strcat(#save_success_message, "' -O");
  234.                 notify(#save_success_message);
  235.             }
  236.             else {
  237.                 notify("'Error saving image file!\nNot enough space? Path wrong?\nFile system is not writable?..' -E");
  238.             }
  239.         }
  240.     }
  241. }
  242.  
  243. :dword convert_image(dword _image_pointer, _w, _h, _path)
  244. {
  245.     img_convert stdcall(_image_pointer, 0, Image_bpp32, 0, 0);
  246.     if (EAX!=0)
  247.     mov     [image_converted], eax    
  248. }
  249.  
  250. #ifndef INCLUDE_LIBIMG_LOAD_SKIN_H
  251. #include "../lib/patterns/libimg_load_skin.h"
  252. #endif
  253.  
  254.  
  255. #endif