Subversion Repositories Kolibri OS

Rev

Rev 9620 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
8818 turbocat 1
/* Written by turbocat2001 (Logaev Maxim) */
2
 
9766 turbocat 3
#include 
4
#include 
8818 turbocat 5
#include 
6
#include 
7
#include 
8
 
9766 turbocat 9
#define NEW_IMG_H 128
8818 turbocat 10
#define NEW_IMG_W 128
11
 
12
#define IMG_H 256
13
#define IMG_W 256
14
 
9766 turbocat 15
Image* image_blend; // Create image struct
8818 turbocat 16
 
9766 turbocat 17
ksys_colors_table_t sys_color_table; // Create system colors table
8818 turbocat 18
 
9766 turbocat 19
void* load_img(char* fname, uint32_t* read_sz)
20
{ // Image file upload function
21
    FILE* f = fopen(fname, "rb");
8818 turbocat 22
    if (!f) {
23
        printf("Can't open file: %s\n", fname);
24
        return NULL;
25
    }
26
    if (fseek(f, 0, SEEK_END)) {
27
        printf("Can't SEEK_END file: %s\n", fname);
28
        return NULL;
29
    }
30
    int filesize = ftell(f);
31
    rewind(f);
32
    void* fdata = malloc(filesize);
9766 turbocat 33
    if (!fdata) {
8818 turbocat 34
        printf("No memory for file %s\n", fname);
35
        return NULL;
36
    }
37
    *read_sz = fread(fdata, 1, filesize, f);
38
    if (ferror(f)) {
39
        printf("Error reading file %s\n", fname);
40
        return NULL;
41
    }
42
    fclose(f);
43
    return fdata;
44
}
45
 
9766 turbocat 46
void draw_gui()
47
{
8818 turbocat 48
    _ksys_start_draw();
9766 turbocat 49
    _ksys_create_window(10, 40, (IMG_W + NEW_IMG_W) + 50, IMG_H + 50, "Libimg", sys_color_table.work_area, 0x34);
50
    img_draw(image_blend, 10, 10, IMG_W * 2, IMG_H, 0, 0); // Draw blended image to window
8818 turbocat 51
    _ksys_end_draw();
52
}
53
 
9766 turbocat 54
int main()
55
{
9620 turbocat 56
    _ksys_get_system_colors(&sys_color_table); // Get system colors theme
8818 turbocat 57
    _ksys_set_event_mask(0xC0000027);
9766 turbocat 58
 
59
    uint32_t img_size;
60
    void* file_data = load_img("logo.png", &img_size); // Get RAW data and size
61
    if (!file_data) {
8818 turbocat 62
        return 1;
63
    }
64
 
65
    Image* image = img_decode(file_data, img_size, 0); // Decode RAW data to Image data
9766 turbocat 66
 
67
    if (image->Type != IMAGE_BPP32) {
8818 turbocat 68
        image = img_convert(image, NULL, IMAGE_BPP32, 0, 0); // Convert image to format BPP32
69
        if (!image) {
9766 turbocat 70
            printf("Сonvert error!: \n");
8818 turbocat 71
            return 1;
72
        }
73
    }
9766 turbocat 74
 
75
    image_blend = img_create(IMG_W + NEW_IMG_W, IMG_H, IMAGE_BPP32);                  // Create an empty layer
76
    img_fill_color(image_blend, IMG_W + NEW_IMG_W, IMG_H, sys_color_table.work_area); // Fill the layer with one color
77
    img_blend(image_blend, image, 0, 0, 0, 0, IMG_W, IMG_H);                          // Blending images to display the alpha channel.
8818 turbocat 78
    /* Reduce image size from 256x256 to 128x128 */
9766 turbocat 79
    image = img_scale(image, 0, 0, IMG_W, IMG_H, NULL, LIBIMG_SCALE_STRETCH, LIBIMG_INTER_BILINEAR, NEW_IMG_W, NEW_IMG_H);
80
    img_blend(image_blend, image, 256, 0, 0, 0, NEW_IMG_W, NEW_IMG_H);
81
    img_destroy(image); // Destroy image structure
82
    free(file_data);    // Free allocated file_data buffer
83
 
8818 turbocat 84
    /* Main event loop */
9766 turbocat 85
    while (1) {
86
        switch (_ksys_get_event()) {
87
        case KSYS_EVENT_REDRAW:
88
            draw_gui();
89
            break;
90
 
91
        case KSYS_EVENT_BUTTON:
92
            if (_ksys_get_button() == 1) {
93
                return 0;
94
            }
95
            break;
8818 turbocat 96
        }
9766 turbocat 97
    }
98
    return 0;
8818 turbocat 99
}