backported imago from eradicate
authorJohn Tsiombikas <nuclear@member.fsf.org>
Fri, 10 Jul 2020 18:08:09 +0000 (21:08 +0300)
committerJohn Tsiombikas <nuclear@member.fsf.org>
Fri, 10 Jul 2020 18:08:09 +0000 (21:08 +0300)
libs/imago/GNUmakefile
libs/imago/Makefile
libs/imago/Makefile.dj
libs/imago/src/conv.c
libs/imago/src/imago2.c
libs/imago/src/imago2.h
libs/imago/src/imago_gl.c
libs/imago/src/inttypes.h
src/image.c [new file with mode: 0644]
src/image.h [new file with mode: 0644]
tools/ropesim/src/ropesim.c

index 8940d2a..542d91f 100644 (file)
@@ -5,7 +5,12 @@ src = $(wildcard src/*.c) \
 obj = $(src:.c=.o)
 alib = libimago.a
 
-CFLAGS = -m32 -Wno-main -g -Izlib -Ilibpng -Ijpeglib
+CC = gcc
+CFLAGS = $(arch) -fno-pie -Wno-main -g -O3 -Izlib -Ilibpng -Ijpeglib
+
+ifeq ($(shell uname -m), x86_64)
+       arch = -m32
+endif
 
 $(alib): $(obj)
        $(AR) rcs $@ $(obj)
index 7959d19..5fc9d08 100644 (file)
@@ -1,50 +1,80 @@
-libpng = png.obj pngerror.obj pngget.obj pngmem.obj pngpread.obj pngread.obj &
-pngrio.obj pngrtran.obj pngrutil.obj pngset.obj pngtrans.obj pngwio.obj &
-pngwrite.obj pngwtran.obj pngwutil.obj
-zlib = adler32.obj compress.obj crc32.obj deflate.obj gzio.obj infback.obj &
-inffast.obj inflate.obj inftrees.obj trees.obj uncompr.obj zutil.obj
-jpeglib = jcapimin.obj jcapistd.obj jccoefct.obj jccolor.obj jcdctmgr.obj &
-jchuff.obj jcinit.obj jcmainct.obj jcmarker.obj jcmaster.obj jcomapi.obj &
-jcparam.obj jcphuff.obj jcprepct.obj jcsample.obj jctrans.obj jdapimin.obj &
-jdapistd.obj jdatadst.obj jdatasrc.obj jdcoefct.obj jdcolor.obj jddctmgr.obj &
-jdhuff.obj jdinput.obj jdmainct.obj jdmarker.obj jdmaster.obj jdmerge.obj &
-jdphuff.obj jdpostct.obj jdsample.obj jdtrans.obj jerror.obj jfdctflt.obj &
-jfdctfst.obj jfdctint.obj jidctflt.obj jidctfst.obj jidctint.obj jidctred.obj &
-jmemmgr.obj jmemnobs.obj jquant1.obj jquant2.obj jutils.obj
-
-obj = conv.obj filejpeg.obj filepng.obj fileppm.obj filergbe.obj &
-filetga.obj ftmodule.obj imago2.obj imago_gl.obj modules.obj &
-$(libpng) $(zlib) $(jpeglib)
+!ifdef __UNIX__
+libpng = libpng/png.obj libpng/pngerror.obj libpng/pngget.obj libpng/pngmem.obj &
+       libpng/pngpread.obj libpng/pngread.obj libpng/pngrio.obj libpng/pngrtran.obj &
+       libpng/pngrutil.obj libpng/pngset.obj libpng/pngtrans.obj libpng/pngwio.obj &
+       libpng/pngwrite.obj libpng/pngwtran.obj libpng/pngwutil.obj
+zlib = zlib/adler32.obj zlib/compress.obj zlib/crc32.obj zlib/deflate.obj &
+       zlib/gzio.obj zlib/infback.obj zlib/inffast.obj zlib/inflate.obj &
+       zlib/inftrees.obj zlib/trees.obj zlib/uncompr.obj zlib/zutil.obj
+jpeglib = jpeglib/jcapimin.obj jpeglib/jcapistd.obj jpeglib/jccoefct.obj &
+       jpeglib/jccolor.obj jpeglib/jcdctmgr.obj jpeglib/jchuff.obj jpeglib/jcinit.obj &
+       jpeglib/jcmainct.obj jpeglib/jcmarker.obj jpeglib/jcmaster.obj jpeglib/jcomapi.obj &
+       jpeglib/jcparam.obj jpeglib/jcphuff.obj jpeglib/jcprepct.obj jpeglib/jcsample.obj &
+       jpeglib/jctrans.obj jpeglib/jdapimin.obj jpeglib/jdapistd.obj jpeglib/jdatadst.obj &
+       jpeglib/jdatasrc.obj jpeglib/jdcoefct.obj jpeglib/jdcolor.obj jpeglib/jddctmgr.obj &
+       jpeglib/jdhuff.obj jpeglib/jdinput.obj jpeglib/jdmainct.obj jpeglib/jdmarker.obj &
+       jpeglib/jdmaster.obj jpeglib/jdmerge.obj jpeglib/jdphuff.obj jpeglib/jdpostct.obj &
+       jpeglib/jdsample.obj jpeglib/jdtrans.obj jpeglib/jerror.obj jpeglib/jfdctflt.obj &
+       jpeglib/jfdctfst.obj jpeglib/jfdctint.obj jpeglib/jidctflt.obj jpeglib/jidctfst.obj &
+       jpeglib/jidctint.obj jpeglib/jidctred.obj jpeglib/jmemmgr.obj jpeglib/jmemnobs.obj &
+       jpeglib/jquant1.obj jpeglib/jquant2.obj jpeglib/jutils.obj
+obj = src/conv.obj src/filejpeg.obj src/filepng.obj src/fileppm.obj src/filergbe.obj &
+       src/filetga.obj src/ftmodule.obj src/imago2.obj src/imago_gl.obj src/modules.obj &
+       $(libpng) $(zlib) $(jpeglib)
+!else
+libpng = libpng\png.obj libpng\pngerror.obj libpng\pngget.obj libpng\pngmem.obj &
+       libpng\pngpread.obj libpng\pngread.obj libpng\pngrio.obj libpng\pngrtran.obj &
+       libpng\pngrutil.obj libpng\pngset.obj libpng\pngtrans.obj libpng\pngwio.obj &
+       libpng\pngwrite.obj libpng\pngwtran.obj libpng\pngwutil.obj
+zlib = zlib\adler32.obj zlib\compress.obj zlib\crc32.obj zlib\deflate.obj &
+       zlib\gzio.obj zlib\infback.obj zlib\inffast.obj zlib\inflate.obj &
+       zlib\inftrees.obj zlib\trees.obj zlib\uncompr.obj zlib\zutil.obj
+jpeglib = jpeglib\jcapimin.obj jpeglib\jcapistd.obj jpeglib\jccoefct.obj &
+       jpeglib\jccolor.obj jpeglib\jcdctmgr.obj jpeglib\jchuff.obj jpeglib\jcinit.obj &
+       jpeglib\jcmainct.obj jpeglib\jcmarker.obj jpeglib\jcmaster.obj jpeglib\jcomapi.obj &
+       jpeglib\jcparam.obj jpeglib\jcphuff.obj jpeglib\jcprepct.obj jpeglib\jcsample.obj &
+       jpeglib\jctrans.obj jpeglib\jdapimin.obj jpeglib\jdapistd.obj jpeglib\jdatadst.obj &
+       jpeglib\jdatasrc.obj jpeglib\jdcoefct.obj jpeglib\jdcolor.obj jpeglib\jddctmgr.obj &
+       jpeglib\jdhuff.obj jpeglib\jdinput.obj jpeglib\jdmainct.obj jpeglib\jdmarker.obj &
+       jpeglib\jdmaster.obj jpeglib\jdmerge.obj jpeglib\jdphuff.obj jpeglib\jdpostct.obj &
+       jpeglib\jdsample.obj jpeglib\jdtrans.obj jpeglib\jerror.obj jpeglib\jfdctflt.obj &
+       jpeglib\jfdctfst.obj jpeglib\jfdctint.obj jpeglib\jidctflt.obj jpeglib\jidctfst.obj &
+       jpeglib\jidctint.obj jpeglib\jidctred.obj jpeglib\jmemmgr.obj jpeglib\jmemnobs.obj &
+       jpeglib\jquant1.obj jpeglib\jquant2.obj jpeglib\jutils.obj
+obj = src\conv.obj src\filejpeg.obj src\filepng.obj src\fileppm.obj src\filergbe.obj &
+       src\filetga.obj src\ftmodule.obj src\imago2.obj src\imago_gl.obj src\modules.obj &
+       $(libpng) $(zlib) $(jpeglib)
+!endif
 
 alib = imago.lib
 
+#opt = -5 -fp5 -od
 opt = -5 -fp5 -otexan
 dbg = -d1
 def = -DPNG_NO_SNPRINTF
 
-!ifdef __UNIX__
-RM = rm -f
-!else
-RM = del
-!endif
-
 CC = wcc386
 CFLAGS = $(dbg) $(opt) $(def) -zq -bt=dos -Ilibpng -Izlib -Ijpeglib
 
-$(alib): cflags.occ $(obj)
+$(alib): $(obj)
        %write objects.lbc $(obj)
        wlib -b -n $@ @objects
 
 .c: src;libpng;jpeglib;zlib
 
-cflags.occ: Makefile
-       %write $@ $(CFLAGS)
-
 .c.obj: .autodepend
-       $(CC) -fo=$@ @cflags.occ $[*
+       $(CC) -fo=$@ $(CFLAGS) $[*
 
+!ifdef __UNIX__
 clean: .symbolic
-       $(RM) *.obj
-       $(RM) *.occ
-       $(RM) *.lbc
-       $(RM) $(alib)
+       rm -f $(obj)
+       rm -f $(alib)
+!else
+clean: .symbolic
+       del src\*.obj
+       del zlib\*.obj
+       del libpng\*.obj
+       del jpeglib\*.obj
+       del objects.lbc
+       del $(alib)
+!endif
index b8b014f..1755c68 100644 (file)
@@ -1,3 +1,5 @@
+-include ../../config.mk
+
 src = $(wildcard src/*.c) \
          $(wildcard zlib/*.c) \
          $(wildcard libpng/*.c) \
@@ -9,12 +11,18 @@ ifeq ($(findstring COMMAND.COM, $(SHELL)), COMMAND.COM)
        hostsys = dos
 else
        hostsys = unix
-       TOOLPREFIX = i586-pc-msdosdjgpp-
+       TCPREFIX = i586-pc-msdosdjgpp-
+endif
+
+ifeq ($(USE_MMX), false)
+       arch = -march=pentium
+else
+       arch = -march=pentium-mmx
 endif
 
-CC = $(TOOLPREFIX)gcc
-AR = $(TOOLPREFIX)ar
-CFLAGS = -Wno-main -march=pentium -O3 -ffast-math -Izlib -Ilibpng -Ijpeglib
+CC = $(TCPREFIX)gcc
+AR = $(TCPREFIX)ar
+CFLAGS = -Wno-main $(arch) -O3 -ffast-math -Izlib -Ilibpng -Ijpeglib
 
 $(alib): $(obj)
        $(AR) rcs $@ $(obj)
index 334fe35..94fb87f 100644 (file)
@@ -1,6 +1,6 @@
 /*
 libimago - a multi-format image file input/output library.
-Copyright (C) 2010 John Tsiombikas <nuclear@member.fsf.org>
+Copyright (C) 2010-2020 John Tsiombikas <nuclear@member.fsf.org>
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU Lesser General Public License as published
@@ -17,6 +17,7 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 #include <string.h>
 #include "imago2.h"
+#include "inttypes.h"
 
 /* pixel-format conversions are sub-optimal at the moment to avoid
  * writing a lot of code. optimize at some point ?
@@ -31,25 +32,31 @@ struct pixel {
 static void unpack_grey8(struct pixel *unp, void *pptr, int count);
 static void unpack_rgb24(struct pixel *unp, void *pptr, int count);
 static void unpack_rgba32(struct pixel *unp, void *pptr, int count);
+static void unpack_bgra32(struct pixel *unp, void *pptr, int count);
 static void unpack_greyf(struct pixel *unp, void *pptr, int count);
 static void unpack_rgbf(struct pixel *unp, void *pptr, int count);
 static void unpack_rgbaf(struct pixel *unp, void *pptr, int count);
+static void unpack_rgb565(struct pixel *unp, void *pptr, int count);
 
 static void pack_grey8(void *pptr, struct pixel *unp, int count);
 static void pack_rgb24(void *pptr, struct pixel *unp, int count);
 static void pack_rgba32(void *pptr, struct pixel *unp, int count);
+static void pack_bgra32(void *pptr, struct pixel *unp, int count);
 static void pack_greyf(void *pptr, struct pixel *unp, int count);
 static void pack_rgbf(void *pptr, struct pixel *unp, int count);
 static void pack_rgbaf(void *pptr, struct pixel *unp, int count);
+static void pack_rgb565(void *pptr, struct pixel *unp, int count);
 
 /* XXX keep in sync with enum img_fmt at imago2.h */
 static void (*unpack[])(struct pixel*, void*, int) = {
        unpack_grey8,
        unpack_rgb24,
        unpack_rgba32,
+       unpack_bgra32,
        unpack_greyf,
        unpack_rgbf,
-       unpack_rgbaf
+       unpack_rgbaf,
+       unpack_rgb565
 };
 
 /* XXX keep in sync with enum img_fmt at imago2.h */
@@ -57,9 +64,11 @@ static void (*pack[])(void*, struct pixel*, int) = {
        pack_grey8,
        pack_rgb24,
        pack_rgba32,
+       pack_bgra32,
        pack_greyf,
        pack_rgbf,
-       pack_rgbaf
+       pack_rgbaf,
+       pack_rgb565
 };
 
 
@@ -140,6 +149,20 @@ static void unpack_rgba32(struct pixel *unp, void *pptr, int count)
        }
 }
 
