libimg/testing/main.c

132 lines
3.0 KiB
C

#include <libimg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stddef.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#ifdef FXCG50
uint16_t *gint_vram = NULL;
#endif
#ifdef FX9860G
uint32_t *gint_vram = NULL;
#endif
img_t loadbmp(char const *path)
{
int fd = open(path, O_RDONLY);
if(fd < 0) { printf("%s: %m\n", path); exit(2); }
struct stat statbuf;
fstat(fd, &statbuf);
void *data = mmap(NULL, statbuf.st_size, PROT_READ, MAP_PRIVATE, fd,0);
if(!data) { printf("mmap: %m\n"); exit(3); }
int start = *(uint32_t *)(data + 10);
int width = *(uint32_t *)(data + 18);
int height = *(uint32_t *)(data + 22);
int stride = (width + 1) & ~1;
img_t img = img_create(width, height);
/* Invert rows */
img_pixel_t *src = data + start + 2 * stride * (height - 1);
img_pixel_t *dst = img.pixels;
for(int y = 0; y < height; y++)
{
for(int x = 0; x < width; x++)
{
if(src[x] == IMG_ALPHA) dst[x] = 0x0000;
else if(src[x] == 0xffff) dst[x] = IMG_ALPHA;
else dst[x] = src[x];
}
dst += img.stride;
src -= stride;
}
return img;
}
void saveraw(img_t img, char const *path)
{
int fd = creat(path, 0644);
if(fd < 0) { printf("%s: %m\n", path); exit(4); }
img_pixel_t *px = img.pixels;
for(int y = 0; y < img.height; y++)
{
write(fd, px, img.width*2);
px += img.stride;
}
close(fd);
}
int main(void)
{
img_t sq_even = loadbmp("img/sq-even.bmp");
img_t sq_odd = loadbmp("img/sq-odd.bmp");
img_t odd_even = loadbmp("img/odd-even.bmp");
img_t even_odd = loadbmp("img/even-odd.bmp");
img_t train = loadbmp("img/train.bmp");
img_t in[4] = { sq_even, sq_odd, odd_even, even_odd };
img_t out = img_create(396,224);
img_fill(out, 0xffff);
img_fill(img_sub(out, 138, 0, 28, -1), 0xc618);
for(int i=0, y=8; i<4; i++, y+=52)
{
img_rotate (in[i], img_at(out, 8, y), 90);
img_rotate (in[i], img_at(out, 8, y+26), 0);
img_rotate (in[i], img_at(out, 34, y), 180);
img_rotate (in[i], img_at(out, 34, y+26), 270);
img_upscale(in[i], img_at(out, 60, y+1), 2);
img_hflip (in[i], img_at(out, 110, y));
img_vflip (in[i], img_at(out, 110, y+26));
img_render (in[i], img_at(out, 140, y+13));
img_dye (in[i], img_at(out, 248, y), 0x25ff);
img_dye (in[i], img_at(out, 248, y+26), 0xfd04);
img_t light = img_copy(in[i]);
img_t dark = img_copy(in[i]);
for(int k=0, x=172; k<=2; k++, x+=26)
{
img_whiten(light, light);
img_darken(dark, dark);
img_render(light, img_at(out, x, y));
img_render(dark, img_at(out, x, y+26));
}
img_destroy(light);
img_destroy(dark);
}
img_t light = img_lighten_create(train);
img_t dark = img_darken_create(train);
img_render(light, img_at(out, 282, 24));
img_render(train, img_at(out, 282, 56+24));
img_render(dark, img_at(out, 282, 56+56+24));
saveraw(out, "transform.bin");
img_destroy(out);
img_destroy(sq_odd);
img_destroy(sq_even);
return 0;
}