Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
3107 leency 1
//Asper
5598 pavelyakov 2
#ifndef INCLUDE_LIBIMG_H
3
#define INCLUDE_LIBIMG_H
3107 leency 4
 
5598 pavelyakov 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
7049 leency 18
#include "../lib/obj/libio.h"
5598 pavelyakov 19
#endif
20
 
3107 leency 21
//library
22
dword libimg = #alibimg;
7252 leency 23
char alibimg[] = "/sys/lib/libimg.obj";
5626 leency 24
 
3107 leency 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;
7229 leency 31
dword img_create  = #aimg_create;
32
dword img_encode  = #aimg_encode;
7977 leency 33
dword img_convert = #aimg_convert;
7190 leency 34
 
3107 leency 35
//dword img_flip    = #aimg_flip;
36
//dword img_rotate  = #aimg_rotate;
3839 Asper 37
$DD 2 dup 0
3107 leency 38
 
39
//import  libimg                     , \
7252 leency 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";
7977 leency 46
char aimg_create[]  = "img_create";
47
char aimg_encode[]  = "img_encode";
48
char aimg_convert[] = "img_convert";
7252 leency 49
//char aimg_flip[]    = "img_flip";
50
//char aimg_rotate[]  = "img_rotate ";
3107 leency 51
 
7977 leency 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
 
7190 leency 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
3107 leency 68
 
7190 leency 69
struct _Image
70
{
71
   dword Checksum; // ((Width ROL 16) OR Height) XOR Data[0]        ; ignored so far
72
   dword Width;
73
   dword Height;
74
   dword Next;
75
   dword Previous;
76
   dword Type;     // one of Image.bppN
77
   dword Data;
78
   dword Palette;  // used iff Type eq Image.bpp1, Image.bpp2, Image.bpp4 or Image.bpp8i
79
   dword Extended;
80
   dword Flags;    // bitfield
81
   dword Delay;    // used iff Image.IsAnimated is set in Flags
82
};
3107 leency 83
 
7190 leency 84
// values for Image.Type
85
// must be consecutive to allow fast switch on Image.Type in support functions
86
#define Image_bpp8i  1  // indexed
87
#define Image_bpp24  2
88
#define Image_bpp32  3
89
#define Image_bpp15  4
90
#define Image_bpp16  5
91
#define Image_bpp1   6
92
#define Image_bpp8g  7  // grayscale
93
#define Image_bpp2i  8
94
#define Image_bpp4i  9
7254 leency 95
#define Image_bpp8a 10  // grayscale with alpha channel; application layer only!!!
96
                        // kernel doesn't handle this image type,
97
                        // libimg can only create and destroy such images
7190 leency 98
 
99
 
7224 leency 100
:dword load_image(dword filename)
3107 leency 101
{
102
        //align 4
103
        dword img_data=0;
104
        dword img_data_len=0;
105
        dword fh=0;
106
        dword image=0;
107
 
108
        byte tmp_buf[40];
109
        $and     img_data, 0
110
        //$mov     eax, filename
111
        //$push    eax
112
        //invoke  file.open, eax, O_READ
113
        file_open stdcall (filename, O_READ);
114
        $or      eax, eax
115
        $jnz      loc05
116
        $stc
117
        return 0;
118
    @loc05:
119
        $mov     fh, eax
120
        //invoke  file.size
121
        file_size stdcall (filename);
122
        $mov     img_data_len, ebx
123
        //stdcall mem.Alloc, ebx
124
        mem_Alloc(EBX);
125
 
126
        $test    eax, eax
127
        $jz      error_close
128
        $mov     img_data, eax
129
        //invoke  file.read, [fh], eax, [img_data_len]
130
        file_read stdcall (fh, EAX, img_data_len);
131
        $cmp     eax, -1
132
        $jz      error_close
133
        $cmp     eax, img_data_len
134
        $jnz     error_close
135
        //invoke  file.close, [fh]
136
        file_close stdcall (fh);
137
        $inc     eax
138
        $jz      error_
139
//; img.decode checks for img.is_img
140
//;       //invoke  img.is_img, [img_data], [img_data_len]
141
//;       $or      eax, eax
142
//;       $jz      exit
143
        //invoke  img.decode, [img_data], [img_data_len], 0
144
        EAX=img_data;
145
        img_decode stdcall (EAX, img_data_len,0);
146
        $or      eax, eax
147
        $jz      error_
148
        $cmp     image, 0
149
        $pushf
150
        $mov     image, eax
151
        //call    init_frame
152
        $popf
153
        //call    update_image_sizes
154
        mem_Free(img_data);//free_img_data(img_data);
155
        $clc
156
        return image;
157
 
158
@error_free:
159
        //invoke  img.destroy, [image]
160
        img_destroy stdcall (image);
161
        $jmp     error_
162
 
163
@error_pop:
164
        $pop     eax
165
        $jmp     error_
166
@error_close:
167
        //invoke  file.close, [fh]
168
        file_close stdcall (fh);
169
@error_:
170
        mem_Free(img_data);
171
        $stc
172
        return 0;
173
}
5598 pavelyakov 174
 