+static void unpack_bgra32(struct pixel *unp, void *pptr, int count)
+{
+       int i;
+       unsigned char *pix = pptr;
+
+       for(i=0; i<count; i++) {
+               unp->a = (float)*pix++ / 255.0;
+               unp->r = (float)*pix++ / 255.0;
+               unp->g = (float)*pix++ / 255.0;
+               unp->b = (float)*pix++ / 255.0;
+               unp++;
+       }
+}
+
 static void unpack_greyf(struct pixel *unp, void *pptr, int count)
 {
        int i;
@@ -180,6 +203,28 @@ static void unpack_rgbaf(struct pixel *unp, void *pptr, int count)
        }
 }
 
+static void unpack_rgb565(struct pixel *unp, void *pptr, int count)
+{
+       int i;
+       uint16_t *pix = pptr;
+
+       for(i=0; i<count; i++) {
+               uint16_t r, g, b, p = *pix++;
+               b = (p & 0x1f) << 3;
+               if(b & 8) b |= 7;       /* fill LSbits with whatever bit 0 was */
+               g = (p >> 2) & 0xfc;
+               if(g & 4) g |= 3;       /* ditto */
+               r = (p >> 8) & 0xf8;
+               if(r & 8) r |= 7;       /* same */
+
+               unp->r = (float)r / 255.0f;
+               unp->g = (float)g / 255.0f;
+               unp->b = (float)b / 255.0f;
+               unp->a = 1.0f;
+               unp++;
+       }
+}
+
 
 static void pack_grey8(void *pptr, struct pixel *unp, int count)
 {
@@ -229,6 +274,25 @@ static void pack_rgba32(void *pptr, struct pixel *unp, int count)
        }
 }
 
