Subversion Repositories HomeAutomation

Rev

Rev 684 | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. /*
  2. File: bmp2lcd.c
  3. Function: converts 128*64 black and white (1 bit per pixel) bitmap images to .lcd binary files
  4. the bytes in .lcd file are arranged to match the paging scheme used by KS0108 or HD61202 controlled
  5. graphic LCDs.
  6. Author: Dincer Aydin (dinceraydin@altavista.net)
  7. Modified by Martin Nordén to output as array
  8. Please note that this software is presented as is,
  9.  without any warranty of any kind. Use it at you own risk !!  */
  10. #include <stdlib.h>
  11. #include <math.h>
  12. #include <stdio.h>
  13. typedef unsigned char byte;
  14. #define SIZEBMP 1086
  15. #define SIZELCD 1024
  16. static int i;
  17. void convert(void);
  18. //byte getvertByte(int ,int bit);
  19.  unsigned char getbits(unsigned x, int p, int n);
  20.   byte count, arrayLCD[SIZELCD], arrayBmp[SIZEBMP];
  21.  main(int argc, char *argv[])
  22. {    
  23.     FILE *fp;
  24.     if(argc > 3)
  25.     {
  26.         printf("too many argumets...\n");
  27.         printf("usage: bmp2lcd fileName.bmp fileName.lcd\n");
  28.         return (0);
  29.     }
  30.     else if(argc < 3)
  31.     {
  32.         printf("missing argumets...\n");
  33.         printf("usage: bmp2lcd fileName.bmp fileName.lcd\n");
  34.         return (0);
  35.     }        
  36.  
  37.   /* open the bitmap for reading in binary mode  */
  38.  
  39.      if ( (fp = fopen(*++argv, "rb")) == NULL)
  40.      {
  41.          fprintf(stderr, "Error opening file.");
  42.          exit(1);
  43.      }
  44.  
  45.      /* Read the bitmap into arrayBmp[]. */
  46.  
  47.      if (fread(arrayBmp, sizeof( byte), SIZEBMP, fp) != SIZEBMP)
  48.      {
  49.          fprintf(stderr, "Error reading file.");
  50.          exit(1);
  51.      }
  52.  
  53.      fclose(fp);
  54. /*-------------------------------------------------------------*/
  55. /*-----between goes the conversion---------------------------*/
  56.  
  57.     convert(); 
  58. /*-------------------------------------------------------------*/
  59.  
  60.  /* Open a binary mode file. */
  61.  
  62.     if ( (fp = fopen(*++argv, "wb")) == NULL)
  63.     {
  64.         fprintf(stderr, "Error opening file.");
  65.         exit(1);
  66.     }
  67.     /* Save arrayLCD[] to the file. */
  68.  
  69.     //if (fwrite(arrayLCD, sizeof( byte), SIZELCD, fp) != SIZELCD)
  70.     //{
  71.     //    fprintf(stderr, "Error writing to file.");
  72.     //    exit(1);
  73.     //}
  74.     fprintf(fp,"static unsigned char PictArray[] PROGMEM = {\n");
  75.     for (i=0; i<SIZELCD-1;i++)
  76.     {
  77.         fprintf(fp,"0x%x,",arrayLCD[i]);
  78.         if ((i+1)%128 == 0){
  79.             fprintf(fp,"//Line %0x \n",(i+1)/128);
  80.         }
  81.     }
  82.         fprintf(fp,"0x%x//Line 7 \n};",arrayLCD[SIZELCD]);
  83.    
  84.    
  85.     fclose(fp);
  86.     return(0);
  87. }
  88.  
  89. void convert(void)
  90. {
  91.     byte i,j,k,bmpCol,page,bitPos,dByte = 0;
  92.     byte oneBlock[8];  
  93.     for(page=0;page<8;page++)
  94.     {
  95.         for(bmpCol=0;bmpCol<16;bmpCol++)
  96.         {
  97.             for(i=0;i<8;i++)
  98.                 oneBlock[i] = arrayBmp[SIZEBMP -1 - i*16 - page*16*8 - bmpCol];
  99.             for(bitPos=0;bitPos<8;bitPos++)
  100.             {  
  101.                 dByte = 0;
  102.                 for(i=0;i<8;i++)
  103.                 {  
  104.                     if(getbits(oneBlock[i], bitPos, 1))
  105.                     dByte += (byte)pow((double)2, (double)i);  
  106.                 }  
  107.                 arrayLCD[(page * 128) + 127 - (bmpCol * 8) - bitPos]= ~dByte;
  108.             }
  109.         }
  110.     }
  111. }
  112.  
  113. /* getbits:  get n bits from position p */
  114.  unsigned char getbits(unsigned x, int p, int n)
  115.    {
  116.        return (x >> (p+1-n)) & ~(~0 << n);
  117.    }