Subversion Repositories Kolibri OS

Compare Revisions

Regard whitespace Rev 8187 → Rev 8541

/programs/develop/ktcc/trunk/samples/clayer/libimg.c
1,24 → 1,30
/* Written by turbocat2001 (Logaev Maxim) */
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include <clayer/libimg.h>
#include <kos32sys1.h>
 
struct kolibri_system_colors sys_color_table;
#define NEW_IMG_H 128
#define NEW_IMG_W 128
 
char path[4096];
char* picture;
int x_size = 200, y_size = 150;
#define IMG_H 256
#define IMG_W 256
 
char* load_file_inmem(char* fname, int32_t* read_sz)
{
Image *image_blend; // Create image struct
 
struct kolibri_system_colors sys_color_table; // Create system colors table
 
char* load_img(char* fname, int32_t* read_sz){ // Image file upload function
FILE *f = fopen(fname, "rb");
if (!f) {
printf("Can't open file: %s\n", fname);
exit(0);
}
if (fseek(f, 0, SEEK_END)) {
printf("Can't SEEK_END file: %s\n", fname);
exit(0);
}
int filesize = ftell(f);
rewind(f);
25,62 → 31,67
char* fdata = malloc(filesize);
if(!fdata) {
printf("No memory for file %s\n", fname);
exit(0);
}
*read_sz = fread(fdata, 1, filesize, f);
if (ferror(f)) {
printf("Error reading file %s\n", fname);
exit(0);
}
fclose(f);
 
return fdata;
}
 
void draw_window()
{
void DrawGUI(){
BeginDraw();
DrawWindow(10, 40, x_size + 50, y_size + 50, "Libimg", sys_color_table.work_area, 0x34);
// Draw Picture
draw_bitmap(picture, 10, 10, x_size, y_size);
DrawWindow(10, 40, (IMG_W+NEW_IMG_W)+50, IMG_H+50, "Libimg", sys_color_table.work_area, 0x34);
img_draw(image_blend, 10, 10, IMG_W*2, IMG_H , 0, 0); // Draw blended image to window
EndDraw();
}
 
int main()
{
if (kolibri_libimg_init() == -1)
{
int main(){
if (kolibri_libimg_init() == -1){
printf("Error loading lib_img.obj\n");
exit(0);
}
 
// Load Image
const int icon_rgb_size = x_size * y_size;
char *image_data,
*filedata;
get_system_colors(&sys_color_table); // Get system colors theme
set_event_mask(0xC0000027);
strcpy(path, "kolibrios.jpg"); // Filename
int32_t read_bytes;
filedata = load_file_inmem(path, &read_bytes);
picture = malloc(icon_rgb_size * 3);
image_data = img_decode(filedata, read_bytes, 0);
img_to_rgb2(image_data, picture);
img_destroy(image_data);
free(filedata);
// End Load Image
uint32_t img_size;
void *file_data = load_img("logo.png", &img_size); // Get RAW data and size
 
get_system_colors(&sys_color_table);
set_event_mask(0xC0000027);
Image* image = img_decode(file_data, img_size, 0); // Decode RAW data to Image data
 
if (image->Type != IMAGE_BPP32) {
image = img_convert(image, NULL, IMAGE_BPP32, 0, 0); // Convert image to format BPP32
if (!image) {
printf("Сonvert error!: \n");
exit(0);
}
}
image_blend = img_create(IMG_W+NEW_IMG_W, IMG_H, IMAGE_BPP32); // Create an empty layer
img_fill_color(image_blend, IMG_W+NEW_IMG_W, IMG_H, sys_color_table.work_area); // Fill the layer with one color
img_blend(image_blend, image, 0, 0, 0, 0, IMG_W, IMG_H); // Blending images to display the alpha channel.
/* Reduce image size from 256x256 to 128x128 */
image = img_scale(image, 0, 0, IMG_W, IMG_H, NULL, LIBIMG_SCALE_STRETCH , LIBIMG_INTER_BILINEAR, NEW_IMG_W, NEW_IMG_H);
img_blend(image_blend, image, 256, 0, 0, 0, NEW_IMG_W, NEW_IMG_H);
img_destroy(image); // Destroy image structure
free(file_data); // Free allocated file_data buffer
/* Main event loop */
while (1) {
switch(get_os_event())
{
switch(get_os_event()){
case KOLIBRI_EVENT_REDRAW:
draw_window();
DrawGUI();
break;
case KOLIBRI_EVENT_BUTTON:
if (get_os_button() == 1) exit(0);
if (get_os_button() == 1){
return 0;
}
break;
}
}
return 0;
exit(0);
}