+static void pack_bgra32(void *pptr, struct pixel *unp, int count)
+{
+       int i;
+       unsigned char *pix = pptr;
+
+       for(i=0; i<count; i++) {
+               int r = (int)(unp->r * 255.0);
+               int g = (int)(unp->g * 255.0);
+               int b = (int)(unp->b * 255.0);
+               int a = (int)(unp->a * 255.0);
+
+               *pix++ = CLAMP(b, 0, 255);
+               *pix++ = CLAMP(g, 0, 255);
+               *pix++ = CLAMP(r, 0, 255);
+               *pix++ = CLAMP(a, 0, 255);
+               unp++;
+       }
+}
+
 static void pack_greyf(void *pptr, struct pixel *unp, int count)
 {
        int i;
@@ -258,3 +322,20 @@ static void pack_rgbaf(void *pptr, struct pixel *unp, int count)
        memcpy(pptr, unp, count * sizeof *unp);
 }
 
+
+static void pack_rgb565(void *pptr, struct pixel *unp, int count)
+{
+       int i;
+       uint16_t p, *pix = pptr;
+
+       for(i=0; i<count; i++) {
+               uint16_t r = (uint16_t)(unp->r * 31.0f);
+               uint16_t g = (uint16_t)(unp->g * 63.0f);
+               uint16_t b = (uint16_t)(unp->b * 31.0f);
+               if(r > 31) r = 31;
+               if(g > 63) g = 63;
+               if(b > 31) b = 31;
+               *pix++ = (r << 11) | (g << 5) | b;
+               unp++;
+       }
+}
index 30782dc..65c554d 100644 (file)
@@ -419,6 +419,7 @@ static int pixel_size(enum img_fmt fmt)
        case IMG_FMT_RGB24:
                return 3;
        case IMG_FMT_RGBA32:
