- Added new function to libimg.h

- Updated libimg example
( applicable to ktcc )

git-svn-id: svn://kolibrios.org@8541 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
superturbocat2001 2021-01-23 08:16:35 +00:00
parent fa2d611c7e
commit c68065b23e
8 changed files with 135 additions and 69 deletions

View File

@ -1,5 +1,9 @@
all: all:
fasm loadlibimg.asm fasm loadlibimg.asm
ar -csr libimg.a loadlibimg.o ar -csr libimg.a loadlibimg.o
install:
mv libimg.a ../../bin/lib/libimg.a
clean: clean:
rm -f *.o *.a rm -f *.o *.a

View File

@ -62,14 +62,13 @@ import lib_libimg, \
img_flip_layer, 'img_flip_layer', \ img_flip_layer, 'img_flip_layer', \
img_rotate, 'img_rotate', \ img_rotate, 'img_rotate', \
img_rotate_layer, 'img_rotate_layer', \ img_rotate_layer, 'img_rotate_layer', \
img_draw, 'img_draw' img_draw, 'img_draw', \
img_blend, 'img_blend', \
img_convert, 'img_convert', \
img_resize_data, 'img_resize_data', \
img_scale, 'img_scale'
public libimg_init as 'libimg_init' public libimg_init as 'libimg_init'
; public img_is_img as '_img_is_img'
;public img_info as '_img_info'
;public img_from_file as '_img_from_file'
;public img_to_file as '_img_to_file'
;public img_from_rgb as '_img_from_rgb'
public img_to_rgb as 'img_to_rgb' public img_to_rgb as 'img_to_rgb'
public img_to_rgb2 as 'img_to_rgb2' public img_to_rgb2 as 'img_to_rgb2'
public img_decode as 'img_decode' public img_decode as 'img_decode'
@ -78,10 +77,12 @@ public img_create as 'img_create'
public img_destroy as 'img_destroy' public img_destroy as 'img_destroy'
public img_destroy_layer as 'img_destroy_layer' public img_destroy_layer as 'img_destroy_layer'
public img_count as 'img_count' public img_count as 'img_count'
;public img_lock_bits as '_img_lock_bits'
;public img_unlock_bits as '_img_unlock_bits'
public img_flip as 'img_flip' public img_flip as 'img_flip'
public img_flip_layer as 'img_flip_layer' public img_flip_layer as 'img_flip_layer'
public img_rotate as 'img_rotate' public img_rotate as 'img_rotate'
public img_rotate_layer as 'img_rotate_layer' public img_rotate_layer as 'img_rotate_layer'
public img_draw as 'img_draw' public img_draw as 'img_draw'
public img_blend as 'img_blend'
public img_convert as 'img_convert'
public img_resize_data as 'img_resize_data'
public img_scale as 'img_scale'

View File

