git » fp-git.git » commit fc806a0

Compressor code more readable

author ecalot
2005-03-24 10:27:37 UTC
committer ecalot
2005-03-24 10:27:37 UTC
parent a351a8643292cc787f742d1f5768984bb40cf568

Compressor code more readable

PR/src/Makefile +1 -1
PR/src/include/compress.h +1 -1
PR/src/include/image.h +1 -1
PR/src/lib/compression/lzg_decompress.c +7 -4
PR/src/lib/compression/rle_compress.c +1 -1
PR/src/lib/compression/rle_decompress.c +5 -4
PR/src/lib/compression/rlev_decompress.c +5 -4
PR/src/lib/object/image/image16.c +79 -21
PR/src/lib/object/image/image2.c +79 -21
PR/src/lib/object/image/image256.c +79 -21
PR/src/lib/object/image/image_common.c +79 -21

diff --git a/PR/src/Makefile b/PR/src/Makefile
index 685873d..8008ea9 100644
--- a/PR/src/Makefile
+++ b/PR/src/Makefile
@@ -50,7 +50,7 @@ else
   LINKERRELEASE = -s
 endif
 
-FILES    = import.o compress.o export.o resources.o tasks.o disk.o\
+FILES    = import.o export.o resources.o tasks.o disk.o\
            dat.o bmp.o mid.o pal.o wav.o plv.o\
            memory.o pr.o
 
diff --git a/PR/src/include/compress.h b/PR/src/include/compress.h
index 97dcc1d..9ca9b06 100644
--- a/PR/src/include/compress.h
+++ b/PR/src/include/compress.h
@@ -42,7 +42,7 @@ compress.c: Princed Resources : Image Compressor headers
 #define COMPRESS_LZG_LR 0x03
 #define COMPRESS_LZG_UD 0x04
 
-#define COMPRESS_WORKING_ALGORITHMS 3
+#define COMPRESS_WORKING_ALGORITHMS 5
 
 #define COMPRESS_RESULT_FATAL   -2
 #define COMPRESS_RESULT_WARNING -1
diff --git a/PR/src/include/image.h b/PR/src/include/image.h
index 97dcc1d..9ca9b06 100644
--- a/PR/src/include/image.h
+++ b/PR/src/include/image.h
@@ -42,7 +42,7 @@ compress.c: Princed Resources : Image Compressor headers
 #define COMPRESS_LZG_LR 0x03
 #define COMPRESS_LZG_UD 0x04
 
-#define COMPRESS_WORKING_ALGORITHMS 3
+#define COMPRESS_WORKING_ALGORITHMS 5
 
 #define COMPRESS_RESULT_FATAL   -2
 #define COMPRESS_RESULT_WARNING -1
diff --git a/PR/src/lib/compression/lzg_decompress.c b/PR/src/lib/compression/lzg_decompress.c
index 5a408ba..64f1347 100644
--- a/PR/src/lib/compression/lzg_decompress.c
+++ b/PR/src/lib/compression/lzg_decompress.c
@@ -30,6 +30,7 @@
  */
 
 #include <stdio.h>
+#include <stdlib.h>
 
 /* modulus to be used in the 10 bits of the algorithm */
 #define LZG_WINDOW_SIZE    0x400 /* =1024=1<<10 */
