amiga_imgv
changeset 7:4c36d0f44aa6
lbm loading
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 29 Oct 2017 13:21:11 +0200 |
parents | ae0ada629b03 |
children | a9edbbdb8213 |
files | Makefile src/endian.c src/endian.h src/image.c src/image.h src/main.c src/sdl/gfx.c |
diffstat | 7 files changed, 134 insertions(+), 42 deletions(-) [+] |
line diff
1.1 --- a/Makefile Fri Oct 27 15:42:58 2017 +0300 1.2 +++ b/Makefile Sun Oct 29 13:21:11 2017 +0200 1.3 @@ -1,5 +1,6 @@ 1.4 src = $(wildcard src/*.c) 1.5 obj = $(src:.c=.o) 1.6 +dep = $(obj:.o=.d) 1.7 bin = imgv 1.8 1.9 CC = vc 1.10 @@ -17,6 +18,17 @@ 1.11 $(bin): $(obj) 1.12 $(CC) -o $@ $(obj) $(LDFLAGS) 1.13 1.14 +ifneq ($(CC), vc) 1.15 +-include $(dep) 1.16 +endif 1.17 + 1.18 +%.d: %.c 1.19 + @$(CPP) $(CFLAGS) $< -MM -MT $(@:.d=.o) >$@ 1.20 + 1.21 .PHONY: clean 1.22 clean: 1.23 rm -f $(obj) $(bin) 1.24 + 1.25 +.PHONY: cleandep 1.26 +cleandep: 1.27 + rm -f $(dep)
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/src/endian.c Sun Oct 29 13:21:11 2017 +0200 2.3 @@ -0,0 +1,58 @@ 2.4 +#include "endian.h" 2.5 + 2.6 +#ifdef NO_INET 2.7 +static uint32_t init_once32(uint32_t x); 2.8 +static uint16_t init_once16(uint16_t x); 2.9 + 2.10 +static uint16_t swap16(uint16_t x); 2.11 +static uint32_t swap32(uint32_t x); 2.12 +static uint16_t nop16(uint16_t x); 2.13 +static uint32_t nop32(uint32_t x); 2.14 + 2.15 +uint16_t (*ntohs)(uint16_t) = init_once16; 2.16 +uint16_t (*htons)(uint16_t) = init_once16; 2.17 +uint32_t (*ntohl)(uint32_t) = init_once32; 2.18 +uint32_t (*htonl)(uint32_t) = init_once32; 2.19 + 2.20 +static uint16_t init_once16(uint16_t x) 2.21 +{ 2.22 + int be = bigendian(); 2.23 + ntohs = be ? nop16 : swap16; 2.24 + htons = be ? nop16 : swap16; 2.25 + return ntohs(x); 2.26 +} 2.27 + 2.28 +static uint32_t init_once32(uint32_t x) 2.29 +{ 2.30 + int be = bigendian(); 2.31 + ntohl = be ? nop32 : swap32; 2.32 + htonl = be ? nop32 : swap32; 2.33 + return ntohl(x); 2.34 +} 2.35 + 2.36 +static uint16_t swap16(uint16_t x) 2.37 +{ 2.38 + return (x << 8) | (x >> 8); 2.39 +} 2.40 + 2.41 +static uint32_t swap32(uint32_t x) 2.42 +{ 2.43 + return (x << 24) | (x >> 24) | ((x << 16) & 0xff0000) | ((x >> 16) & 0xff00); 2.44 +} 2.45 + 2.46 +static uint16_t nop16(uint16_t x) 2.47 +{ 2.48 + return x; 2.49 +} 2.50 + 2.51 +static uint32_t nop32(uint32_t x) 2.52 +{ 2.53 + return x; 2.54 +} 2.55 +#endif 2.56 + 2.57 +int bigendian(void) 2.58 +{ 2.59 + static const uint16_t x = 0xaabb; 2.60 + return *(unsigned char*)&x == 0xaa; 2.61 +}
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/src/endian.h Sun Oct 29 13:21:11 2017 +0200 3.3 @@ -0,0 +1,20 @@ 3.4 +#ifndef ENDIAN_H_ 3.5 +#define ENDIAN_H_ 3.6 + 3.7 +#if defined(__unix__) || defined(__APPLE__) 3.8 +#include <arpa/inet.h> 3.9 +#elif defined(WIN32) 3.10 +#include <winsock2.h> 3.11 +#else 3.12 +#define NO_INET 3.13 +#include "inttypes.h" 3.14 + 3.15 +extern uint16_t (*ntohs)(uint16_t); 3.16 +extern uint16_t (*htons)(uint16_t); 3.17 +extern uint32_t (*ntohl)(uint32_t); 3.18 +extern uint32_t (*htonl)(uint32_t); 3.19 +#endif 3.20 + 3.21 +int bigendian(void); 3.22 + 3.23 +#endif /* ENDIAN_H_ */
4.1 --- a/src/image.c Fri Oct 27 15:42:58 2017 +0300 4.2 +++ b/src/image.c Sun Oct 29 13:21:11 2017 +0200 4.3 @@ -4,17 +4,7 @@ 4.4 #include <errno.h> 4.5 #include "image.h" 4.6 #include "logger.h" 4.7 - 4.8 -#ifdef __unix__ 4.9 -#include <arpa/inet.h> 4.10 -#define HAVE_NTOHS 4.11 -#else 4.12 -static int bigendian(void); 4.13 -static uint16_t swap16(uint16_t x); 4.14 -static uint16_t nop16(uint16_t x); 4.15 -static uint16_t (*ntohs)(uint16_t); 4.16 -#endif 4.17 - 4.18 +#include "endian.h" 4.19 4.20 #ifdef __GNUC__ 4.21 #define PACKED __attribute__((packed)) 4.22 @@ -39,10 +29,6 @@ 4.23 struct palchange *tail = 0; 4.24 FILE *fp; 4.25 4.26 -#ifndef HAVE_NTOHS 4.27 - ntohs = bigendian() ? nop16 : swap16; 4.28 -#endif 4.29 - 4.30 logmsg("opening file: %s\n", fname); 4.31 if(!(fp = fopen(fname, "rb"))) { 4.32 logmsg("failed to open %s: %s\n", fname, strerror(errno)); 4.33 @@ -65,6 +51,7 @@ 4.34 img->width = ntohs(hdr.width); 4.35 img->height = ntohs(hdr.height); 4.36 img->nbitplanes = hdr.nbitplanes; 4.37 + img->ham = 1; 4.38 4.39 imgsz = img->width * img->height / 8 * img->nbitplanes; 4.40 4.41 @@ -174,21 +161,3 @@ 4.42 4.43 return img; 4.44 } 4.45 - 4.46 -#ifndef HAVE_NTOHS 4.47 -static int bigendian(void) 4.48 -{ 4.49 - static const uint16_t x = 0xaabb; 4.50 - return *(unsigned char*)&x == 0xaa; 4.51 -} 4.52 - 4.53 -static uint16_t swap16(uint16_t x) 4.54 -{ 4.55 - return (x << 8) | (x >> 8); 4.56 -} 4.57 - 4.58 -static uint16_t nop16(uint16_t x) 4.59 -{ 4.60 - return x; 4.61 -} 4.62 -#endif
5.1 --- a/src/image.h Fri Oct 27 15:42:58 2017 +0300 5.2 +++ b/src/image.h Sun Oct 29 13:21:11 2017 +0200 5.3 @@ -12,12 +12,13 @@ 5.4 struct ham_image { 5.5 int width, height; 5.6 unsigned char *pixels; 5.7 - uint16_t palette[16]; 5.8 - int nbitplanes; 5.9 + uint16_t palette[256]; 5.10 + int nbitplanes, ham; 5.11 struct palchange *chglist; 5.12 }; 5.13 5.14 struct ham_image *load_ham_image(const char *fname); 5.15 struct ham_image *gen_ham_image(int w, int h, int nbpl); 5.16 +struct ham_image *load_ilbm(const char *fname); 5.17 5.18 #endif /* IMAGE_H_ */
6.1 --- a/src/main.c Fri Oct 27 15:42:58 2017 +0300 6.2 +++ b/src/main.c Sun Oct 29 13:21:11 2017 +0200 6.3 @@ -11,13 +11,37 @@ 6.4 6.5 int main(int argc, char **argv) 6.6 { 6.7 + int i; 6.8 + unsigned int flags = 0; 6.9 + const char *fname = 0; 6.10 struct ham_image *img; 6.11 6.12 - if(argv[1]) { 6.13 - if(!(img = load_ham_image(argv[1]))) { 6.14 - fprintf(stderr, "failed to load image: %s\n", argv[1]); 6.15 - return 1; 6.16 + for(i=1; i<argc; i++) { 6.17 + if(argv[i][0] == '-') { 6.18 + if(strcmp(argv[i], "-ilace") == 0) { 6.19 + flags |= GFX_ILACE; 6.20 + } else { 6.21 + fprintf(stderr, "invalid option: %s\n", argv[i]); 6.22 + return 1; 6.23 + } 6.24 + } else { 6.25 + if(fname) { 6.26 + fprintf(stderr, "unexpected argument: %s\n", argv[i]); 6.27 + return 1; 6.28 + } 6.29 + fname = argv[i]; 6.30 } 6.31 + } 6.32 + 6.33 + if(fname) { 6.34 + if(!(img = load_ilbm(fname))) { 6.35 + if(!(img = load_ham_image(fname))) { 6.36 + fprintf(stderr, "failed to load image: %s\n", fname); 6.37 + return 1; 6.38 + } 6.39 + } 6.40 + logmsg("loaded %s: %dx%d, %d bitplanes%s\n", fname, img->width, img->height, 6.41 + img->nbitplanes, img->ham ? " (HAM)" : ""); 6.42 } else { 6.43 printf("generating test image ...\n"); 6.44 if(!(img = gen_ham_image(320, 256, 6))) { 6.45 @@ -26,9 +50,13 @@ 6.46 } 6.47 } 6.48 6.49 - if(gfx_init(6, GFX_HAM) == -1) { 6.50 + if(img->nbitplanes >= 6) img->ham = 1; /* XXX */ 6.51 + if(img->ham) flags |= GFX_HAM; 6.52 + 6.53 + if(gfx_init(img->nbitplanes, flags) == -1) { 6.54 return 1; 6.55 } 6.56 + gfx_set_framebuffer(0, img->width, img->height); 6.57 gfx_wait_vblank(); 6.58 gfx_show_image(img); 6.59
7.1 --- a/src/sdl/gfx.c Fri Oct 27 15:42:58 2017 +0300 7.2 +++ b/src/sdl/gfx.c Sun Oct 29 13:21:11 2017 +0200 7.3 @@ -7,10 +7,12 @@ 7.4 static int scr_width, scr_height; 7.5 static int fb_width, fb_height; 7.6 static int num_bitplanes; 7.7 +static int init_flags; 7.8 7.9 7.10 int gfx_init(int nbpl, unsigned int flags) 7.11 { 7.12 + init_flags = flags; 7.13 num_bitplanes = nbpl; 7.14 scr_width = fb_width = (flags & GFX_HIRES) ? 640 : 320; 7.15 scr_height = fb_height = (flags & GFX_ILACE) ? 512 : 256; 7.16 @@ -161,14 +163,16 @@ 7.17 7.18 for(j=0; j<img->width; j++) { 7.19 unsigned char idx = 0; 7.20 - unsigned char ham; 7.21 + unsigned char ham = 0; 7.22 int bit = 7 - (j & 7); 7.23 7.24 for(k=0; k<img->nbitplanes; k++) { 7.25 idx |= (((*(src + k * img->width / 8) >> bit) & 1) << k); 7.26 } 7.27 7.28 - ham = (idx >> 4) & 3; 7.29 + if(init_flags & GFX_HAM) { 7.30 + ham = (idx >> 4) & 3; 7.31 + } 7.32 7.33 switch(ham) { 7.34 case 0: