summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpancake <pancake@dazo>2007-04-30 06:26:27 +0200
committerpancake <pancake@dazo>2007-04-30 06:26:27 +0200
commit3e13254858c69e76c86ba19aa9cb6e24454b7555 (patch)
treee9540001959cfb76d5eb05f1276048ae41a7ea15
parent5e3de1ba068f9f83265458e326e46177737d6abe (diff)
download0xFFFF-3e13254858c69e76c86ba19aa9cb6e24454b7555.tar.bz2
* Initial import of the logotool utility
-rw-r--r--logotool/Makefile8
-rw-r--r--logotool/README17
-rw-r--r--logotool/compress.c72
-rw-r--r--logotool/logotool.c127
-rw-r--r--logotool/rgb2yuv.c112
-rw-r--r--logotool/uncompress.c124
6 files changed, 460 insertions, 0 deletions
diff --git a/logotool/Makefile b/logotool/Makefile
new file mode 100644
index 0000000..22e6a44
--- /dev/null
+++ b/logotool/Makefile
@@ -0,0 +1,8 @@
+OBJ=logotool.o compress.o uncompress.o rgb2yuv.o
+CFLAGS=-Wall -g
+
+all: ${OBJ}
+ ${CC} ${OBJ} -o logotool
+
+clean:
+ rm -f ${OBJ} logotool
diff --git a/logotool/README b/logotool/README
new file mode 100644
index 0000000..b872d3a
--- /dev/null
+++ b/logotool/README
@@ -0,0 +1,17 @@
+Compressing/Uncompressing images
+================================
+
+To compress your image type:
+
+ $ gimp -> create an image and save as raw rgb (24 bits)
+ $ logotool -w 300 -h 200 -c MyImage.rgb
+
+This command will create MyImage.rgb.logo
+
+You can check if the image is fine typing:
+
+ $ logotool -u MyImage.rgb.logo
+
+To see the image you'll need mplayer and type this:
+
+ $ `logotool -u MyImage.rgb.logo`
diff --git a/logotool/compress.c b/logotool/compress.c
new file mode 100644
index 0000000..327d50e
--- /dev/null
+++ b/logotool/compress.c
@@ -0,0 +1,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;
+}
diff --git a/logotool/logotool.c b/logotool/logotool.c
new file mode 100644
index 0000000..b81e407
--- /dev/null
+++ b/logotool/logotool.c
@@ -0,0 +1,127 @@
+/*
+ * 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>
+
+#define VERSION "0.1"
+
+int uncompress_image(char *srcf, char *dstf);
+int compress_image(char *srcf, char *dstf, int w, int h);
+
+static int show_usage()
+{
+ printf("Usage: logotool [-flags]\n");
+ printf(" -u [img] uncompress image to img.rgb\n");
+ printf(" -c [img] compres a 24 bit rgb raw image to a 16(565) one\n");
+ printf(" -m [img] show image using mplayer (dumps to <img>.yuv)\n");
+ 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(" -h specify height (required for -v and -c)\n");
+ printf(" -V version number\n");
+ printf("\n");
+ printf("Example:\n");
+ printf(" $ `logotool -u logo-file` # uncompress and display a logo\n");
+ return 1;
+}
+
+int main ( int argc , char** argv )
+{
+ int c;
+ int w=0,h=0;
+ char *mimg = NULL;
+ char *cimg = NULL;
+ char *uimg = NULL;
+ char *view = NULL;
+
+ while((c = getopt(argc, argv, "w:h:Vv:u:c:m:")) != -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 'w': w = atoi(optarg); break;
+ case 'h': h = atoi(optarg); break;
+ case 'v': view = optarg; break;
+ }
+ }
+
+ if (mimg) {
+ char *buf;
+ if (w==0||h==0) {
+ printf("You must specify width and height with '-w' and '-h'.\n");
+ return 1;
+ }
+
+ buf = (char *)malloc(strlen(mimg)+128);
+ 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);
+ system(buf);
+ } else {
+ printf("Oops\n");
+ }
+ free(buf);
+ } else
+ if (view) {
+ char *buf;
+
+ if (w==0||h==0) {
+ printf("You must specify width and height with '-w' and '-h'.\n");
+ return 1;
+ }
+ buf = (char *)malloc(strlen(view)+128);
+ sprintf(buf, "display -size %dx%d -depth 16 %s", w,h,view);
+ printf("%s\n", buf);
+ system(buf);
+ free(buf);
+ } else
+ if (uimg) {
+ char *dst = (char*)malloc(strlen(uimg)+5);
+ strcpy(dst, uimg);
+ strcat(dst, ".gray");
+ uncompress_image(uimg, dst);
+ free(dst);
+ } else
+ if (cimg) {
+ char *dst;
+ if (w==0||h==0) {
+ printf("You must specify width and height with '-w' and '-h'.\n");
+ return 1;
+ }
+ dst = (char*)malloc(strlen(cimg)+5);
+ strcpy(dst, cimg);
+ strcat(dst, ".logo");
+ compress_image(cimg, dst, w, h);
+ free(dst);
+ } else
+ show_usage();
+
+ return 0;
+}
diff --git a/logotool/rgb2yuv.c b/logotool/rgb2yuv.c
new file mode 100644
index 0000000..cb95cf9
--- /dev/null
+++ b/logotool/rgb2yuv.c
@@ -0,0 +1,112 @@
+/*
+ * Copyright (C) 2007
+ * 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 <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+
+void RGBtoYUV(unsigned short in565, unsigned char *yuv)
+{
+ /* 2 bytes, 565
+ * RRRRR GGGGGG BBBBB
+ */
+
+ int i;
+ i = (in565>>8)&0xFF;
+ in565 = (in565<<8)&0xFF00;
+ in565 |= i;
+
+ int rgb[3];
+
+ rgb[0] = (in565>>11)&0x1F;
+ rgb[1] = (in565>>5)&0x3F;
+ rgb[2] = (in565)&0x1F;
+
+ rgb[0] = (rgb[0]<<3) | 0x7;
+ rgb[1] = (rgb[1]<<2) | 0x3;
+ rgb[2] = (rgb[2]<<3) | 0x7;
+
+ /*yuv[0] = 0.299*rgb[0] + 0.587*rgb[1] + 0.114*rgb[2];
+ yuv[1] = (rgb[2]-yuv[0])*0.565 + 128;
+ yuv[2] = (rgb[0]-yuv[0])*0.713 + 128;*/
+
+ yuv[0] = (0.257 * rgb[0]) + (0.504 * rgb[1]) + (0.098 * rgb[2]) + 16;
+ yuv[1] = 128 - (0.148 * rgb[0]) - (0.291 * rgb[1]) + (0.439 * rgb[2]);
+ yuv[2] = 128 + (0.439 * rgb[0]) - (0.368 * rgb[1]) - (0.071 * rgb[2]);
+}
+// mplayer caca.yuv -demuxer rawvideo -rawvideo fps=1:w=416:h=70 //:i420
+
+
+int rgb2yuv(char *from, char *to, int width, int height)
+{
+ int fd=open (from, O_RDONLY );
+ int fout= open ( to, O_CREAT| O_WRONLY , 0660 );
+ int fsize;
+ unsigned char* src;
+ unsigned char* dstY;
+ unsigned char* dstU;
+ unsigned char* dstV;
+ unsigned char YUV[3];
+ unsigned short inP;
+ int i,j;
+
+ if (fd == -1) {
+ printf("cannot open %s\n", from);
+ return 0;
+ }
+ if (fout == -1) {
+ printf("cannot open %s\n", to);
+ return 0;
+ }
+ fsize = lseek ( fd, 0,SEEK_END);
+ fsize = lseek ( fd, 0,SEEK_CUR);
+ lseek ( fd,0, SEEK_SET);
+
+ src=malloc ( fsize );
+ dstY=malloc ( (fsize/2) );
+ dstU=malloc ( (fsize/2) );
+ dstV=malloc ( (fsize/2) );
+
+
+ for ( i = 0 ; i < fsize; i+=2 ) {
+ read ( fd, &inP, 2 );
+ RGBtoYUV ( inP, YUV );
+ dstY[i>>1]=YUV[0];
+ dstU[i>>1]=YUV[1];
+ dstV[i>>1]=YUV[2];
+ }
+
+ for ( i = 0 ; i < (fsize/2); i++ )
+ write ( fout,&dstY[i], 1);
+
+ for ( i = 0 ; i < height ; i+=2 )
+ for ( j = 0; j < width ; j +=2 )
+ write ( fout,&dstU[j+(i*width)], 1);
+
+ for ( i = 0 ; i < height ; i+=2 )
+ for ( j = 0; j < width ; j +=2 )
+ write ( fout,&dstV[j+(i*width)], 1);
+ return 1;
+}
diff --git a/logotool/uncompress.c b/logotool/uncompress.c
new file mode 100644
index 0000000..9de905b
--- /dev/null
+++ b/logotool/uncompress.c
@@ -0,0 +1,124 @@
+/*
+ * 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 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 ((int)src == -1 || (int)dst == -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: %d\n",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;
+}