@@ -43,10 +44,11 @@ unsigned char popBit(unsigned char *byte) {
 
 /* Expands LZ Groody algorithm. This is the core of PR */
 int expandLzg(const unsigned char* input, int inputSize, 
-               unsigned char* output, int *outputSize) {
+               unsigned char** output2, int *outputSize) {
 
 	int           loc, oCursor=0, iCursor=0;
 	unsigned char maskbyte=0, rep, k;
+	unsigned char output[65000];
 
 	/* initialize the first 1024 bytes of the window with zeros */
 	for(oCursor=0;oCursor<LZG_WINDOW_SIZE;output[oCursor++]=0);
@@ -82,10 +84,11 @@ int expandLzg(const unsigned char* input, int inputSize,
 	}
 	
 	/* ignore the first 1024 bytes */
+	*outputSize=oCursor-LZG_WINDOW_SIZE;
+	*output2=malloc(*outputSize);
 	for(iCursor=LZG_WINDOW_SIZE;iCursor<oCursor;iCursor++)
-		output[iCursor-LZG_WINDOW_SIZE]=output[iCursor];
+		(*output2)[iCursor-LZG_WINDOW_SIZE]=output[iCursor];
 
-	*outputSize=oCursor-LZG_WINDOW_SIZE;
-	return maskbyte;
+	return (!maskbyte)-1;
 }
 
diff --git a/PR/src/lib/compression/rle_compress.c b/PR/src/lib/compression/rle_compress.c
index 3b55656..f289db6 100644
--- a/PR/src/lib/compression/rle_compress.c
+++ b/PR/src/lib/compression/rle_compress.c
@@ -41,7 +41,7 @@ void compressRle(const unsigned char* input, int inputSize,
 	unsigned char* cursorData        = output;
 	char*          counter;
 	const unsigned char* cursorPix   = input;
-	const unsigned char* imgEnd      = input+(*outputSize);
+	const unsigned char* imgEnd      = input+inputSize;
 
 	while (cursorPix<imgEnd) {
 		/* Step 1: Create counter */
diff --git a/PR/src/lib/compression/rle_decompress.c b/PR/src/lib/compression/rle_decompress.c
index c593513..73d27e3 100644
--- a/PR/src/lib/compression/rle_decompress.c
+++ b/PR/src/lib/compression/rle_decompress.c
@@ -33,28 +33,29 @@ compress.c: Princed Resources : Image Compression Library
 */
 
 #include <stdio.h>
+#include <stdlib.h>
 #include "compress.h"
 
 /* Expands RLE algorithm */
 int expandRle(const unsigned char* input, int inputSize, 
-               unsigned char* output, int *outputSize) {
+               unsigned char** output, int *outputSize) {
 	int cursor=0;
 	register signed char rep;
 	int pos=0;
 
-	if ((output=malloc(*outputSize+128))==NULL) return COMPRESS_RESULT_FATAL; /* reserve memory */
+	if ((*output=malloc(*outputSize+128))==NULL) return COMPRESS_RESULT_FATAL; /* reserve memory */
 
 	/* main loop */
 	while (cursor<*outputSize) {
 		rep=(signed char)(input[pos++]);
 		if (rep<0) {
 			/* Negative */
-			while (rep++) output[cursor++]=input[pos];
+			while (rep++) (*output)[cursor++]=input[pos];
 			pos++;
 		} else {
 			/* Positive */
 			rep=~rep;
-			while (rep++) output[cursor++]=input[pos++];
+			while (rep++) (*output)[cursor++]=input[pos++];
 		}
 	}
 	return ((pos==inputSize)&(cursor==*outputSize))-1; /* WARNING or SUCCESS */
diff --git a/PR/src/lib/compression/rlev_decompress.c b/PR/src/lib/compression/rlev_decompress.c
index c593513..73d27e3 100644
--- a/PR/src/lib/compression/rlev_decompress.c
+++ b/PR/src/lib/compression/rlev_decompress.c
@@ -33,28 +33,29 @@ compress.c: Princed Resources : Image Compression Library
 */
 
 #include <stdio.h>
+#include <stdlib.h>
 #include "compress.h"
 
 /* Expands RLE algorithm */
 int expandRle(const unsigned char* input, int inputSize, 
-               unsigned char* output, int *outputSize) {
+               unsigned char** output, int *outputSize) {
 	int cursor=0;
 	register signed char rep;
 	int pos=0;
 
-	if ((output=malloc(*outputSize+128))==NULL) return COMPRESS_RESULT_FATAL; /* reserve memory */
+	if ((*output=malloc(*outputSize+128))==NULL) return COMPRESS_RESULT_FATAL; /* reserve memory */
 
 	/* main loop */
 	while (cursor<*outputSize) {
 		rep=(signed char)(input[pos++]);
 		if (rep<0) {
 			/* Negative */
-			while (rep++) output[cursor++]=input[pos];
+			while (rep++) (*output)[cursor++]=input[pos];
 			pos++;
 		} else {
 			/* Positive */
 			rep=~rep;
-			while (rep++) output[cursor++]=input[pos++];
+			while (rep++) (*output)[cursor++]=input[pos++];
 		}
 	}
 	return ((pos==inputSize)&(cursor==*outputSize))-1; /* WARNING or SUCCESS */
diff --git a/PR/src/lib/object/image/image16.c b/PR/src/lib/object/image/image16.c
index 00be94a..74c09fb 100644
--- a/PR/src/lib/object/image/image16.c
+++ b/PR/src/lib/object/image/image16.c
@@ -47,14 +47,17 @@ compress.c: Princed Resources : Image Compression Library
 |                Internal compression prototypes                |
 \***************************************************************/
 
+/* compress and sets the bytes */
 void compressLzg(const unsigned char* input, int inputSize, 
                  unsigned char* output, int *outputSize);
 void compressRle(const unsigned char* input, int inputSize, 
                  unsigned char* output, int *outputSize);
+
+/* uncompress and allocates output */
 int expandLzg(const unsigned char* input, int inputSize, 
-               unsigned char* output, int *outputSize);
+               unsigned char** output, int *outputSize);
 int expandRle(const unsigned char* input, int inputSize, 
-               unsigned char* output, int *outputSize);
+               unsigned char** output, int *outputSize);
 
 /***************************************************************\
 |                        Image transpose                        |
@@ -67,18 +70,20 @@ int transpose(int x,int w,int h) {
 
 void transposeImage(tImage* image,int size) {
 	unsigned char* outputaux=getMemory(size);
-	int cursor=0;
+	int cursor;
 
-	while (cursor<size) {outputaux[transpose(cursor,image->widthInBytes,image->height)]=image->pix[cursor];cursor++;}
+	for (cursor=0;cursor<size;cursor++) 
+		outputaux[transpose(cursor,image->widthInBytes,image->height)]=image->pix[cursor];
 	free(image->pix);
 	image->pix=outputaux;
 }
 
 void antiTransposeImage(tImage* image,int size) {
 	unsigned char* outputaux=getMemory(size);
-	int cursor=0;
+	int cursor;
 
-	while (cursor<size) {outputaux[cursor]=image->pix[transpose(cursor,image->widthInBytes,image->height)];cursor++;}
+	for (cursor=0;cursor<size;cursor++) 
+		outputaux[cursor]=image->pix[transpose(cursor,image->widthInBytes,image->height)];
 	free(image->pix);
 	image->pix=outputaux;
 }
@@ -124,28 +129,29 @@ int mExpandGraphic(const unsigned char* data,tImage *image, int dataSizeInBytes)
 
 	switch (getAlgor(image->type)) {
 		case COMPRESS_RAW: /* No Compression Algorithm */
+						/* TODO: use dataSize */
 			if ((image->pix=getMemory(imageSizeInBytes))==NULL) return COMPRESS_RESULT_FATAL;
 			memcpy(image->pix,data,imageSizeInBytes);
 			result=COMPRESS_RESULT_SUCCESS;
 			break;
 		case COMPRESS_RLE_LR: /* RLE Left to Right Compression Algorithm */
-			result=expandRle(data,dataSizeInBytes,image,imageSizeInBytes);
+			result=expandRle(data,dataSizeInBytes,&(image->pix),&imageSizeInBytes);
 			break;
 		case COMPRESS_RLE_UD: /* RLE Up to Down Compression Algorithm */
-			result=expandRle(data,dataSizeInBytes,image,imageSizeInBytes);
+			result=expandRle(data,dataSizeInBytes,&(image->pix),&imageSizeInBytes);
 			if (result==COMPRESS_RESULT_FATAL) return COMPRESS_RESULT_FATAL;
 			transposeImage(image,imageSizeInBytes);
 			break;
 		case COMPRESS_LZG_LR: /* LZ Groody Left to Right Compression Algorithm */
-			result=expandLzg(data,dataSizeInBytes,image,imageSizeInBytes);
+			result=expandLzg(data,dataSizeInBytes,&(image->pix),&imageSizeInBytes);
 			break;
 		case COMPRESS_LZG_UD: /* LZ Groody Up to Down Compression Algorithm */
-			result=expandLzg(data,dataSizeInBytes,image,imageSizeInBytes);
+			result=expandLzg(data,dataSizeInBytes,&(image->pix),&imageSizeInBytes);
 			if (result==COMPRESS_RESULT_FATAL) return COMPRESS_RESULT_FATAL;
 			transposeImage(image,imageSizeInBytes);
 			break;
 		default:
-			result=COMPRESS_RESULT_FATAL;
+			result=COMPRESS_RESULT_FATAL; /* unknown algorithm */
 			break;
 	}
 	return result; /* Ok */
@@ -162,24 +168,76 @@ int mCompressGraphic(unsigned char* *data,tImage* image, int* dataSizeInBytes) {
 
 	/* Initialize variables */
 	imageSizeInBytes=image->widthInBytes*image->height;
-	for (i=0;i<COMPRESS_WORKING_ALGORITHMS;i++) compressedSize[i]=imageSizeInBytes;
+	for (i=0;i<COMPRESS_WORKING_ALGORITHMS;i++) compressedSize[i]=imageSizeInBytes; /* TODO: remove this line */
 
 	/*
 		Perform all compressions
 	*/
 
-	/* COMPRESS_RAW */
-	compressed[COMPRESS_RAW]=getMemory(compressedSize[COMPRESS_RAW]);
-	memcpy(compressed[COMPRESS_RAW],image->pix,compressedSize[COMPRESS_RAW]);
 
-	/* COMPRESS_RLE_LR */
-	compressed[COMPRESS_RLE_LR]=getMemory((10*imageSizeInBytes+50)); /* This will reserve 10*(image size)+50 bytes, to allocate the compressed file */
-	compressRle(compressed[COMPRESS_RLE_LR],image,&(compressedSize[COMPRESS_RLE_LR]));
+	/* Forward compression */
+	
+	/* COMPRESS_RAW
+	 * The allocation size is the image size.
+	 * The algorithm is hardcoded.
+	 * There is no need to code a transposed version because
+	 * we have no compression to improve.
+	 */
+	compressed[COMPRESS_RAW]=getMemory(imageSizeInBytes);
+	compressedSize[COMPRESS_RAW]=imageSizeInBytes;
+	memcpy(compressed[COMPRESS_RAW],image->pix,imageSizeInBytes);
+
+	/* COMPRESS_RLE_LR
+	 * If all the uncompressed data has a big enthropy, there
+	 * will be a control byte for a block of 127 bytes.
+	 * The allocation size has a maximum value of the image
+	 * size plus a byte each 127 bytes.
+	 * This is accoted by 2*n+50
+	 */
+	compressed[COMPRESS_RLE_LR]=getMemory((2*imageSizeInBytes+50)); 
+	compressRle(
+		image->pix,imageSizeInBytes,
+		compressed[COMPRESS_RLE_LR],&(compressedSize[COMPRESS_RLE_LR])
+	);
+
+	/* COMPRESS_LZG_LR 
+	 * If all the uncompressed data has a big enthropy, there
+	 * will be a maskbyte for a block of 8 bytes.
+	 * The allocation size has a maximum value of the image
+	 * size plus a byte in 8.
+	 * Additionally, this compressor needs 1024 bytes extra
+	 * allocated.
+	 * This is accoted by 2*n+1050
+	 */
+	compressed[COMPRESS_LZG_LR]=getMemory((2*imageSizeInBytes+1050));
+	compressLzg(
+		image->pix,imageSizeInBytes,
+		compressed[COMPRESS_LZG_LR],&(compressedSize[COMPRESS_LZG_LR])
+	);
+	
+
+	/* Transposed compression
+	 * Transposition is used to test the same compression
+	 * algorithms with other input in order to get a better
+	 * compression.
+	 * The following algorithms will be the same as above, but
+	 * using the image matrix transposed.
+	 */
+	antiTransposeImage(image,imageSizeInBytes);
 
 	/* COMPRESS_RLE_UD */
-	compressed[COMPRESS_RLE_UD]=getMemory(10*imageSizeInBytes+50); /* This will reserve 10*(image size)+50 bytes, to allocate the compressed file */
-	antiTransposeImage(image,imageSizeInBytes);
-	compressRle(compressed[COMPRESS_RLE_UD],image,&(compressedSize[COMPRESS_RLE_UD]));
+	compressed[COMPRESS_RLE_UD]=getMemory(2*imageSizeInBytes+50);
+	compressRle(
+		image->pix,imageSizeInBytes,
+		compressed[COMPRESS_RLE_UD],&(compressedSize[COMPRESS_RLE_UD])
+	);
+
+	/* COMPRESS_LZG_UD */
+	compressed[COMPRESS_LZG_UD]=getMemory(2*imageSizeInBytes+1050);
+	compressLzg(
+		image->pix,imageSizeInBytes,
+		compressed[COMPRESS_LZG_UD],&(compressedSize[COMPRESS_LZG_UD])
+	);
 
 	/*
 		Process results
diff --git a/PR/src/lib/object/image/image2.c b/PR/src/lib/object/image/image2.c
index 00be94a..74c09fb 100644
--- a/PR/src/lib/object/image/image2.c
+++ b/PR/src/lib/object/image/image2.c
@@ -47,14 +47,17 @@ compress.c: Princed Resources : Image Compression Library
 |                Internal compression prototypes                |
 \***************************************************************/
 
+/* compress and sets the bytes */
 void compressLzg(const unsigned char* input, int inputSize, 
                  unsigned char* output, int *outputSize);
 void compressRle(const unsigned char* input, int inputSize, 
                  unsigned char* output, int *outputSize);
+
+/* uncompress and allocates output */
 int expandLzg(const unsigned char* input, int inputSize, 
-               unsigned char* output, int *outputSize);
+               unsigned char** output, int *outputSize);
 int expandRle(const unsigned char* input, int inputSize, 
-               unsigned char* output, int *outputSize);
+               unsigned char** output, int *outputSize);
 
 /***************************************************************\
 |                        Image transpose                        |
@@ -67,18 +70,20 @@ int transpose(int x,int w,int h) {
 
 void transposeImage(tImage* image,int size) {
 	unsigned char* outputaux=getMemory(size);
-	int cursor=0;
+	int cursor;
 
-	while (cursor<size) {outputaux[transpose(cursor,image->widthInBytes,image->height)]=image->pix[cursor];cursor++;}
+	for (cursor=0;cursor<size;cursor++) 
+		outputaux[transpose(cursor,image->widthInBytes,image->height)]=image->pix[cursor];
 	free(image->pix);
 	image->pix=outputaux;
 }
 
 void antiTransposeImage(tImage* image,int size) {
 	unsigned char* outputaux=getMemory(size);
-	int cursor=0;
+	int cursor;
 
-	while (cursor<size) {outputaux[cursor]=image->pix[transpose(cursor,image->widthInBytes,image->height)];cursor++;}
+	for (cursor=0;cursor<size;cursor++) 
+		outputaux[cursor]=image->pix[transpose(cursor,image->widthInBytes,image->height)];
 	free(image->pix);
 	image->pix=outputaux;
 }
@@ -124,28 +129,29 @@ int mExpandGraphic(const unsigned char* data,tImage *image, int dataSizeInBytes)
 
 	switch (getAlgor(image->type)) {
 		case COMPRESS_RAW: /* No Compression Algorithm */
+						/* TODO: use dataSize */
 			if ((image->pix=getMemory(imageSizeInBytes))==NULL) return COMPRESS_RESULT_FATAL;
 			memcpy(image->pix,data,imageSizeInBytes);
 			result=COMPRESS_RESULT_SUCCESS;
 			break;
 		case COMPRESS_RLE_LR: /* RLE Left to Right Compression Algorithm */
-			result=expandRle(data,dataSizeInBytes,image,imageSizeInBytes);
+			result=expandRle(data,dataSizeInBytes,&(image->pix),&imageSizeInBytes);
 			break;
 		case COMPRESS_RLE_UD: /* RLE Up to Down Compression Algorithm */
-			result=expandRle(data,dataSizeInBytes,image,imageSizeInBytes);
+			result=expandRle(data,dataSizeInBytes,&(image->pix),&imageSizeInBytes);
 			if (result==COMPRESS_RESULT_FATAL) return COMPRESS_RESULT_FATAL;
 			transposeImage(image,imageSizeInBytes);
 			break;
 		case COMPRESS_LZG_LR: /* LZ Groody Left to Right Compression Algorithm */
-			result=expandLzg(data,dataSizeInBytes,image,imageSizeInBytes);
+			result=expandLzg(data,dataSizeInBytes,&(image->pix),&imageSizeInBytes);
 			break;
 		case COMPRESS_LZG_UD: /* LZ Groody Up to Down Compression Algorithm */
-			result=expandLzg(data,dataSizeInBytes,image,imageSizeInBytes);
+			result=expandLzg(data,dataSizeInBytes,&(image->pix),&imageSizeInBytes);
 			if (result==COMPRESS_RESULT_FATAL) return COMPRESS_RESULT_FATAL;
 			transposeImage(image,imageSizeInBytes);
 			break;
 		default:
-			result=COMPRESS_RESULT_FATAL;
+			result=COMPRESS_RESULT_FATAL; /* unknown algorithm */
 			break;
 	}
 	return result; /* Ok */
