Subversion Repositories Kolibri OS

Rev

Rev 7995 | 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. // values for Image.Type
  70. // must be consecutive to allow fast switch on Image.Type in support functions
  71. #define IMAGE_BPP8i  1  // indexed
  72. #define IMAGE_BPP24  2
  73. #define IMAGE_BPP32  3
  74. #define IMAGE_BPP15  4
  75. #define IMAGE_BPP16  5
  76. #define IMAGE_BPP1   6
  77. #define IMAGE_BPP8g  7  // grayscale
  78. #define IMAGE_BPP2i  8
  79. #define IMAGE_BPP4i  9
  80. #define IMAGE_BPP8a 10  // grayscale with alpha channel; application layer only!!!
  81.                         // kernel doesn't handle this image type,
  82.                         // libimg can only create and destroy such images
  83.  
  84. struct libimg_image
  85. {
  86.     dword checksum; // ((Width ROL 16) OR Height) XOR Data[0]        ; ignored so far
  87.     dword w;
  88.     dword h;
  89.     dword next;
  90.     dword previous;
  91.     dword type;     // one of Image.bppN
  92.     dword imgsrc;
  93.     dword palette;  // used iff Type eq Image.bpp1, Image.bpp2, Image.bpp4 or Image.bpp8i
  94.     dword extended;
  95.     dword flags;    // bitfield
  96.     dword delay;    // used iff Image.IsAnimated is set in Flags
  97.     dword image;
  98.     void load();
  99.     void convert_into();
  100.     void replace_color();
  101.     void set_vars();
  102.     void draw();
  103. };
  104.  
  105. :void libimg_image::set_vars()
  106. {
  107.     $push edi
  108.     EDI = image;
  109.     checksum = DSWORD[EDI];
  110.     w = ESDWORD[EDI+4];
  111.     h = ESDWORD[EDI+8];
  112.     next = ESDWORD[EDI+12];
  113.     previous = ESDWORD[EDI+16];
  114.     imgsrc = ESDWORD[EDI+24];      
  115.     palette = ESDWORD[EDI+28];      
  116.     extended = ESDWORD[EDI+32];    
  117.     flags = ESDWORD[EDI+36];        
  118.     delay = ESDWORD[EDI+40];    
  119.     $pop edi
  120. }
  121.  
  122. :void libimg_image::load(dword file_path)
  123. {
  124.     load_image(file_path);
  125.     if (!EAX) {
  126.         notify("'Error: Image not loaded'E");
  127.     } else {
  128.         image = EAX;
  129.         set_vars();
  130.     }
  131. }
  132.  
  133. :void libimg_image::replace_color(dword old_color, new_color)
  134. {
  135.     EDX =  w * h * 4 + imgsrc;
  136.     for (ESI = imgsrc; ESI < EDX; ESI += 4) if (DSDWORD[ESI]==old_color) DSDWORD[ESI] = new_color;
  137. }
  138.  
  139. :void libimg_image::draw(dword _x, _y, _w, _h, _xoff, _yoff)
  140. {
  141.     if (image) img_draw stdcall(image, _x, _y, _w, _h, _xoff, _yoff);
  142. }
  143.  
  144. :void libimg_image::convert_into(dword _to)
  145. {
  146.     img_convert stdcall(image, 0, _to, 0, 0);
  147.     if (!EAX) {
  148.         notify("'LibImg convertation error!'E");
  149.     } else {
  150.         image = EAX;
  151.         set_vars();
  152.     }
  153. }
  154.  
  155. :dword load_image(dword filename)
  156. {
  157.         //align 4
  158.         dword img_data=0;
  159.         dword img_data_len=0;
  160.         dword fh=0;
  161.         dword image=0;
  162.  
  163.         byte tmp_buf[40];
  164.         $and     img_data, 0
  165.         //$mov     eax, filename
  166.         //$push    eax        
  167.         //invoke  file.open, eax, O_READ
  168.         file_open stdcall (filename, O_READ);
  169.         $or      eax, eax
  170.         $jnz      loc05  
  171.         $stc
  172.         return 0;
  173.     @loc05:    
  174.         $mov     fh, eax
  175.         //invoke  file.size
  176.         file_size stdcall (filename);
  177.         $mov     img_data_len, ebx
  178.         //stdcall mem.Alloc, ebx
  179.         mem_Alloc(EBX);
  180.        
  181.         $test    eax, eax
  182.         $jz      error_close
  183.         $mov     img_data, eax
  184.         //invoke  file.read, [fh], eax, [img_data_len]
  185.         file_read stdcall (fh, EAX, img_data_len);
  186.         $cmp     eax, -1
  187.         $jz      error_close
  188.         $cmp     eax, img_data_len
  189.         $jnz     error_close
  190.         //invoke  file.close, [fh]
  191.         file_close stdcall (fh);
  192.         $inc     eax
  193.         $jz      error_
  194. //; img.decode checks for img.is_img
  195. //;       //invoke  img.is_img, [img_data], [img_data_len]
  196. //;       $or      eax, eax
  197. //;       $jz      exit
  198.         //invoke  img.decode, [img_data], [img_data_len], 0
  199.         EAX=img_data;
  200.         img_decode stdcall (EAX, img_data_len,0);
  201.         $or      eax, eax
  202.         $jz      error_
  203.         $cmp     image, 0
  204.         $pushf
  205.         $mov     image, eax
  206.         //call    init_frame
  207.         $popf
  208.         //call    update_image_sizes
  209.         mem_Free(img_data);//free_img_data(img_data);
  210.         $clc
  211.         return image;
  212.  
  213. @error_free:
  214.         //invoke  img.destroy, [image]
  215.         img_destroy stdcall (image);
  216.         $jmp     error_
  217.  
  218. @error_pop:
  219.         $pop     eax
  220.         $jmp     error_
  221. @error_close:
  222.         //invoke  file.close, [fh]
  223.         file_close stdcall (fh);
  224. @error_:
  225.         mem_Free(img_data);
  226.         $stc
  227.         return 0;
  228. }
  229.  
  230. :dword create_image(dword type, dword width, dword height) {
  231.     img_create stdcall(width, height, type);
  232.     return EAX;
  233. }
  234.  
  235. // size - output parameter, error code / the size of encoded data
  236. :dword encode_image(dword image_ptr, dword options, dword specific_options, dword* size) {
  237.     img_encode stdcall(image_ptr, options, specific_options);
  238.     ESDWORD[size] = ECX;
  239.    
  240.     return EAX;
  241. }
  242.  
  243. //NOTICE: DO NOT FORGET TO INIT libio AND libimg!!!
  244. #ifdef LANG_RUS
  245. #define TEXT_FILE_SAVED_AS "'Файл сохранен как "
  246. #else
  247. #define TEXT_FILE_SAVED_AS "'File saved as "
  248. #endif
  249. :void save_image(dword _image_pointer, _w, _h, _path)
  250. {
  251.     char save_success_message[4096+200];
  252.     dword encoded_data=0;
  253.     dword encoded_size=0;
  254.     dword image_ptr = 0;
  255.    
  256.     image_ptr = create_image(IMAGE_BPP24, _w, _h);
  257.  
  258.     if (image_ptr == 0) {
  259.         notify("'Error saving file, probably not enought memory!' -E");
  260.     }
  261.     else {
  262.         EDI = image_ptr;
  263.         memmov(EDI.libimg_image.imgsrc, _image_pointer, _w * _h * 3);
  264.  
  265.         encoded_data = encode_image(image_ptr, LIBIMG_FORMAT_PNG, 0, #encoded_size);
  266.  
  267.         img_destroy stdcall(image_ptr);
  268.  
  269.         if(encoded_data == 0) {
  270.             notify("'Error saving file, incorrect data!' -E");
  271.         }
  272.         else {
  273.             if (CreateFile(encoded_size, encoded_data, _path) == 0) {
  274.                 strcpy(#save_success_message, TEXT_FILE_SAVED_AS);
  275.                 strcat(#save_success_message, _path);
  276.                 strcat(#save_success_message, "' -O");
  277.                 notify(#save_success_message);
  278.             }
  279.             else {
  280.                 notify("'Error saving image file!\nNot enough space? Path wrong?\nFile system is not writable?..' -E");
  281.             }
  282.         }
  283.     }
  284. }
  285.  
  286.  
  287.  
  288. /////////////////////////////
  289. /*
  290. //  DRAW ICON PATTERN / TEMP
  291. */
  292. /////////////////////////////
  293.  
  294. :void DrawIcon32(dword x,y, _bg, icon_n) {
  295.     static libimg_image i32;
  296.     static dword bg;
  297.     //load_dll(libimg, #libimg_init,1);
  298.     if (!i32.image) || (bg!=_bg) {
  299.         bg = _bg;
  300.         i32.load("/sys/icons32.png");
  301.         i32.replace_color(0x00000000, bg);
  302.     }
  303.     if (icon_n>=0) i32.draw(x, y, 32, 32, 0, icon_n*32);
  304. }
  305.  
  306. :void DrawIcon16(dword x,y, bg, icon_n) {
  307.     static libimg_image i16;
  308.     //load_dll(libimg, #libimg_init,1);
  309.     if (!i16.image) {
  310.         i16.load("/sys/icons16.png");
  311.         i16.replace_color(0xffFFFfff, bg);
  312.         i16.replace_color(0xffCACBD6, MixColors(bg, 0, 220));
  313.     }
  314.     if (icon_n>=0) i16.draw(x, y, 16, 16, 0, icon_n*16);
  315. }
  316.  
  317.  
  318. #endif