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