@ -1,9 +1,15 @@
/* Written by turbocat2001 (Logaev Maxim) */
#ifndef KOLIBRI_LIBIMG_H #ifndef KOLIBRI_LIBIMG_H
#define KOLIBRI_LIBIMG_H #define KOLIBRI_LIBIMG_H
#include <stddef.h> #include <stddef.h>
#include <stdbool.h>
extern int kolibri_libimg_init(void); extern int kolibri_libimg_init(void);
#define _stdcall __attribute__((__stdcall__))
//list of format id's //list of format id's
#define LIBIMG_FORMAT_BMP 1 #define LIBIMG_FORMAT_BMP 1
#define LIBIMG_FORMAT_ICO 2 #define LIBIMG_FORMAT_ICO 2
@ -20,6 +26,22 @@ extern int kolibri_libimg_init(void);
#define LIBIMG_FORMAT_XBM 13 #define LIBIMG_FORMAT_XBM 13
#define LIBIMG_FORMAT_Z80 14 #define LIBIMG_FORMAT_Z80 14
#pragma pack(push, 1)
typedef struct{
uint32_t Checksum; // ((Width ROL 16) OR Height) XOR Data[0] ; ignored so far
uint32_t Width;
uint32_t Height;
uint32_t Next;
uint32_t Previous;
uint32_t Type; // one of Image.bppN
uint32_t* Data;
uint32_t Palette; // used iff Type eq Image.bpp1, Image.bpp2, Image.bpp4 or Image.bpp8i
uint32_t Extended;
uint32_t Flags; // bitfield
uint32_t Delay; // used iff Image.IsAnimated is set in Flags
} Image;
#pragma pack(pop);
#define IMAGE_BPP8i 1 // indexed #define IMAGE_BPP8i 1 // indexed
#define IMAGE_BPP24 2 #define IMAGE_BPP24 2
#define IMAGE_BPP32 3 #define IMAGE_BPP32 3
@ -31,6 +53,25 @@ extern int kolibri_libimg_init(void);
#define IMAGE_BPP4i 9 #define IMAGE_BPP4i 9
#define IMAGE_BPP8a 10 #define IMAGE_BPP8a 10
// scale type
#define LIBIMG_SCALE_NONE 0
#define LIBIMG_SCALE_INTEGER 1
#define LIBIMG_SCALE_TILE 2
#define LIBIMG_SCALE_STRETCH 3
#define LIBIMG_SCALE_FIT_BOTH LIBIMG_SCALE_STRETCH
#define LIBIMG_SCALE_FIT_MIN 4
#define LIBIMG_SCALE_FIT_RECT LIBIMG_SCALE_FIT_MIN
#define LIBIMG_SCALE_FIT_WIDTH 5
#define LIBIMG_SCALE_FIT_HEIGHT 6
#define LIBIMG_SCALE_FIT_MAX 7
// interpolation algorithm
#define LIBIMG_INTER_NONE 0 // use it with LIBIMG_SCALE_INTEGER, LIBIMG_SCALE_TILE, etc
#define LIBIMG_INTER_BILINEAR 1
#define LIBIMG_INTER_BICUBIC 2
#define LIBIMG_INTER_LANCZOS 3
#define LIBIMG_INTER_DEFAULT LIBIMG_INTER_BILINEAR
//error codes //error codes
#define LIBIMG_ERROR_OUT_OF_MEMORY 1 #define LIBIMG_ERROR_OUT_OF_MEMORY 1
#define LIBIMG_ERROR_FORMAT 2 #define LIBIMG_ERROR_FORMAT 2
@ -49,7 +90,6 @@ extern int kolibri_libimg_init(void);
#define LIBIMG_ENCODE_DELETE_ALPHA 0x08 #define LIBIMG_ENCODE_DELETE_ALPHA 0x08
#define LIBIMG_ENCODE_FLUSH_ALPHA 0x10 #define LIBIMG_ENCODE_FLUSH_ALPHA 0x10
#define FLIP_VERTICAL 0x01 #define FLIP_VERTICAL 0x01
#define FLIP_HORIZONTAL 0x02 #define FLIP_HORIZONTAL 0x02
@ -59,18 +99,28 @@ extern int kolibri_libimg_init(void);
#define ROTATE_90_CCW ROTATE_270_CW #define ROTATE_90_CCW ROTATE_270_CW
#define ROTATE_270_CCW ROTATE_90_CW #define ROTATE_270_CCW ROTATE_90_CW
extern void* (*img_decode __attribute__((__stdcall__)))(void* file_data, uint32_t length, uint32_t options); extern Image* (*img_decode _stdcall)(void* file_data, uint32_t size, uint32_t b_color);
extern void* (*img_encode __attribute__((__stdcall__)))(void* image_data, uint32_t length, uint32_t option); extern Image* (*img_encode _stdcall)(Image* img, uint32_t length, uint32_t option);
extern void* (*img_create __attribute__((__stdcall__)))(uint32_t width, uint32_t height, uint32_t type); extern Image* (*img_create _stdcall)(uint32_t width, uint32_t height, uint32_t type);
extern void (*img_to_rgb2 __attribute__((__stdcall__)))(void* image_data, void *rgb_data); extern void (*img_to_rgb2 _stdcall)(Image* img, void *rgb_data);
extern void* (*img_to_rgb __attribute__((__stdcall__)))(void *image_data); extern Image* (*img_to_rgb _stdcall)(Image* img);
extern uint32_t (*img_flip __attribute__((__stdcall__)))(void* image_data, uint32_t flip); extern bool (*img_flip _stdcall)(Image* img, uint32_t flip);
extern uint32_t (*img_flip_layer __attribute__((__stdcall__)))(void *image_data, uint32_t flip); extern bool (*img_flip_layer _stdcall)(Image *img, uint32_t flip);
extern uint32_t (*img_rotate __attribute__((__stdcall__)))(void* image_data, uint32_t rotate); extern bool (*img_rotate _stdcall)(Image *img, uint32_t rotate);
extern uint32_t (*img_rotate_layer __attribute__((__stdcall__)))(void* image_data, uint32_t rotate); extern bool (*img_rotate_layer _stdcall)(Image* data, uint32_t rotate);
extern void (*img_draw __attribute__((__stdcall__)))(void *image_data, uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint32_t xoff, uint32_t yoff); extern void (*img_draw _stdcall)(Image *img, uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint32_t xoff, uint32_t yoff);
extern uint32_t (*img_count __attribute__((__stdcall__)))(void *image_data); extern int32_t (*img_count _stdcall)(Image *img);
extern uint32_t (*img_destroy __attribute__((__stdcall__)))(void *image_data); extern bool (*img_destroy _stdcall)(Image *img);
extern uint32_t (*img_destroy_layer __attribute__((__stdcall__)))(void* image_data); extern bool (*img_destroy_layer _stdcall)(Image* img);
extern Image* (*img_blend _stdcall)(Image* dst, Image* src, uint32_t out_x, uint32_t out_y, uint32_t in_x, uint32_t in_y, uint32_t width, uint32_t height);
extern Image* (*img_convert _stdcall)(Image *src, Image *dst, uint32_t dst_type, uint32_t, uint32_t);
extern Image* (*img_resize_data _stdcall)(Image *src, uint32_t width, uint32_t height);
extern Image* (*img_scale _stdcall)(Image* src, uint32_t crop_x, uint32_t crop_y, uint32_t crop_width, uint32_t crop_height, Image* dst, uint32_t scale_type, uint32_t inter, uint32_t new_width, uint32_t new_height);
void img_fill_color(Image* img, uint32_t width, uint32_t height, uint32_t color){
for (uint32_t i = 0; i < width*height; i++) {
img->Data[i] = color;
}
}
#endif /* KOLIBRI_LIBIMG_H */ #endif /* KOLIBRI_LIBIMG_H */