+       case IMG_FMT_BGRA32:
                return 4;
        case IMG_FMT_GREYF:
                return sizeof(float);
@@ -426,6 +427,8 @@ static int pixel_size(enum img_fmt fmt)
                return 3 * sizeof(float);
        case IMG_FMT_RGBAF:
                return 4 * sizeof(float);
+       case IMG_FMT_RGB565:
+               return 2;
        default:
                break;
        }
index b0bea09..8086d28 100644 (file)
@@ -32,9 +32,11 @@ enum img_fmt {
        IMG_FMT_GREY8,
        IMG_FMT_RGB24,
        IMG_FMT_RGBA32,
+       IMG_FMT_BGRA32,
        IMG_FMT_GREYF,
        IMG_FMT_RGBF,
        IMG_FMT_RGBAF,
+       IMG_FMT_RGB565,
 
        NUM_IMG_FMT
 };
index 7dc444d..4dbfada 100644 (file)
@@ -207,6 +207,11 @@ unsigned int img_gltexture_read(struct img_io *io)
 #endif
 
 #include <dlfcn.h>
+
+#ifndef RTLD_DEFAULT
+#define RTLD_DEFAULT   0
+#endif
+
 #endif
 #ifdef WIN32
 #include <windows.h>
index bfa5d5c..cbcc4d0 100644 (file)
@@ -18,7 +18,7 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #ifndef INT_TYPES_H_
 #define INT_TYPES_H_
 