@@ -162,24 +168,76 @@ int mCompressGraphic(unsigned char* *data,tImage* image, int* dataSizeInBytes) {
 
 	/* Initialize variables */
 	imageSizeInBytes=image->widthInBytes*image->height;
-	for (i=0;i<COMPRESS_WORKING_ALGORITHMS;i++) compressedSize[i]=imageSizeInBytes;
+	for (i=0;i<COMPRESS_WORKING_ALGORITHMS;i++) compressedSize[i]=imageSizeInBytes; /* TODO: remove this line */
 
 	/*
 		Perform all compressions
 	*/
 
-	/* COMPRESS_RAW */
-	compressed[COMPRESS_RAW]=getMemory(compressedSize[COMPRESS_RAW]);
-	memcpy(compressed[COMPRESS_RAW],image->pix,compressedSize[COMPRESS_RAW]);
 
-	/* COMPRESS_RLE_LR */
-	compressed[COMPRESS_RLE_LR]=getMemory((10*imageSizeInBytes+50)); /* This will reserve 10*(image size)+50 bytes, to allocate the compressed file */
-	compressRle(compressed[COMPRESS_RLE_LR],image,&(compressedSize[COMPRESS_RLE_LR]));
+	/* Forward compression */
+	
+	/* COMPRESS_RAW
+	 * The allocation size is the image size.
+	 * The algorithm is hardcoded.
+	 * There is no need to code a transposed version because
+	 * we have no compression to improve.
+	 */
+	compressed[COMPRESS_RAW]=getMemory(imageSizeInBytes);
+	compressedSize[COMPRESS_RAW]=imageSizeInBytes;
+	memcpy(compressed[COMPRESS_RAW],image->pix,imageSizeInBytes);
+
+	/* COMPRESS_RLE_LR
+	 * If all the uncompressed data has a big enthropy, there
+	 * will be a control byte for a block of 127 bytes.
+	 * The allocation size has a maximum value of the image
+	 * size plus a byte each 127 bytes.
+	 * This is accoted by 2*n+50
+	 */
+	compressed[COMPRESS_RLE_LR]=getMemory((2*imageSizeInBytes+50)); 
+	compressRle(
+		image->pix,imageSizeInBytes,
+		compressed[COMPRESS_RLE_LR],&(compressedSize[COMPRESS_RLE_LR])
+	);
+
+	/* COMPRESS_LZG_LR 
+	 * If all the uncompressed data has a big enthropy, there
+	 * will be a maskbyte for a block of 8 bytes.
+	 * The allocation size has a maximum value of the image
+	 * size plus a byte in 8.
+	 * Additionally, this compressor needs 1024 bytes extra
+	 * allocated.
+	 * This is accoted by 2*n+1050
+	 */
+	compressed[COMPRESS_LZG_LR]=getMemory((2*imageSizeInBytes+1050));
+	compressLzg(
+		image->pix,imageSizeInBytes,
+		compressed[COMPRESS_LZG_LR],&(compressedSize[COMPRESS_LZG_LR])
+	);
+	
+
+	/* Transposed compression
+	 * Transposition is used to test the same compression
+	 * algorithms with other input in order to get a better
+	 * compression.
+	 * The following algorithms will be the same as above, but
+	 * using the image matrix transposed.
+	 */
+	antiTransposeImage(image,imageSizeInBytes);
 
 	/* COMPRESS_RLE_UD */
-	compressed[COMPRESS_RLE_UD]=getMemory(10*imageSizeInBytes+50); /* This will reserve 10*(image size)+50 bytes, to allocate the compressed file */
-	antiTransposeImage(image,imageSizeInBytes);
-	compressRle(compressed[COMPRESS_RLE_UD],image,&(compressedSize[COMPRESS_RLE_UD]));
+	compressed[COMPRESS_RLE_UD]=getMemory(2*imageSizeInBytes+50);
+	compressRle(
+		image->pix,imageSizeInBytes,
+		compressed[COMPRESS_RLE_UD],&(compressedSize[COMPRESS_RLE_UD])
+	);
+
+	/* COMPRESS_LZG_UD */
+	compressed[COMPRESS_LZG_UD]=getMemory(2*imageSizeInBytes+1050);
+	compressLzg(
+		image->pix,imageSizeInBytes,
+		compressed[COMPRESS_LZG_UD],&(compressedSize[COMPRESS_LZG_UD])
+	);
 
 	/*
 		Process results
diff --git a/PR/src/lib/object/image/image256.c b/PR/src/lib/object/image/image256.c
index 00be94a..74c09fb 100644
--- a/PR/src/lib/object/image/image256.c
+++ b/PR/src/lib/object/image/image256.c
@@ -47,14 +47,17 @@ compress.c: Princed Resources : Image Compression Library
 |                Internal compression prototypes                |
 \***************************************************************/
 
+/* compress and sets the bytes */
 void compressLzg(const unsigned char* input, int inputSize, 
                  unsigned char* output, int *outputSize);
 void compressRle(const unsigned char* input, int inputSize, 
                  unsigned char* output, int *outputSize);
+
+/* uncompress and allocates output */
 int expandLzg(const unsigned char* input, int inputSize, 
-               unsigned char* output, int *outputSize);
+               unsigned char** output, int *outputSize);
 int expandRle(const unsigned char* input, int inputSize, 
-               unsigned char* output, int *outputSize);
+               unsigned char** output, int *outputSize);
 
 /***************************************************************\
 |                        Image transpose                        |
@@ -67,18 +70,20 @@ int transpose(int x,int w,int h) {
 
 void transposeImage(tImage* image,int size) {
 	unsigned char* outputaux=getMemory(size);
-	int cursor=0;
+	int cursor;
 
-	while (cursor<size) {outputaux[transpose(cursor,image->widthInBytes,image->height)]=image->pix[cursor];cursor++;}
+	for (cursor=0;cursor<size;cursor++) 
+		outputaux[transpose(cursor,image->widthInBytes,image->height)]=image->pix[cursor];
 	free(image->pix);
 	image->pix=outputaux;
 }
 
 void antiTransposeImage(tImage* image,int size) {
 	unsigned char* outputaux=getMemory(size);
-	int cursor=0;
+	int cursor;
 
-	while (cursor<size) {outputaux[cursor]=image->pix[transpose(cursor,image->widthInBytes,image->height)];cursor++;}
+	for (cursor=0;cursor<size;cursor++) 
+		outputaux[cursor]=image->pix[transpose(cursor,image->widthInBytes,image->height)];
 	free(image->pix);
 	image->pix=outputaux;
 }
@@ -124,28 +129,29 @@ int mExpandGraphic(const unsigned char* data,tImage *image, int dataSizeInBytes)
 
 	switch (getAlgor(image->type)) {
 		case COMPRESS_RAW: /* No Compression Algorithm */
