Subversion Repositories Kolibri OS

Rev

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