r3665 - in packages/trunk/hex-a-hop/debian: . tools

Miriam Ruiz baby-guest at alioth.debian.org
Mon Aug 13 20:15:11 UTC 2007


Author: baby-guest
Date: 2007-08-13 20:15:10 +0000 (Mon, 13 Aug 2007)
New Revision: 3665

Added:
   packages/trunk/hex-a-hop/debian/tools/
   packages/trunk/hex-a-hop/debian/tools/bmp2dat.c
   packages/trunk/hex-a-hop/debian/tools/dat2bmp.c
Log:
Added tools to convert to/from bmp and dat files



Added: packages/trunk/hex-a-hop/debian/tools/bmp2dat.c
===================================================================
--- packages/trunk/hex-a-hop/debian/tools/bmp2dat.c	                        (rev 0)
+++ packages/trunk/hex-a-hop/debian/tools/bmp2dat.c	2007-08-13 20:15:10 UTC (rev 3665)
@@ -0,0 +1,109 @@
+// gcc -Wall -g bmp2dat.c -o bmp2dat `sdl-config --cflags --libs`
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <limits.h>
+#include <errno.h>
+#include <string.h>
+
+#include <SDL/SDL.h>
+
+#define IMAGE_DAT_OR_MASK 0xff030303 // Reduce colour depth of images slightly for better compression (and remove useless top 8 bits!)
+
+#define ERROR_PRINTF(...) { fprintf(stderr, "%s:%s:%u Error: ", __FILE__, __PRETTY_FUNCTION__, __LINE__); fprintf(stderr,__VA_ARGS__); fflush(stderr); }
+
+static int Peek(SDL_Surface* i, int x, int y)
+{
+	if (x<0 || y<0 || x>=i->w || y>=i->h)
+		return 0;
+	unsigned int p=0;
+	const int BytesPerPixel = i->format->BytesPerPixel;
+	const int BitsPerPixel = i->format->BitsPerPixel;
+	if (BitsPerPixel==8)
+		p = ((unsigned char*)i->pixels)[i->pitch*y + x*BytesPerPixel];
+	else if (BitsPerPixel==15 || BitsPerPixel==16)
+		p = *(short*)(((char*)i->pixels) + (i->pitch*y + x*BytesPerPixel));
+	else if (BitsPerPixel==32)
+		p = *(unsigned int*)(((char*)i->pixels) + (i->pitch*y + x*BytesPerPixel));
+	else if (BitsPerPixel==24)
+		p = (int)((unsigned char*)i->pixels)[i->pitch*y + x*BytesPerPixel]
+		  | (int)((unsigned char*)i->pixels)[i->pitch*y + x*BytesPerPixel] << 8
+		  | (int)((unsigned char*)i->pixels)[i->pitch*y + x*BytesPerPixel] << 16;
+
+	return p;
+}
+
+static int IsEmpty(SDL_Surface* im, int x, int y, int w, int h)
+{
+	int i, j;
+	for (i=x; i<x+w; i++)
+		for (j=y; j<y+h; j++)
+			if (Peek(im,i,j) != Peek(im,0,im->h-1)) 
+				return 0;
+	return 1;
+}
+
+int main(int argc, const char *argv[])
+{
+	typedef unsigned int uint32;
+	SDL_Surface * g = NULL;
+	const char *bmp = NULL;
+	const char *dat = NULL;
+	int colourKey = 1;
+
+	if (argc < 3)
+	{
+		fprintf(stderr, "Usage: %s file.bmp file.dat\n", argv[0]);
+		return -1;
+	}
+
+	bmp = argv[1];
+	dat = argv[2];
+
+	g = SDL_LoadBMP(bmp);
+
+//	SDL_PixelFormat p;
+//	p.sf = 1;
+//	SDL_Surface* tmp = SDL_ConvertSurface(g, &p, SDL_SWSURFACE);
+
+	short w=g->w, h=g->h;
+	char* buf = (char*) g->pixels;
+	if (colourKey)
+	{
+		while (IsEmpty(g, w-1, 0, 1, h) && w>1)
+			w--;
+		while (IsEmpty(g, 0, h-1, w, 1) && h>1)
+			h--;
+	}
+
+	FILE* f = fopen(dat, "wb");
+	fwrite(&w, sizeof(w), 1, f);
+	fwrite(&h, sizeof(h), 1, f);
+
+	uint32 mask = IMAGE_DAT_OR_MASK;
+	int i;
+	for (i=0; i<(int)w*h; )
+	{
+		uint32 c = (*(uint32*)&buf[(i%w)*3 + (i/w)*g->pitch] | mask);
+		int i0 = i;
+		while (i < (int)w*h && c == (*(uint32*)&buf[(i%w)*3 + (i/w)*g->pitch] | mask))
+			i++;
+		c &= 0xffffff;
+		i0 = i-i0-1;
+		if (i0 < 0xff)
+			c |= i0 << 24;
+		else
+			c |= 0xff000000;
+
+		fwrite(&c, sizeof(c), 1, f);
+
+		if (i0 >= 0xff)
+			fwrite(&i0, sizeof(i0), 1, f);
+	}
+	fclose(f);
+
+	SDL_FreeSurface(g);
+
+	return 0;
+}