+						/* TODO: use dataSize */
 			if ((image->pix=getMemory(imageSizeInBytes))==NULL) return COMPRESS_RESULT_FATAL;
 			memcpy(image->pix,data,imageSizeInBytes);
 			result=COMPRESS_RESULT_SUCCESS;
 			break;
 		case COMPRESS_RLE_LR: /* RLE Left to Right Compression Algorithm */
-			result=expandRle(data,dataSizeInBytes,image,imageSizeInBytes);
+			result=expandRle(data,dataSizeInBytes,&(image->pix),&imageSizeInBytes);
 			break;
 		case COMPRESS_RLE_UD: /* RLE Up to Down Compression Algorithm */
-			result=expandRle(data,dataSizeInBytes,image,imageSizeInBytes);
+			result=expandRle(data,dataSizeInBytes,&(image->pix),&imageSizeInBytes);
 			if (result==COMPRESS_RESULT_FATAL) return COMPRESS_RESULT_FATAL;
 			transposeImage(image,imageSizeInBytes);
 			break;
 		case COMPRESS_LZG_LR: /* LZ Groody Left to Right Compression Algorithm */
-			result=expandLzg(data,dataSizeInBytes,image,imageSizeInBytes);
+			result=expandLzg(data,dataSizeInBytes,&(image->pix),&imageSizeInBytes);
 			break;
 		case COMPRESS_LZG_UD: /* LZ Groody Up to Down Compression Algorithm */