-#if defined(__DOS__) || defined(__MSDOS__) || defined(WIN32)
+#if defined(__DOS__) || defined(__MSDOS__)
 typedef char int8_t;
 typedef short int16_t;
 typedef long int32_t;
@@ -29,7 +29,29 @@ typedef unsigned long uint32_t;
 
 typedef unsigned long intptr_t;
 #else
+
+#ifdef _MSC_VER
+typedef __int8 int8_t;
+typedef __int16 int16_t;
+typedef __int32 int32_t;
+typedef unsigned __int8 uint8_t;
+typedef unsigned __int16 uint16_t;
+typedef unsigned __int32 uint32_t;
+
+#ifdef _WIN64
+typedef __int64 intptr_t;
+#else
+typedef __int32 intptr_t;
+#endif
+#else  /* not msvc */
+
+#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199900
 #include <stdint.h>
+#else
+#include <sys/types.h>
 #endif
 
+#endif /* end !msvc */
+#endif /* end !dos */
+
 #endif /* INT_TYPES_H_ */
diff --git a/src/image.c b/src/image.c
new file mode 100644 (file)
index 0000000..d4288a6
--- /dev/null
@@ -0,0 +1,219 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <imago2.h>
+#include "image.h"
+#include "treestor.h"
+#include "util.h"
+
+int load_image(struct image *img, const char *fname)
+{
+       FILE *fp;
+       char sig[8];
+       uint16_t width, height;
+
+       memset(img, 0, sizeof *img);
+
+       if(!(fp = fopen(fname, "rb"))) {
+               fprintf(stderr, "load_image: failed to open file: %s: %s\n", fname, strerror(errno));
+               return -1;
+       }
+       if(fread(sig, 1, 8, fp) < 8) {
+               fprintf(stderr, "unexpected EOF while reading: %s\n", fname);
+               fclose(fp);
+               return -1;
+       }
+
+       if(memcmp(sig, "IDUMP565", 8) != 0) {
+               goto not565;
+       }
+
+       /* it's a 565 dump, read it and return */
+       if(!fread(&width, 2, 1, fp) || !fread(&height, 2, 1, fp)) {
+               fprintf(stderr, "unexpected EOF while reading: %s\n", fname);
+               fclose(fp);
+               return -1;
+       }
+#ifdef BUILD_BIGENDIAN
+       width = BSWAP16(width);
+       height = BSWAP16(height);
+#endif
+
+       if(!(img->pixels = malloc(width * height * 2))) {
+               fprintf(stderr, "failed to allocate %dx%d pixel buffer for %s\n", width, height, fname);
+               fclose(fp);
+               return -1;
+       }
+       if(fread(img->pixels, 2, width * height, fp) < width * height) {
+               fprintf(stderr, "unexpected EOF while reading: %s\n", fname);
+               free(img->pixels);
+               img->pixels = 0;
+               fclose(fp);
+               return -1;
+       }
+
+#ifdef BUILD_BIGENDIAN
+       {
+               int i, npix = width * height;
+               for(i=0; i<npix; i++) {
+                       uint16_t p = img->pixels[i];
+                       img->pixels[i] = BSWAP16(p);
+               }
+       }
+#endif
+
+       fclose(fp);
+       img->width = width;
+       img->height = height;
+       return 0;
+not565:
+       fclose(fp);
+
+       if(memcmp(sig, "animtex", 7) == 0) {
+               /* it's an animated texture. read metadata, and recurse with the new name*/
+               struct ts_node *root, *node;
+               const char *imgfname;
+               int fps, num_frames = 0;
+
+               if(!(root = ts_load(fname))) {
+                       fprintf(stderr, "failed to load animation %s\n", fname);
+                       return -1;
+               }
+               if(!(imgfname = ts_lookup_str(root, "animtex.image", 0))) {
+                       fprintf(stderr, "animtex %s missing `image` attribute\n", fname);
+                       ts_free_tree(root);
+                       return -1;
+               }
+               if(strcmp(imgfname, fname) == 0) {
+                       fprintf(stderr, "animtex %s is silly...\n", fname);
+                       ts_free_tree(root);
+                       return -1;
+               }
+
+               if(load_image(img, imgfname) == -1) {
+                       ts_free_tree(root);
+                       return -1;
+               }
+
+               fps = ts_lookup_int(root, "animtex.framerate", 25);
+               img->frame_interval = 1.0f / (float)fps;
+
+               /* count frames */
+               node = root->child_list;
+               while(node) {
+                       if(strcmp(node->name, "frame") == 0) {
+                               num_frames++;
+                       }
+                       node = node->next;
+               }
+
+               if(!(img->uoffs = malloc(2 * num_frames * sizeof *img->uoffs))) {
+                       fprintf(stderr, "animtex %s: failed to allocate uvoffset array for %d frames\n", fname, num_frames);
+                       free(img->pixels);
+                       ts_free_tree(root);
+                       return -1;
+               }
+               img->voffs = img->uoffs + num_frames;
+
+               num_frames = 0;
+               node = root->child_list;
+               while(node) {
+                       if(strcmp(node->name, "frame") == 0) {
+                               float *v = ts_get_attr_vec(node, "uvoffset", 0);
+                               if(v) {
+                                       img->uoffs[num_frames] = v[0];
+                                       img->voffs[num_frames++] = v[1];
+                               } else {
+                                       fprintf(stderr, "animtex %s: ignoring frame without uvoffset\n", fname);
+                               }
+                       }
+                       node = node->next;
+               }
+
+               img->num_frames = num_frames;
+
+               ts_free_tree(root);
+               return 0;
+       }
+
+       /* just a regular image */
+       if(!(img->pixels = img_load_pixels(fname, &img->width, &img->height, IMG_FMT_RGB565))) {
+               fprintf(stderr, "failed to load image: %s\n", fname);
+               return -1;
+       }
+       return 0;
+}
+
+int dump_image(struct image *img, const char *fname)
+{
+       FILE *fp;
+       uint16_t width, height;
+
+#ifdef BUILD_BIGENDIAN
+       int i, npix = img->width * img->height;
+       width = BSWAP16(img->width);
+       height = BSWAP16(img->height);
+#else
+       width = img->width;
+       height = img->height;
+#endif
+
+       if(!(fp = fopen(fname, "wb"))) {
+               fprintf(stderr, "dump_image: failed to open %s: %s\n", fname, strerror(errno));
+               return -1;
+       }
+       fwrite("IDUMP565", 1, 8, fp);
+       fwrite(&width, 2, 1, fp);
+       fwrite(&height, 2, 1, fp);
+
+#ifdef BUILD_BIGENDIAN
+       for(i=0; i<npix; i++) {
+               uint16_t p = BSWAP16(img->pixels[i]);
+               fwrite(&p, 2, 1, fp);
+       }
+#else
+       fwrite(img->pixels, 2, img->width * img->height, fp);
+#endif
+       fclose(fp);
+       return 0;
+}
+
+void destroy_image(struct image *img)
+{
+       if(img) {
+               img_free_pixels(img->pixels);
+               img->pixels = 0;
+       }
+}
+
+int load_cubemap(struct image *cube, const char *fname_fmt)
+{
+       int i;
+       char dirstr[3] = {0};
+       char fname[256];
+
+       for(i=0; i<6; i++) {
+               dirstr[0] = i & 1 ? 'n' : 'p';
+               dirstr[1] = i < 2 ? 'x' : (i < 4 ? 'y' : 'z');
+               sprintf(fname, fname_fmt, dirstr);
+               if(load_image(cube + i, fname) == -1) {
+                       while(--i >= 0) {
+                               destroy_image(cube + i);
+                       }
+                       return -1;
+               }
+       }
+       return 0;
+}
+
+void destroy_cubemap(struct image *cube)
+{
+       int i;
+
+       if(!cube) return;
+
+       for(i=0; i<6; i++) {
+               destroy_image(cube + i);
+       }
+}
diff --git a/src/image.h b/src/image.h
new file mode 100644 (file)
index 0000000..5da8652
--- /dev/null
@@ -0,0 +1,23 @@
+#ifndef IMAGE_H_
+#define IMAGE_H_
+
+#include "inttypes.h"
+
+struct image {
+       int width, height;
+       uint16_t *pixels;
+
+       /* optional animation data */
+       float cur_dur, frame_interval;
+       int cur_frame, num_frames;
+       float *uoffs, *voffs;
+};
+
+int load_image(struct image *img, const char *fname);
+int dump_image(struct image *img, const char *fname);
+void destroy_image(struct image *img);
+
+int load_cubemap(struct image *cube, const char *fname_fmt);
+void destroy_cubemap(struct image *cube);
+
+#endif /* IMAGE_H_ */
index eea2add..33279be 100644 (file)
@@ -67,12 +67,12 @@ static void step(struct rsim_world *rsim, struct rsim_rope *rope, float dt)
                                cgm_vscale(&dir, s);
                        }
                        fmag = (len - spr->rest_len) * spr->k;
-                       if(i == 5) {
+                       /*if(i == 5) {
                                printf("%d-%d fmag: %f\n", i, j, fmag);
                                if(fmag > 20) asm volatile("int $3");
-                       }
+                       }*/
 
-                       assert(rope->masses[j].m != 0.0f);
+                       //assert(rope->masses[j].m != 0.0f);
                        cgm_vscale(&dir, fmag / rope->masses[j].m);
                        cgm_vadd(&rope->masses[j].f, &dir);
                }