Subversion Repositories Kolibri OS

Rev

Rev 7220 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. // TODO
  2. // Settings: delay, savepath
  3. // Icons and better UI
  4.  
  5. #define MEMSIZE 1024 * 20
  6. #include "../lib/kolibri.h"
  7. #include "../lib/strings.h"
  8. #include "../lib/mem.h"
  9. #include "../lib/gui.h"
  10.  
  11. #include "../lib/obj/libimg.h"
  12.  
  13. #ifndef AUTOBUILD
  14.         #include "lang.h--"
  15. #endif
  16.  
  17. /* === TRANSLATIONS === */
  18.  
  19. #ifdef LANG_RUS
  20.         ?define T_TAKE_SCREENSHOT "‘¤¥« âì áªà¨­è®â"
  21.         ?define T_SAVE "‘®åà ­¨âì"
  22.         ?define T_PREVIEW "à¥¤¯à®á¬®âà"
  23. #else
  24.         ?define T_TAKE_SCREENSHOT "Take a screenshot"
  25.         ?define T_SAVE "Save"
  26.         ?define T_PREVIEW "Preview"
  27. #endif
  28.  
  29. /* === DATA === */
  30.  
  31. proc_info Form;
  32.  
  33. dword screenshot,
  34.       preview;
  35.  
  36. int screenshot_length,
  37.     preview_width,
  38.     preview_height,
  39.     preview_length;
  40.  
  41. enum {
  42.         BTN_MAKE_SCREENSHOT=10,
  43.         BTN_SAVE
  44. };
  45.  
  46. #define TOOLBAR_H 46;
  47.  
  48. /* === CODE === */
  49.  
  50.  
  51. void main()
  52. {      
  53.         char id;
  54.         int take_scr_btn_width;
  55.  
  56.         load_dll(libimg, #libimg_init, 1);
  57.  
  58.         screenshot_length = screen.width * screen.height * 3;
  59.         preview_width  = screen.width / 2;
  60.         preview_height = screen.height / 2;
  61.         preview_length = screenshot_length / 2;
  62.  
  63.         screenshot  = malloc(screenshot_length);
  64.         preview = malloc(screenshot_length/2);
  65.  
  66.         loop() switch(WaitEvent())
  67.         {
  68.         case evButton:
  69.                 id = GetButtonID();
  70.                 if (id == CLOSE_BTN) ExitProcess();
  71.                 if (id == BTN_MAKE_SCREENSHOT) EventTakeScreenshot();
  72.                 if (id == BTN_SAVE) EventSaveFile();
  73.                 break;
  74.  
  75.         case evKey:
  76.                 GetKeys();
  77.                 if (SCAN_CODE_KEY_S == key_scancode) EventSaveFile();
  78.                 if (SCAN_CODE_ENTER == key_scancode) EventTakeScreenshot();
  79.                 break;
  80.      
  81.         case evReDraw:
  82.                 system.color.get();
  83.                 DefineAndDrawWindow(screen.width/4, screen.height/4,
  84.                         preview_width + 9, preview_height + skin_height + TOOLBAR_H + 4,
  85.                         0x74, 0, "EasyShot v0.5",0);
  86.                 GetProcessInfo(#Form, SelfInfo);
  87.                 if (Form.status_window>2) break;
  88.                 DrawBar(0, 0, Form.cwidth, TOOLBAR_H, system.color.work);
  89.                 take_scr_btn_width = DrawStandartCaptButton(10, 10, BTN_MAKE_SCREENSHOT, T_TAKE_SCREENSHOT);
  90.                 if (ESDWORD[preview]==0) {
  91.                         DrawBar(0, TOOLBAR_H,  preview_width, preview_height, 0xEEEeee);
  92.                         WriteText(Form.cwidth-calc(strlen(T_PREVIEW)*8)/2, Form.cheight/2, 0x90, 0x777777, T_PREVIEW);
  93.                 }
  94.                 else {
  95.                         _PutImage(0, TOOLBAR_H,  preview_width, preview_height, preview);
  96.                         DrawStandartCaptButton(take_scr_btn_width + 10, 10, BTN_SAVE, T_SAVE);
  97.                 }
  98.         }
  99. }
  100.  
  101. void EventTakeScreenshot() {
  102.         MinimizeWindow();
  103.         pause(100);
  104.         CopyScreen(screenshot, 0, 0, screen.width, screen.height);
  105.         ZoomImageTo50percent();
  106.         ActivateWindow(GetProcessSlot(Form.ID));
  107. }
  108.  
  109. void EventSaveFile()
  110. {
  111.         int i=0;
  112.         char save_file_name[4096];
  113.         do {
  114.                 i++;
  115.                 sprintf(#save_file_name, "/tmp0/1/screen_%i.png", i);
  116.         } while (file_exists(#save_file_name));
  117.         SaveFile(screenshot, screen.width, screen.height, #save_file_name);
  118. }
  119.  
  120. void SaveFile(dword _image, _w, _h, _path)
  121. {
  122.         char save_success_message[4096+200];
  123.         dword encoded_data=0;
  124.         dword encoded_size=0;
  125.         dword image_ptr = 0;
  126.        
  127.         image_ptr = create_image(Image_bpp24, _w, _h);
  128.  
  129.         if (image_ptr == 0) {
  130.                 notify("'Error saving file, probably not enought memory!' -E");
  131.         }
  132.         else {
  133.                 EDI = image_ptr;
  134.                 memmov(EDI._Image.Data, _image, _w * _h * 3);
  135.  
  136.                 encoded_data = encode_image(image_ptr, LIBIMG_FORMAT_PNG, 0, #encoded_size);
  137.  
  138.                 img_destroy stdcall(image_ptr);
  139.  
  140.                 if(encoded_data == 0) {
  141.                         notify("'Error saving file, incorrect data!' -E");
  142.                 }
  143.                 else {
  144.                         if (WriteFile(encoded_size, encoded_data, _path) == 0) {
  145.                                 sprintf(#save_success_message, "'File saved as %s' -O", _path);
  146.                                 notify(#save_success_message);
  147.                         }
  148.                         else {
  149.                                 notify("'Error saving file! Probably not enought space or file system is not writable!' -E");
  150.                         }
  151.                 }
  152.         }
  153. }
  154.  
  155. inline byte calc_rgb(dword B, item_h)
  156. {
  157.         return calc(ESBYTE[B+3] + ESBYTE[B] + ESBYTE[B-3]
  158.                 + ESBYTE[B-item_h] + ESBYTE[B+item_h] / 5);
  159. }
  160.  
  161. void ZoomImageTo50percent() {
  162.         dword point_x = 0;
  163.         dword item_h = screen.width * 3;
  164.         dword small = preview;
  165.         dword big = screenshot;
  166.  
  167.         while( (small <= preview + preview_length) && (big <= screenshot + screenshot_length ) ) {
  168.                
  169.                 if (big <= screenshot + item_h) || (big >= screenshot + screenshot_length - item_h)
  170.                 {
  171.                         ESBYTE[small]   = ESBYTE[big];
  172.                         ESBYTE[small+1] = ESBYTE[big+1];
  173.                         ESBYTE[small+2] = ESBYTE[big+2];
  174.                 }
  175.                 else
  176.                 {
  177.                         ESBYTE[small]   = calc_rgb(big, item_h);
  178.                         ESBYTE[small+1] = calc_rgb(big+1, item_h);
  179.                         ESBYTE[small+2] = calc_rgb(big+2, item_h);
  180.                 }
  181.        
  182.                 small+=3;
  183.                 big+=6;
  184.  
  185.                 point_x+=2;
  186.                 if (point_x >= screen.width)
  187.                 {
  188.                         big += item_h;
  189.                         point_x = 0;
  190.                 }
  191.         }
  192. }
  193.  
  194.  
  195.  
  196. stop:
  197.