-			result=expandLzg(data,dataSizeInBytes,image,imageSizeInBytes);
+			result=expandLzg(data,dataSizeInBytes,&(image->pix),&imageSizeInBytes);
 			if (result==COMPRESS_RESULT_FATAL) return COMPRESS_RESULT_FATAL;
 			transposeImage(image,imageSizeInBytes);
 			break;
 		default:
-			result=COMPRESS_RESULT_FATAL;
+			result=COMPRESS_RESULT_FATAL; /* unknown algorithm */
 			break;
 	}
 	return result; /* Ok */
@@ -162,24 +168,76 @@ int mCompressGraphic(unsigned char* *data,tImage* image, int* dataSizeInBytes) {
 
 	/* Initialize variables */
 	imageSizeInBytes=image->widthInBytes*image->height;
-	for (i=0;i<COMPRESS_WORKING_ALGORITHMS;i++) compressedSize[i]=imageSizeInBytes;
+	for (i=0;i<COMPRESS_WORKING_ALGORITHMS;i++) compressedSize[i]=imageSizeInBytes; /* TODO: remove this line */
 
 	/*
 		Perform all compressions
 	*/
 
-	/* COMPRESS_RAW */
-	compressed[COMPRESS_RAW]=getMemory(compressedSize[COMPRESS_RAW]);
-	memcpy(compressed[COMPRESS_RAW],image->pix,compressedSize[COMPRESS_RAW]);
 
-	/* COMPRESS_RLE_LR */
-	compressed[COMPRESS_RLE_LR]=getMemory((10*imageSizeInBytes+50)); /* This will reserve 10*(image size)+50 bytes, to allocate the compressed file */
-	compressRle(compressed[COMPRESS_RLE_LR],image,&(compressedSize[COMPRESS_RLE_LR]));
+	/* Forward compression */
+	
+	/* COMPRESS_RAW
+	 * The allocation size is the image size.
+	 * The algorithm is hardcoded.
+	 * There is no need to code a transposed version because
+	 * we have no compression to improve.
+	 */
+	compressed[COMPRESS_RAW]=getMemory(imageSizeInBytes);
+	compressedSize[COMPRESS_RAW]=imageSizeInBytes;
+	memcpy(compressed[COMPRESS_RAW],image->pix,imageSizeInBytes);
+
+	/* COMPRESS_RLE_LR
+	 * If all the uncompressed data has a big enthropy, there
+	 * will be a control byte for a block of 127 bytes.
+	 * The allocation size has a maximum value of the image
+	 * size plus a byte each 127 bytes.
+	 * This is accoted by 2*n+50
+	 */
+	compressed[COMPRESS_RLE_LR]=getMemory((2*imageSizeInBytes+50)); 
+	compressRle(
+		image->pix,imageSizeInBytes,
+		compressed[COMPRESS_RLE_LR],&(compressedSize[COMPRESS_RLE_LR])
+	);
+
+	/* COMPRESS_LZG_LR 
+	 * If all the uncompressed data has a big enthropy, there
+	 * will be a maskbyte for a block of 8 bytes.
+	 * The allocation size has a maximum value of the image
+	 * size plus a byte in 8.
+	 * Additionally, this compressor needs 1024 bytes extra
+	 * allocated.
+	 * This is accoted by 2*n+1050
+	 */
+	compressed[COMPRESS_LZG_LR]=getMemory((2*imageSizeInBytes+1050));
+	compressLzg(
+		image->pix,imageSizeInBytes,
+		compressed[COMPRESS_LZG_LR],&(compressedSize[COMPRESS_LZG_LR])
+	);
+	
+
+	/* Transposed compression
+	 * Transposition is used to test the same compression
+	 * algorithms with other input in order to get a better
+	 * compression.
+	 * The following algorithms will be the same as above, but
+	 * using the image matrix transposed.
+	 */
+	antiTransposeImage(image,imageSizeInBytes);
 
 	/* COMPRESS_RLE_UD */
-	compressed[COMPRESS_RLE_UD]=getMemory(10*imageSizeInBytes+50); /* This will reserve 10*(image size)+50 bytes, to allocate the compressed file */
-	antiTransposeImage(image,imageSizeInBytes);
-	compressRle(compressed[COMPRESS_RLE_UD],image,&(compressedSize[COMPRESS_RLE_UD]));
+	compressed[COMPRESS_RLE_UD]=getMemory(2*imageSizeInBytes+50);
+	compressRle(
+		image->pix,imageSizeInBytes,
+		compressed[COMPRESS_RLE_UD],&(compressedSize[COMPRESS_RLE_UD])
+	);
+
+	/* COMPRESS_LZG_UD */
+	compressed[COMPRESS_LZG_UD]=getMemory(2*imageSizeInBytes+1050);
+	compressLzg(
+		image->pix,imageSizeInBytes,
+		compressed[COMPRESS_LZG_UD],&(compressedSize[COMPRESS_LZG_UD])
+	);
 
 	/*
 		Process results
diff --git a/PR/src/lib/object/image/image_common.c b/PR/src/lib/object/image/image_common.c
index 00be94a..74c09fb 100644
--- a/PR/src/lib/object/image/image_common.c
+++ b/PR/src/lib/object/image/image_common.c
@@ -47,14 +47,17 @@ compress.c: Princed Resources : Image Compression Library
 |                Internal compression prototypes                |
 \***************************************************************/
 
+/* compress and sets the bytes */
 void compressLzg(const unsigned char* input, int inputSize, 
                  unsigned char* output, int *outputSize);
 void compressRle(const unsigned char* input, int inputSize, 
                  unsigned char* output, int *outputSize);
+
+/* uncompress and allocates output */
 int expandLzg(const unsigned char* input, int inputSize, 
-               unsigned char* output, int *outputSize);
+               unsigned char** output, int *outputSize);
 int expandRle(const unsigned char* input, int inputSize, 
-               unsigned char* output, int *outputSize);
+               unsigned char** output, int *outputSize);
 
 /***************************************************************\
 |                        Image transpose                        |
@@ -67,18 +70,20 @@ int transpose(int x,int w,int h) {
 
 void transposeImage(tImage* image,int size) {
 	unsigned char* outputaux=getMemory(size);
-	int cursor=0;
+	int cursor;
 
-	while (cursor<size) {outputaux[transpose(cursor,image->widthInBytes,image->height)]=image->pix[cursor];cursor++;}
+	for (cursor=0;cursor<size;cursor++) 
+		outputaux[transpose(cursor,image->widthInBytes,image->height)]=image->pix[cursor];
 	free(image->pix);
 	image->pix=outputaux;
 }
 
 void antiTransposeImage(tImage* image,int size) {
 	unsigned char* outputaux=getMemory(size);
-	int cursor=0;
+	int cursor;
 
-	while (cursor<size) {outputaux[cursor]=image->pix[transpose(cursor,image->widthInBytes,image->height)];cursor++;}
+	for (cursor=0;cursor<size;cursor++) 
+		outputaux[cursor]=image->pix[transpose(cursor,image->widthInBytes,image->height)];
 	free(image->pix);
 	image->pix=outputaux;
 }
@@ -124,28 +129,29 @@ int mExpandGraphic(const unsigned char* data,tImage *image, int dataSizeInBytes)
 
 	switch (getAlgor(image->type)) {
 		case COMPRESS_RAW: /* No Compression Algorithm */