View File

@ -10,7 +10,7 @@
../tcc clayer/rasterworks.c -lck -lrasterworks -o /tmp0/1/rasterworks ../tcc clayer/rasterworks.c -lck -lrasterworks -o /tmp0/1/rasterworks
../tcc clayer/boxlib.c -lck -lbox -o /tmp0/1/boxlib_ex ../tcc clayer/boxlib.c -lck -lbox -o /tmp0/1/boxlib_ex
../tcc clayer/libimg.c -lck -limg -o /tmp0/1/libimg_ex ../tcc clayer/libimg.c -lck -limg -o /tmp0/1/libimg_ex
cp clayer/kolibrios.jpg /tmp0/1/kolibrios.jpg cp clayer/logo.png /tmp0/1/logo.png
../tcc clayer/dialog.c -lck -ldialog -o /tmp0/1/dialog_ex ../tcc clayer/dialog.c -lck -ldialog -o /tmp0/1/dialog_ex
../tcc dir_example.c -lck -o /tmp0/1/dir_example ../tcc dir_example.c -lck -o /tmp0/1/dir_example
../tcc net/tcpsrv_demo.c -lck -o /tmp0/1/tcpsrv_demo ../tcc net/tcpsrv_demo.c -lck -o /tmp0/1/tcpsrv_demo

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.7 KiB

