add ati tiff to bmp conv tool
authorhackbard <hackbard>
Thu, 9 Oct 2003 16:39:54 +0000 (16:39 +0000)
committerhackbard <hackbard>
Thu, 9 Oct 2003 16:39:54 +0000 (16:39 +0000)
conv_fg_tif.c [new file with mode: 0644]

diff --git a/conv_fg_tif.c b/conv_fg_tif.c
new file mode 100644 (file)
index 0000000..eb1205a
--- /dev/null
@@ -0,0 +1,235 @@
+/*
+ * ati framegrabber tif to bmp converter ;)
+ *
+ * achtung: totaler muell code, der hoffentlich seinen zweck erfuellt
+ *          fuer die netten tiff 16bit rgb bilder vom tem(?).
+ */
+
+#include <stdio.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>
+
+typedef struct s_pic_info {
+ int width;
+ int height;
+ int bps;
+ short bits[3]; /* max 3 values */
+ int rps; /* rows per strip */
+ int soc; /* strip offset counts */
+ int sbc; /* strip byte counts */
+ int *so; /* pointer to strip offsets */
+ int *sb; /* pointer to the byte counts per strip */
+} t_pic_info;
+
+static inline void _short2int(short *dst, unsigned char *buf) {
+       *dst=(buf[1]<<8)|buf[0];        
+}
+
+static inline void _word2int(int *dst, unsigned char *buf) {
+       *dst=(buf[3]<<24)|(buf[2]<<16)|(buf[1]<<8)|buf[0];
+}
+#define short2int(a,b) _short2int(&a,b)
+#define word2int(a,b) _word2int(&a,b)
+
+int parse_table(int fd,int offset,t_pic_info *pi) {
+ int i;
+ short de;
+ unsigned char buf[12]; /* 12 byte directory entry */
+
+ lseek(fd,offset,SEEK_SET);
+ read(fd,buf,2);
+ short2int(de,buf);
+
+ printf("parsing %d tags\n",de);
+
+ for(i=0;i<de;i++) {
+  unsigned char *obuf;
+  short tag;
+  short type;
+  int count;
+  int vo;
+  int j;
+
+  lseek(fd,offset+2+i*12,SEEK_SET);
+  read(fd,buf,12);
+  short2int(tag,buf);
+  short2int(type,buf+2);
+  word2int(count,buf+4);
+  word2int(vo,buf+8);
+
+  switch(tag) {
+   case 256:
+    pi->width=vo;
+    printf("width: %d\n",vo);
+    break;
+   case 257:
+    pi->height=vo;
+    printf("height: %d\n",vo);
+    break;
+   case 258:
+    pi->bps=count;
+    if(count!=1) {
+     printf("warning, only converting 16 bit rgb files!");
+     obuf=(unsigned char *)malloc(count*2);
+     lseek(fd,vo,SEEK_SET);
+     read(fd,obuf,2*count);
+     for(j=0;j<count;j++) short2int(pi->bits[j],obuf+2*j);
+     free(obuf);
+    }
+    else pi->bits[0]=vo;
+    printf("bits per sample: %d %d %d\n",pi->bits[0],pi->bits[1],pi->bits[2]);
+    break;
+   case 259:
+    if(vo!=1) puts("warning, compressed file - i am going to do bullshit!");
+    break;
+   case 262:
+    if(vo!=2) puts("warning, no rgb file - i am going to do bullshit!");
+    break;
+   case 273:
+    pi->soc=count;
+    printf("%d strip offsets\n",count);
+    obuf=(unsigned char *)malloc(count*4);
+    pi->so=(int *)malloc(count*4);
+    lseek(fd,vo,SEEK_SET);
+    read(fd,obuf,count*4);
+    for(j=0;j<count;j++) word2int(pi->so[j],obuf+4*j);
+    free(obuf);
+    break;
+   case 278:
+    pi->rps=vo;
+    printf("rows per strip: %d\n",vo);
+    break;
+   case 279:
+    pi->sbc=count;
+    printf("strip bytes: %d\n",count);
+    obuf=(unsigned char *)malloc(count*4);
+    pi->sb=(int *)malloc(count*4);
+    lseek(fd,vo,SEEK_SET);
+    read(fd,obuf,count*4);
+    for(j=0;j<count;j++) word2int(pi->sb[j],obuf+4*j);
+    free(obuf);
+    break;
+  }
+ }
+
+ return 1;
+}
+
+int main(int argc,char **argv) {
+ int tfd,fd,i,tmp=0;
+ int offset,size;
+ unsigned char *buf;
+ unsigned char tbuf[4];
+ t_pic_info pi;
+ /* for bmp */
+ int foo,k,l,bmps;
+ unsigned char bbuf[128];
+ short bla;
+
+ puts("");
+ puts("ati framegrabber tiff to bmp converter.");
+ puts("");
+ puts("no compressed files!");
+ puts("no 'big' or 'special' files! ;)");
+ puts("");
+
+ memset(&pi,0,sizeof(t_pic_info));
+
+ fd=open(argv[1],O_RDONLY);
+ tfd=open(argv[2],O_WRONLY|O_CREAT);
+
+ if((fd<0) || (tfd<0)) {
+  puts("error opening files ...");
+  return -1;
+ }
+
+ lseek(fd,0,SEEK_SET);
+ size=lseek(fd,0,SEEK_END);
+ lseek(fd,0,SEEK_SET);
+
+ lseek(fd,4,SEEK_SET);
+ read(fd,tbuf,4);
+ word2int(offset,tbuf);
+
+ printf("offset: %04x (%d)\n",offset,offset);
+
+ /* we only need first table! */
+ parse_table(fd,offset,&pi);
+
+ printf("\n");
+ printf("let's see what we have:\n");
+ puts("");
+ printf("we have %d strips, the offsets are:\n",pi.soc);
+ printf("1st: %04x, 2nd: %04x, ... last: %04x\n",pi.so[0],pi.so[1],pi.so[pi.soc-1]);
+ printf("their lenght in bytes is:\n");
+ printf("1st: %d ... last: %d\n",pi.sb[0],pi.sb[pi.sbc-1]);
+
+ printf("checking ...\n");
+ printf("we have %d pixels, each has %d bytes => %d bytes\n",pi.width*pi.height,pi.bps*pi.bits[0]/8,pi.width*pi.height*pi.bps*pi.bits[0]/8);
+ for(i=0;i<pi.sbc;i++) tmp+=pi.sb[i];
+ printf("the tiff provides %d bytes! ok? :)\n",tmp);
+
+ buf=(unsigned char *)malloc(size);
+
+ if(buf==NULL) {
+  puts("error allocating memory ...");
+  return -1;
+ }
+
+ puts("");
+ printf("writing bmp file %s...",argv[2]);
+
+ foo=3*pi.width;
+ bmps=(foo+(4-foo%4))*pi.height;
+
+ memset(bbuf,0,sizeof(bbuf));
+ bbuf[0]='B'; bbuf[1]='M';
+ bbuf[2]=(bmps+0x36)&0xff;
+ bbuf[3]=(bmps+0x36)>>8;
+ bbuf[10]=0x36;
+ bbuf[14]=0x28;
+ bbuf[18]=pi.width&0xff;
+ bbuf[19]=pi.width>>8;
+ bbuf[22]=pi.height&0xff;
+ bbuf[23]=pi.height>>8;
+ bbuf[26]=1;
+ bbuf[28]=24;
+ bbuf[34]=bmps&0xff;
+ bbuf[35]=bmps>>8;
+ bbuf[38]=0x12;
+ bbuf[39]=0x0b;
+ bbuf[42]=bbuf[38];
+ bbuf[43]=bbuf[39];
+ write(tfd,bbuf,54);
+ for(k=0;k<pi.height;k++) {
+  for(l=0;l<pi.width;l++) {
+   /* read rgb value (16bit) */
+   lseek(fd,pi.so[k/pi.rps]+2*l+2*(k%pi.rps)*pi.width,SEEK_SET);
+   read(fd,tbuf,pi.bits[0]/8);
+   short2int(bla,tbuf);
+   // bla=(tbuf[0]<<8)|tbuf[1];
+   bbuf[0]=(bla>>10)<<3;
+   bbuf[1]=((bla>>5)&31)<<3;
+   bbuf[2]=(bla&31)<<3;
+   write(tfd,bbuf,3);
+  }
+  if(foo%4) {
+   memset(bbuf,0,4-foo%4);
+   write(tfd,bbuf,4-foo%4);
+  }
+ }
+
+ printf("done!\n");
+ printf("now go and kill ati!!!1\n");
+
+ close(fd);
+ close(tfd);
+ free(buf);
+
+ return 1;
+}