+						/* TODO: use dataSize */
 			if ((image->pix=getMemory(imageSizeInBytes))==NULL) return COMPRESS_RESULT_FATAL;
 			memcpy(image->pix,data,imageSizeInBytes);
 			result=COMPRESS_RESULT_SUCCESS;
 			break;
 		case COMPRESS_RLE_LR: /* RLE Left to Right Compression Algorithm */
-			result=expandRle(data,dataSizeInBytes,image,imageSizeInBytes);
+			result=expandRle(data,dataSizeInBytes,&(image->pix),&imageSizeInBytes);
 			break;
 		case COMPRESS_RLE_UD: /* RLE Up to Down Compression Algorithm */
-			result=expandRle(data,dataSizeInBytes,image,imageSizeInBytes);
+			result=expandRle(data,dataSizeInBytes,&(image->pix),&imageSizeInBytes);
 			if (result==COMPRESS_RESULT_FATAL) return COMPRESS_RESULT_FATAL;
 			transposeImage(image,imageSizeInBytes);
 			break;
 		case COMPRESS_LZG_LR: /* LZ Groody Left to Right Compression Algorithm */
-			result=expandLzg(data,dataSizeInBytes,image,imageSizeInBytes);
+			result=expandLzg(data,dataSizeInBytes,&(image->pix),&imageSizeInBytes);
 			break;
 		case COMPRESS_LZG_UD: /* LZ Groody Up to Down Compression Algorithm */