View File

@ -1,86 +1,97 @@
/* Written by turbocat2001 (Logaev Maxim) */
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <stddef.h> #include <stddef.h>
#include <clayer/libimg.h> #include <clayer/libimg.h>
#include <kos32sys1.h> #include <kos32sys1.h>
struct kolibri_system_colors sys_color_table; #define NEW_IMG_H 128
#define NEW_IMG_W 128
char path[4096]; #define IMG_H 256
char* picture; #define IMG_W 256
int x_size = 200, y_size = 150;
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"); FILE *f = fopen(fname, "rb");
if (!f) { if (!f) {
printf("Can't open file: %s\n", fname); printf("Can't open file: %s\n", fname);
exit(0);
} }
if (fseek(f, 0, SEEK_END)) { if (fseek(f, 0, SEEK_END)) {
printf("Can't SEEK_END file: %s\n", fname); printf("Can't SEEK_END file: %s\n", fname);
exit(0);
} }
int filesize = ftell(f); int filesize = ftell(f);
rewind(f); rewind(f);
char* fdata = malloc(filesize); char* fdata = malloc(filesize);
if(!fdata) { if(!fdata) {
printf("No memory for file %s\n", fname); printf("No memory for file %s\n", fname);
exit(0);
} }
*read_sz = fread(fdata, 1, filesize, f); *read_sz = fread(fdata, 1, filesize, f);
if (ferror(f)) { if (ferror(f)) {
printf("Error reading file %s\n", fname); printf("Error reading file %s\n", fname);
exit(0);
} }
fclose(f); fclose(f);
return fdata; return fdata;
} }
void draw_window() void DrawGUI(){
{
BeginDraw(); BeginDraw();
DrawWindow(10, 40, x_size + 50, y_size + 50, "Libimg", sys_color_table.work_area, 0x34); 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
// Draw Picture
draw_bitmap(picture, 10, 10, x_size, y_size);
EndDraw(); EndDraw();
} }
int main() int main(){
{ if (kolibri_libimg_init() == -1){
if (kolibri_libimg_init() == -1)
{
printf("Error loading lib_img.obj\n"); printf("Error loading lib_img.obj\n");
exit(0);
} }
// Load Image get_system_colors(&sys_color_table); // Get system colors theme
const int icon_rgb_size = x_size * y_size; set_event_mask(0xC0000027);
char *image_data,
*filedata;
strcpy(path, "kolibrios.jpg"); // Filename uint32_t img_size;
int32_t read_bytes; void *file_data = load_img("logo.png", &img_size); // Get RAW data and size
filedata = load_file_inmem(path, &read_bytes);
picture = malloc(icon_rgb_size * 3); Image* image = img_decode(file_data, img_size, 0); // Decode RAW data to Image data
image_data = img_decode(filedata, read_bytes, 0);
img_to_rgb2(image_data, picture); if (image->Type != IMAGE_BPP32) {
img_destroy(image_data); image = img_convert(image, NULL, IMAGE_BPP32, 0, 0); // Convert image to format BPP32
free(filedata); if (!image) {
// End Load Image printf("Сonvert error!: \n");
exit(0);
get_system_colors(&sys_color_table); }
set_event_mask(0xC0000027); }
while (1) { image_blend = img_create(IMG_W+NEW_IMG_W, IMG_H, IMAGE_BPP32); // Create an empty layer
switch(get_os_event()) 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()){
case KOLIBRI_EVENT_REDRAW: case KOLIBRI_EVENT_REDRAW:
draw_window(); DrawGUI();
break; break;
case KOLIBRI_EVENT_BUTTON: case KOLIBRI_EVENT_BUTTON:
if (get_os_button() == 1) exit(0); if (get_os_button() == 1){
break; return 0;
} }
} break;
return 0; }
}
exit(0);
} }

Binary file not shown.

After

Width:  |  Height:  |  Size: 51 KiB