home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / bmp2tex1.zip / bmp2tex.c next >
C/C++ Source or Header  |  1994-09-04  |  6KB  |  218 lines

  1. /******************************************************************************
  2.   
  3.                            BMP2TEX
  4.  
  5.                a converter from BitMap to LaTeX
  6.  
  7.  
  8.  *  Copyright (c) 1994 Maurizio Giunti - All rights reserved  *
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions
  11. are met:
  12.  1. Redistributions of source code must retain the above copyright
  13.     notice, this list of conditions and the following disclaimer.
  14.  2. Redistributions in binary form must reproduce the above copyright
  15.     notice, this list of conditions and the following disclaimer in the
  16.     documentation and/or other materials provided with the distribution.
  17.  3. All advertising materials mentioning features or use of this software
  18.     must display the following acknowledgement:
  19.     "This product includes software developed by Maurizio Giunti"
  20.  
  21. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
  22. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  23. AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  24. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  27. OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  28. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR
  29. OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  30. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *******************************************************************************
  32. If you change this source to compile on a different environment
  33. please contact me:
  34.  
  35.                     Maurizio Giunti
  36.                     Via Foggini, 24
  37.                     I-50142 Firenze
  38.  
  39.                     Fidonet: 2:332/102.3
  40.                     Internet: giunti@stat.ds.unifi.it
  41. *******************************************************************************
  42. History:
  43. 04-09-1994 First release
  44.  
  45. *******************************************************************************
  46. Compiled on:
  47.     OS               BY                                 Note
  48.  OS/2 2.11      Maurizio Giunti                     First release
  49.  DOS            Maurizio Giunti
  50.  
  51. ******************************************************************************/
  52.  
  53.  
  54.  
  55. #include <stdio.h>
  56.  
  57. #define VER "1.00"
  58.  
  59.  
  60. /* Bitmap Header */
  61. typedef struct tagBITMAPFILEHEADER {    
  62.     char ID[2];
  63.     short fill1[8];
  64.     long  DimX;
  65.     long  DimY;
  66.     short fill2;
  67.     short Planes;
  68.     short fill3;
  69. } BITMAPH;
  70.  
  71.  
  72. /* Functions declaration */
  73. short getbit(FILE *f);
  74.  
  75.  
  76. /* Global data */
  77. static short count;
  78. static unsigned char byte;
  79. static long pos;
  80.  
  81. int main(int argc,char **argv)
  82. {
  83.  FILE *f;       /* Input file (BitMap)   */
  84.  FILE *out;     /* Output file           */
  85.  BITMAPH head;  /* Bitmap header         */
  86.  long x,y;      /* Line/column counters  */
  87.  long lx;       /* Line points max       */
  88.  long Pbuf;     /* Number of near points */
  89.  long Psx;      /* Start point           */
  90.  
  91.  
  92.  printf("\nBMP2TEX V%s - (C) 1994/95 Maurizio Giunti\n\n",VER);
  93.  
  94.  if(argc!=3)
  95.     {
  96.      puts("Usage:");
  97.      puts(" bmp2tex <bitmap> <texfile>");
  98.      puts("\nEx:\n bmp2tex dummy.bmp dummy.pic");
  99.      return 0;
  100.     }
  101.  
  102.  if((f=fopen(argv[1],"rb"))==NULL)
  103.     {
  104.      puts("BMP file not found!");
  105.      return 1;
  106.     }
  107.  
  108.  if((out=fopen(argv[2],"wt"))==NULL)
  109.     {
  110.      puts("Can't create destination file!");
  111.      fclose(f);
  112.      return 2;
  113.     }
  114.  
  115.  /* Bitmap header reading */
  116.  fread(&head,sizeof(BITMAPH),1,f);
  117.  
  118.  if(head.ID[0]!='B' || head.ID[1]!='M'  )
  119.     {
  120.      puts("This is not a bitmap!");
  121.      fclose(f);
  122.      return 3;
  123.     }
  124.  
  125.  printf("BMP: %ldx%ld - %ld planes\n",head.DimX,head.DimY,head.Planes);
  126.  
  127.  if(head.Planes!=1)
  128.     {
  129.      puts("BMP must have 1 bit plane!");
  130.      fclose(f);
  131.      return 4;
  132.     }
  133.  
  134.  if((out=fopen(argv[2],"wt"))==NULL)
  135.     {
  136.      puts("Can't create destination file!");
  137.      fclose(f);
  138.      return 5;
  139.     }
  140.  
  141.  /* Begin picture */
  142.  fprintf(out,"%%\n%% Created by BMP2TEX V%s - (C) 1994/95 Maurizio Giunti\n%%\n",VER);
  143.  fprintf(out,"\\begin{picture}(%ld,%ld)(0,-%ld)\n",head.DimX,head.DimY,head.DimY);
  144.  fprintf(out,"\\linethickness{\\unitlength}\n");
  145.  
  146.  /* Compute lx */
  147.  lx=head.DimX;
  148.  while((lx%32)!=0) lx++;
  149.  count=9;
  150.  pos=0;
  151.  Pbuf=0;
  152.  for(y=0;y<head.DimY;y++)
  153.     {
  154.      pos-=(lx/8);
  155.      printf("\rLine: %ld/%ld  \r",y+1,head.DimY);
  156.  
  157.  
  158.      /* Convertion cycle */
  159.      for(x=0;x<lx;x++)
  160.         {
  161.          short bit=getbit(f);
  162.          if(x>=head.DimX) continue;
  163.          if(bit==1)
  164.             {
  165.              if(Pbuf==0)
  166.                 {
  167.                  Psx=x;
  168.                 }
  169.              Pbuf ++;
  170.             }
  171.         else if(Pbuf>0)
  172.             {
  173.              fprintf(out,"\\put(%ld,-%ld){\\line(1,0){%ld}}\n",Psx,y,Pbuf);
  174.              Pbuf=0;
  175.             }
  176.  
  177.         }
  178.     if(Pbuf>0)
  179.             {
  180.              fprintf(out,"\\put(%ld,-%ld){\\line(1,0){%ld}}\n",Psx,y,Pbuf);
  181.              Pbuf=0;
  182.             }
  183.     pos-=(lx/8);
  184.     }
  185.  
  186.  
  187.  /* End picture */
  188.  fprintf(out,"\\end{picture}\n");
  189.  fprintf(out,"%% End.\n");
  190.  
  191.  fclose(out);
  192.  fclose(f);
  193.  printf("\n<Done!>\n");
  194.  return 0;
  195. }
  196.  
  197.  
  198. /* Get a single bit from the file */
  199. short getbit(FILE *f)
  200. {
  201.  const unsigned char pow[8]={128,64,32,16,8,4,2,1};
  202.  short bit;
  203.  if(count>=8)
  204.     {
  205.      fseek(f,pos,SEEK_END);
  206.      byte=getc(f);
  207.      count=0;
  208.      pos++;
  209.     }
  210.  bit= !((byte&pow[count])==pow[count]);
  211.  count ++;
  212.  return bit;
  213. }
  214.  
  215.  
  216.  
  217.  
  218.