7224 leency 175
:dword create_image(dword type, dword width, dword height) {
176
    img_create stdcall(width, height, type);
177
    return EAX;
178
}
179
 
180
// size - output parameter, error code / the size of encoded data
181
:dword encode_image(dword image_ptr, dword options, dword specific_options, dword* size) {
182
    img_encode stdcall(image_ptr, options, specific_options);
183
    ESDWORD[size] = ECX;
184
 
185
    return EAX;
186
}
187
 
188
:void DrawLibImage(dword image_pointer,x,y,w,h,offx,offy) {
6978 leency 189
    img_draw stdcall (
190
        image_pointer,
191
        x,
192
        y,
193
        w,
194
        h,
195
        offx,
196
        offy
197
    );
198
}
199
 
7254 leency 200
//NOTICE: DO NOT FORGET TO INIT libio AND libimg!!!
7867 leency 201
#ifdef LANG_RUS
7893 leency 202
#define TEXT_FILE_SAVED_AS "'Файл сохранен как "
203
#else
7867 leency 204
#define TEXT_FILE_SAVED_AS "'File saved as "
205
#endif
7254 leency 206
:void save_image(dword _image_pointer, _w, _h, _path)
7224 leency 207
{
208
    char save_success_message[4096+200];
209
    dword encoded_data=0;
210
    dword encoded_size=0;
211
    dword image_ptr = 0;
212
 
213
    image_ptr = create_image(Image_bpp24, _w, _h);
7190 leency 214
 
7224 leency 215
    if (image_ptr == 0) {
216
        notify("'Error saving file, probably not enought memory!' -E");
217
    }
218
    else {
219
        EDI = image_ptr;
7254 leency 220
        memmov(EDI._Image.Data, _image_pointer, _w * _h * 3);
7224 leency 221
 
222
        encoded_data = encode_image(image_ptr, LIBIMG_FORMAT_PNG, 0, #encoded_size);
223
 
224
        img_destroy stdcall(image_ptr);
225
 
226
        if(encoded_data == 0) {
227
            notify("'Error saving file, incorrect data!' -E");
228
        }
229
        else {
7227 leency 230
            if (CreateFile(encoded_size, encoded_data, _path) == 0) {
7867 leency 231
                strcpy(#save_success_message, TEXT_FILE_SAVED_AS);
7783 leency 232
                strcat(#save_success_message, _path);
233
                strcat(#save_success_message, "' -O");
7224 leency 234
                notify(#save_success_message);
235
            }
236
            else {
7783 leency 237
                notify("'Error saving image file!\nNot enough space? Path wrong?\nFile system is not writable?..' -E");
7224 leency 238
            }
239
        }
240
    }
7190 leency 241
}
242
 
7977 leency 243
:dword convert_image(dword _image_pointer, _w, _h, _path)
244
{
245
    img_convert stdcall(_image_pointer, 0, Image_bpp32, 0, 0);
246
    if (EAX!=0)
247
    mov     [image_converted], eax
248
}
249
 
7229 leency 250
#ifndef INCLUDE_LIBIMG_LOAD_SKIN_H
251
#include "../lib/patterns/libimg_load_skin.h"
252
#endif
7224 leency 253
 
7229 leency 254
 
5598 pavelyakov 255
#endif