-			result=expandLzg(data,dataSizeInBytes,image,imageSizeInBytes);
+			result=expandLzg(data,dataSizeInBytes,&(image->pix),&imageSizeInBytes);
 			if (result==COMPRESS_RESULT_FATAL) return COMPRESS_RESULT_FATAL;
 			transposeImage(image,imageSizeInBytes);
 			break;
 		default:
-			result=COMPRESS_RESULT_FATAL;
+			result=COMPRESS_RESULT_FATAL; /* unknown algorithm */
 			break;
 	}
 	return result; /* Ok */
@@ -162,24 +168,76 @@ int mCompressGraphic(unsigned char* *data,tImage* image, int* dataSizeInBytes) {
 
 	/* Initialize variables */
 	imageSizeInBytes=image->widthInBytes*image->height;
-	for (i=0;i<COMPRESS_WORKING_ALGORITHMS;i++) compressedSize[i]=imageSizeInBytes;
+	for (i=0;i<COMPRESS_WORKING_ALGORITHMS;i++) compressedSize[i]=imageSizeInBytes; /* TODO: remove this line */
 
 	/*
 		Perform all compressions
 	*/
 
-	/* COMPRESS_RAW */
-	compressed[COMPRESS_RAW]=getMemory(compressedSize[COMPRESS_RAW]);
-	memcpy(compressed[COMPRESS_RAW],image->pix,compressedSize[COMPRESS_RAW]);
 
-	/* COMPRESS_RLE_LR */
-	compressed[COMPRESS_RLE_LR]=getMemory((10*imageSizeInBytes+50)); /* This will reserve 10*(image size)+50 bytes, to allocate the compressed file */
-	compressRle(compressed[COMPRESS_RLE_LR],image,&(compressedSize[COMPRESS_RLE_LR]));
+	/* Forward compression */
+	
+	/* COMPRESS_RAW
+	 * The allocation size is the image size.
+	 * The algorithm is hardcoded.
+	 * There is no need to code a transposed version because
+	 * we have no compression to improve.
+	 */
+	compressed[COMPRESS_RAW]=getMemory(imageSizeInBytes);
+	compressedSize[COMPRESS_RAW]=imageSizeInBytes;
+	memcpy(compressed[COMPRESS_RAW],image->pix,imageSizeInBytes);
+
+	/* COMPRESS_RLE_LR
+	 * If all the uncompressed data has a big enthropy, there
+	 * will be a control byte for a block of 127 bytes.
+	 * The allocation size has a maximum value of the image
+	 * size plus a byte each 127 bytes.
+	 * This is accoted by 2*n+50
+	 */
+	compressed[COMPRESS_RLE_LR]=getMemory((2*imageSizeInBytes+50)); 
+	compressRle(
+		image->pix,imageSizeInBytes,
+		compressed[COMPRESS_RLE_LR],&(compressedSize[COMPRESS_RLE_LR])
+	);
+
+	/* COMPRESS_LZG_LR 
+	 * If all the uncompressed data has a big enthropy, there
+	 * will be a maskbyte for a block of 8 bytes.
+	 * The allocation size has a maximum value of the image
+	 * size plus a byte in 8.
+	 * Additionally, this compressor needs 1024 bytes extra
+	 * allocated.
+	 * This is accoted by 2*n+1050
+	 */
+	compressed[COMPRESS_LZG_LR]=getMemory((2*imageSizeInBytes+1050));
+	compressLzg(
+		image->pix,imageSizeInBytes,
+		compressed[COMPRESS_LZG_LR],&(compressedSize[COMPRESS_LZG_LR])
+	);
+	
+
+	/* Transposed compression
+	 * Transposition is used to test the same compression
+	 * algorithms with other input in order to get a better
+	 * compression.
+	 * The following algorithms will be the same as above, but
+	 * using the image matrix transposed.
+	 */
+	antiTransposeImage(image,imageSizeInBytes);
 
 	/* COMPRESS_RLE_UD */
-	compressed[COMPRESS_RLE_UD]=getMemory(10*imageSizeInBytes+50); /* This will reserve 10*(image size)+50 bytes, to allocate the compressed file */
-	antiTransposeImage(image,imageSizeInBytes);
-	compressRle(compressed[COMPRESS_RLE_UD],image,&(compressedSize[COMPRESS_RLE_UD]));
+	compressed[COMPRESS_RLE_UD]=getMemory(2*imageSizeInBytes+50);
+	compressRle(
+		image->pix,imageSizeInBytes,
+		compressed[COMPRESS_RLE_UD],&(compressedSize[COMPRESS_RLE_UD])
+	);
+
+	/* COMPRESS_LZG_UD */
+	compressed[COMPRESS_LZG_UD]=getMemory(2*imageSizeInBytes+1050);
+	compressLzg(
+		image->pix,imageSizeInBytes,
+		compressed[COMPRESS_LZG_UD],&(compressedSize[COMPRESS_LZG_UD])
+	);
 
 	/*
 		Process results