summaryrefslogtreecommitdiffstats
path: root/logotool/uncompress.c
blob: 7fb8b10e4309f239c4726efcf3ce6630636f2b03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
 *  logotool - tool to modify logo images with simple compression
 *  Copyright (C) 2007
 *       pancake <pancake@youterm.com>
 *       esteve <esteve@eslack.org>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <getopt.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int uncompress_image(char *srcf, char *dstf)
{
	int fd;
	int fout;
	int width,height;
	int fsize;
	char* src;
	char* dst;
	char* src2;
	char* eof;
	char *deof;
	char* dst2 = NULL;
	unsigned char byte;

	fd    = open( srcf, O_RDONLY );

	if (fd == -1) {
		printf("Cannot open file '%s'\n", srcf);
		return 1;
	}

	fout  = open( dstf, O_CREAT | O_WRONLY , 0660 );

	if (fout == -1) {
		printf("Cannot open file '%s' for writing\n", dstf);
		return 1;
	}

	fprintf(stderr, "Input file: %s\n", srcf);
	fprintf(stderr, "Output file: %s\n", dstf);
	fsize = lseek ( fd, 0,SEEK_END);
	fsize = lseek ( fd, 0,SEEK_CUR);
	lseek ( fd,0, SEEK_SET);

	fsize -= 8;

	read ( fd, &width, 4 );
	read ( fd, &height, 4 );

	fprintf(stderr, "Width: %d\nHeight: %d\n",width, height);
	fprintf(stderr, "Input Size: %d\n",fsize);
	src = malloc ( fsize );
	dst = malloc ( width*height*2 );
	deof = dst+width*height*2;
	if (src == (void *)-1 || dst == (void *)-1 ) {
		printf("Cannot malloc\n");
		return 1;
	}

	memset(dst,'\0', width*height*2);

	read(fd, src, fsize);
	eof = src+fsize;

	for(dst2 = dst, src2 = src; src2<eof;) {
		//byte = src2[0] & 0xff;
		//src2 = src2 + 1;
		byte=(*src2++)&0xff;
		// [1xxxyyyy] [char] [char]
		// copies xxxyyyy times two chars
		if (byte > 0x7f) {
			int i,j = byte&0x7f;
			for(i=0; i++<j; dst2+=2) 
				memcpy(dst2, src2, 2);
			src2+=2;
		} else {	
			if ( byte != 0x00 ) {
				byte<<=1;
			//	fprintf(stderr, "COPYING: %d %d\n", byte, (eof-src2));
				if ((dst2+byte >= deof || (src2+byte >= eof))) {
					fprintf(stderr, "Break\n");
					break;
				} 
				memcpy(dst2, src2, byte);
				src2+=byte; dst2+=byte;
			}
		}
	}
	fprintf(stderr, "Output Size: %lld\n",(long long int)(dst2-dst));

	if ((dst2-dst)!=(width*height*2))
		fprintf(stderr, "failed?\n");

	write ( fout, dst,  width*height*2);

	close(fout);
	close(fd);

	//printf("./logotool -w %d -h %d -v %s || \n", width, height, dstf);
	//printf("logotool -w %d -h %d -v %s\n", width, height, dstf);
	printf("eval PATH=$PWD:$PATH logotool -w %d -h %d -m %s\n", width, height, dstf);

	return 0;
}