Subversion Repositories Kolibri OS

Rev

Rev 7220 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
7221 leency 1
// TODO
2
// Settings: delay, savepath
3
// Icons and better UI
4
 
5
#define MEMSIZE 1024 * 20
5598 pavelyakov 6
#include "../lib/kolibri.h"
7
#include "../lib/strings.h"
8
#include "../lib/mem.h"
9
#include "../lib/gui.h"
5520 leency 10
 
7220 leency 11
#include "../lib/obj/libimg.h"
12
 
5520 leency 13
#ifndef AUTOBUILD
14
	#include "lang.h--"
15
#endif
16
 
7221 leency 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
 
5520 leency 29
/* === DATA === */
30
 
31
proc_info Form;
32
 
7221 leency 33
dword screenshot,
7220 leency 34
      preview;
5520 leency 35
 
7221 leency 36
int screenshot_length,
7220 leency 37
    preview_width,
38
    preview_height,
39
    preview_length;
5520 leency 40
 
7220 leency 41
enum {
42
	BTN_MAKE_SCREENSHOT=10,
43
	BTN_SAVE
44
};
5520 leency 45
 
7221 leency 46
#define TOOLBAR_H 46;
7220 leency 47
 
5520 leency 48
/* === CODE === */
49
 
50
 
51
void main()
52
{
53
	char id;
7221 leency 54
	int take_scr_btn_width;
55
 
56
	load_dll(libimg, #libimg_init, 1);
57
 
58
	screenshot_length = screen.width * screen.height * 3;
7220 leency 59
	preview_width  = screen.width / 2;
60
	preview_height = screen.height / 2;
7221 leency 61
	preview_length = screenshot_length / 2;
5520 leency 62
 
7221 leency 63
	screenshot  = malloc(screenshot_length);
64
	preview = malloc(screenshot_length/2);
5520 leency 65
 
7220 leency 66
	loop() switch(WaitEvent())
5520 leency 67
	{
7220 leency 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,
7221 leency 84
			preview_width + 9, preview_height + skin_height + TOOLBAR_H + 4,
85
			0x74, 0, "EasyShot v0.5",0);
7220 leency 86
		GetProcessInfo(#Form, SelfInfo);
87
		if (Form.status_window>2) break;
7221 leency 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);
7220 leency 90
		if (ESDWORD[preview]==0) {
7221 leency 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);
7220 leency 93
		}
94
		else {
7221 leency 95
			_PutImage(0, TOOLBAR_H,  preview_width, preview_height, preview);
96
			DrawStandartCaptButton(take_scr_btn_width + 10, 10, BTN_SAVE, T_SAVE);
7220 leency 97
		}
98
	}
5520 leency 99
}
100
 
7220 leency 101
void EventTakeScreenshot() {
5520 leency 102
	MinimizeWindow();
7220 leency 103
	pause(100);
7221 leency 104
	CopyScreen(screenshot, 0, 0, screen.width, screen.height);
5520 leency 105
	ZoomImageTo50percent();
106
	ActivateWindow(GetProcessSlot(Form.ID));
107
}
108
 
7220 leency 109
void EventSaveFile()
110
{
7221 leency 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);
7220 leency 118
}
119
 
120
void SaveFile(dword _image, _w, _h, _path)
121
{
7221 leency 122
	char save_success_message[4096+200];
7220 leency 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) {
7221 leency 145
				sprintf(#save_success_message, "'File saved as %s' -O", _path);
146
				notify(#save_success_message);
7220 leency 147
			}
148
			else {
7221 leency 149
				notify("'Error saving file! Probably not enought space or file system is not writable!' -E");
7220 leency 150
			}
151
		}
152
	}
153
}
154
 
7221 leency 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
 
5520 leency 161
void ZoomImageTo50percent() {
7221 leency 162
	dword point_x = 0;
163
	dword item_h = screen.width * 3;
164
	dword small = preview;
165
	dword big = screenshot;
5520 leency 166
 
7221 leency 167
	while( (small <= preview + preview_length) && (big <= screenshot + screenshot_length ) ) {
5520 leency 168
 
7221 leency 169
		if (big <= screenshot + item_h) || (big >= screenshot + screenshot_length - item_h)
5520 leency 170
		{
7221 leency 171
			ESBYTE[small]   = ESBYTE[big];
172
			ESBYTE[small+1] = ESBYTE[big+1];
173
			ESBYTE[small+2] = ESBYTE[big+2];
5520 leency 174
		}
175
		else
176
		{
7221 leency 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);
5520 leency 180
		}
181
 
7221 leency 182
		small+=3;
183
		big+=6;
5520 leency 184
 
185
		point_x+=2;
7220 leency 186
		if (point_x >= screen.width)
5520 leency 187
		{
7221 leency 188
			big += item_h;
5520 leency 189
			point_x = 0;
190
		}
191
	}
192
}
193
 
194
 
7221 leency 195
 
5520 leency 196
stop: