* Enhacements and fixups for logotool

* Add support for RGBA raw images (New flag -a)
* Autodetect RGBA images
* Fix endianness stuff when writting the logo header
This commit is contained in:
pancake committed 2007-05-05 03:04:52 +02:00
1 parent d0f9d78335
commit ed37b5ee62
2 files changed
+94 -17

No files matched your search

+88 -14
View File
@@ -28,45 +28,119 @@
#include <sys/stat.h>
#include <fcntl.h>
extern int rgba;
int isbe()
{
int a=1; char *b=(char *)&a;
return (int)b[0];
}
void ememcpy(char *dst, char *src)
{
char buffer[4];
memcpy(buffer, src, 4);
dst[0] = buffer[3]; dst[1] = buffer[2];
dst[2] = buffer[1]; dst[3] = buffer[0];
}
int compress_image(char *srcf, char *dstf, int w, int h)
{
char buf[256];
char tmp[256];
int fd, fdout;
int fdout;
FILE *fd;
int i,ret;
fd = open(srcf, O_RDONLY);
if (fd == -1) {
fd = fopen(srcf, "rb");
if (fd == NULL) {
printf("Cannot open file '%s' \n", srcf);
return 1;
}
fdout = open( dstf, O_CREAT | O_WRONLY , 0660 );
fdout = open( dstf, O_TRUNC | 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)
if (isbe()) {
char oop[4];
ememcpy((char *)&oop, (char *)&w);
write(fdout, &oop, 4);
ememcpy((char *)&oop, (char *)&h);
write(fdout, &oop, 4);
} else {
write(fdout, &w, 4);
write(fdout, &h, 4);
}
fseek(fd, 0, SEEK_END);
if (!rgba && ftell(fd)%3) {
fprintf(stderr, "Oops..looks like RGBA..forcing -a\n");
rgba = 1;
}
fseek(fd, 0, SEEK_SET);
#define MAX 3*80
if (rgba) {
for(;;) {
unsigned char c;
ret = read(fd, buf, 3*80);
c = (int)((2*ret/3)>>1);
write(fdout,&c, 1);
ret = fread(&buf, 1, MAX, fd);
if (ret <= 0) break;
c = ret/4;
write(fdout,&c, 1);
for(i=0;i<ret;i+=4) {
tmp[0] = buf[i+0]&0xf8;
tmp[0] |= buf[i+1]&0x07;
tmp[1] = buf[i+1]&0xe0;
tmp[1] |= buf[i+2]&0x1f;
write(fdout, tmp, 2);
}
}
} else {
for(;;) {
unsigned char c;
ret = fread(buf, 1, MAX, fd);
if (ret <= 0) break;
c = ret/3;
write(fdout,&c, 1);
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+2]&0x1f;
write(fdout, tmp, 2);
}
}
}
fclose(fd);
close(fdout);
printf("eval PATH=$PWD:$PATH logotool -u %s\n", dstf);
return 1;
}
#if 0
// TODO: compression stuff
/* is a repeated block? */
c = buf[0];
for(i=1;i<MAX&&buf[i]==c&&buf[i+1]==c;i+=2);
printf("memcpy %d -> %d (ret %d)\n",i,ret-i, ret);
for(i=0;i<ret-i;i++)
printf(" %02x ", (unsigned char)buf[i]);
printf("\n");
if (i>5) {
// if (i==MAX) {
if (0x80>ret) printf("PRON\n");
c = 0x80 | ret; //(int)((2*ret/3)>>1);
write(fdout,&c, 1);
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);
// restore state
memcpy(buf,buf+i,ret-i);
ret -= i;
}
}
close(fd);
close(fdout);
printf("eval PATH=$PWD:$PATH logotool -u %s\n", dstf);
return 1;
}
#endif
+6 -3
View File
@@ -33,6 +33,7 @@
int uncompress_image(char *srcf, char *dstf);
int compress_image(char *srcf, char *dstf, int w, int h);
int rgb2yuv(char *from, char *to, int width, int height);
int rgba = 0;
static int show_usage()
{
@@ -43,6 +44,7 @@ static int show_usage()
printf(" -v [img] view uncompressed image using the 'display'\n");
printf(" command from ImageMagick (in monochrome)\n");
printf(" -w specify width (required for -v and -c)\n");
printf(" -a force RGBA instead of RGB when compressing (use with -c)\n");
printf(" -h specify height (required for -v and -c)\n");
printf(" -V version number\n");
printf("\n");
@@ -60,12 +62,13 @@ int main ( int argc , char** argv )
char *uimg = NULL;
char *view = NULL;
while((c = getopt(argc, argv, "w:h:Vv:u:c:m:")) != -1) {
while((c = getopt(argc, argv, "w:h:Vv:u:c:m:a")) != -1) {
switch(c) {
case 'V': printf("%s\n", VERSION); return 0;
case 'u': uimg = optarg; break;
case 'c': cimg = optarg; break;
case 'm': mimg = optarg; break;
case 'a': rgba = 1; break;
case 'w': w = atoi(optarg); break;
case 'h': h = atoi(optarg); break;
case 'v': view = optarg; break;
@@ -83,7 +86,7 @@ int main ( int argc , char** argv )
sprintf(buf, "%s.rgb", mimg);
if ( rgb2yuv(mimg, buf, w,h ) ) {
sprintf(buf, "echo pause | mplayer %s.rgb -slave -demuxer rawvideo -rawvideo fps=1:w=416:h=70", mimg);
sprintf(buf, "echo pause | mplayer %s.rgb -slave -demuxer rawvideo -rawvideo fps=1:w=%d:h=%d", mimg, w,h);
system(buf);
} else {
printf("Oops\n");
@@ -116,7 +119,7 @@ int main ( int argc , char** argv )
printf("You must specify width and height with '-w' and '-h'.\n");
return 1;
}
dst = (char*)malloc(strlen(cimg)+5);
dst = (char*)malloc(strlen(cimg)+6);
strcpy(dst, cimg);
strcat(dst, ".logo");
compress_image(cimg, dst, w, h);