summaryrefslogtreecommitdiffstats
path: root/logotool/compress.c
blob: 327d50ea1f308a8f307d0aa5b1eaf8f596d65e81 (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
/*
 * Copyright (C) 2007
 *       pancake <pancake@youterm.com>
 *       esteve <esteve@eslack.org>
 *
 * logotool 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 2 of the License, or
 * (at your option) any later version.
 *
 * logotool 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 logotool; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#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 compress_image(char *srcf, char *dstf, int w, int h)
{
	char buf[256];
	char tmp[256];
	int fd, fdout;
	int i,ret;

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

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

	// TODO: force little endian!! (not safe for big endian)
	write(fdout, &w, 4);
	write(fdout, &h, 4);

	for(;;) {
		unsigned char c;
		ret = read(fd, buf, 3*80);
		c = (int)((2*ret/3)>>1);
		write(fdout,&c, 1);
		if (ret <= 0) break;
		for(i=0;i<ret;i+=3) {
			tmp[0]  = buf[i+0]&0xf8;
			tmp[0] |= buf[i+1]&0x07;
			tmp[1]  = buf[i+1]&0xe0;
			tmp[1] |= buf[i+1]&0x1f;
			write(fdout, tmp, 2);
		}
	}
	close(fd);
	close(fdout);
	printf("eval PATH=$PWD:$PATH logotool -u %s\n", dstf);
	return 1;
}