Added: packages/trunk/hex-a-hop/debian/tools/dat2bmp.c
===================================================================
--- packages/trunk/hex-a-hop/debian/tools/dat2bmp.c	                        (rev 0)
+++ packages/trunk/hex-a-hop/debian/tools/dat2bmp.c	2007-08-13 20:15:10 UTC (rev 3665)
@@ -0,0 +1,82 @@
+// gcc -Wall -g dat2bmp.c -o dat2bmp `sdl-config --cflags --libs`
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <limits.h>
+#include <errno.h>
+#include <string.h>
+
+#include <SDL/SDL.h>
+
+#define ERROR_PRINTF(...) { fprintf(stderr, "%s:%s:%u Error: ", __FILE__, __PRETTY_FUNCTION__, __LINE__); fprintf(stderr,__VA_ARGS__); fflush(stderr); }
+
+int main(int argc, const char *argv[])
+{
+	FILE* f = NULL;
+	uint32_t *tmp = NULL;
+	const char *bmp = NULL;
+	const char *dat = NULL;
+	SDL_Surface * g = NULL;
+
+	if (argc < 3)
+	{
+		fprintf(stderr, "Usage: %s file.dat file.bmp\n", argv[0]);
+		return -1;
+	}
+
+	dat = argv[1];
+	bmp = argv[2];
+
+	f = fopen(dat, "rb");
+	if (!f)
+	{
+		ERROR_PRINTF("Unable to open file %s: %s", dat, strerror(errno));
+		return -1;
+	}
+
+	short w,h;
+	fread(&w, sizeof(w), 1, f);
+	fread(&h, sizeof(h), 1, f);
+
+	if (w>1500 || h>1500)
+	{
+		ERROR_PRINTF("Invalid file %s", dat);
+		return -1;
+	}
+
+	tmp = (uint32_t *)calloc((int) w * h, sizeof(uint32_t));
+
+	uint32_t c = 0;
+	uint32_t cnt = 0;
+	int p = 0;
+	for (p = 0; p < (int) w * h; p++)
+	{
+		if (cnt)
+			cnt -= 0x1;
+		else
+		{
+			fread(&c, sizeof(c), 1, f);
+			cnt = c >> 24;
+			if (cnt==255)
+				fread(&cnt, sizeof(cnt), 1, f);
+		}
+		tmp[p] = c | 0xff000000;
+	}
+
+	g = SDL_CreateRGBSurfaceFrom(tmp, w, h, 32, w*4, 
+		0xff0000,
+		0xff00,
+		0xff,
+		0xff000000 );
+
+	if (SDL_SaveBMP(g, bmp) == -1)
+	{
+		ERROR_PRINTF("Unable to write BMP file %s: %s", bmp, strerror(errno));
+		return -1;
+	}
+
+	fclose(f);
+	free(tmp);
+	return 0;
+}




More information about the Pkg-games-commits mailing list