2013年11月13日 星期三

HEX轉成BMP檔

也是J-Link取得的HEX資料,轉成影像格式,用來檢查資料。

#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <windows.h>

#define  SIZE                (0x1000000)
#define  WIDTH                (672)
#define  WIDTH_ALIG            ((-(WIDTH))&0x3)
#define  HEIGHT                (6144)
#define  BMP_OFFSET         0x436
#define  BMP_SIZE           ((WIDTH+WIDTH_ALIG)*HEIGHT+BMP_OFFSET)

unsigned char data[SIZE];
unsigned char image[BMP_SIZE];
FILE *src;
FILE *dest;

unsigned int  *p_color;
unsigned char *p_data;
unsigned char *p_src;
BITMAPFILEHEADER    *p_FileHeader;
BITMAPINFOHEADER    *p_InfoHeader;
// unsigned char d0,d1;

unsigned char B2bin(unsigned char *x)
{
    int t;
    int i;
    unsigned short r;

    r = 0;
    for ( i=0; i<2; i++ )
    {
        if ( *x <= '9' )
        {
            t = (*x-'0');
        }
        else
        {
            t = (*x-'A'+0xA);
        }
        r = (r<<4) + t;
        x++;
    }
    return r;
}

unsigned short W2bin(unsigned char *x)
{
    int t;
    int i;
    unsigned int r;

    r = 0;
    for ( i=0; i<4; i++ )
    {
        if ( *x <= '9' )
        {
            t = (*x-'0');
        }
        else
        {
            t = (*x-'A'+0xA);
        }
        r = (r<<4) + t;
        x++;
    }
    return r;
}

void main(void)
{
    int  i,j,n;
    char temp[80];
    int  byte_count;
    unsigned short address;
    int  record_type;
    int  flag;
    unsigned int img_address;

    p_FileHeader = (BITMAPFILEHEADER*)&image[0];
    p_InfoHeader = (BITMAPINFOHEADER*)&image[0xE];
    p_color    = (unsigned int *)&image[0x36];
    p_data     = &image[BMP_OFFSET];

    p_FileHeader->bfType = 'B' | ('M'<<8);
    p_FileHeader->bfSize = WIDTH*HEIGHT+BMP_OFFSET;
    p_FileHeader->bfOffBits = BMP_OFFSET;

    p_InfoHeader->biSize   = 40;
    p_InfoHeader->biWidth  = WIDTH;
    p_InfoHeader->biHeight = HEIGHT;
    p_InfoHeader->biPlanes = 1;
    p_InfoHeader->biBitCount = 8;
    p_InfoHeader->biCompression = 0;
    p_InfoHeader->biSizeImage = (WIDTH+WIDTH_ALIG)*HEIGHT;
    p_InfoHeader->biXPelsPerMeter = 0;
    p_InfoHeader->biYPelsPerMeter = 0;
    p_InfoHeader->biClrUsed = 0;
    p_InfoHeader->biClrImportant = 0;

    for (i=0; i<0x100; i++)
    {
        p_color[i] = (i<<16) | (i<<8) | i;    // set gray pattern
    }

    src = fopen("memory.hex", "rb");
    fread(data,1,SIZE,src);
    fclose(src);
    p_src = &data[0];
    temp[0] = 0;
    flag = 1;
    while ( flag )
    {
        sscanf(p_src,"%s",temp);
        n = strlen(temp);
        if ( n == 0 )
        {
            break;
        }
//        printf("%s\n",temp);
        if (':' != temp[0]) continue;
        byte_count = B2bin(&temp[1]);
        address    = W2bin(&temp[3]);
        record_type= B2bin(&temp[7]);
        switch ( record_type )
        {
        case 0:  // data record
            for( j=0; j<byte_count; j++ )
            {
                p_data[img_address+address+j] = B2bin(&temp[9+j*2]);
            }
            break;
        case 1:  // end
            flag = 0;
            break;
        case 4:  // relocate
            img_address = ((W2bin(&temp[9]) & 0xfff)<<16);
            break;
        default:
            break;
        }
        // reset var.
        p_src += (n+2);
        temp[0] = 0;
    }

    dest = fopen("CIS.bmp", "wb");
    fwrite(image, 1, BMP_SIZE, dest);
    fclose(dest);
}

1 則留言:

  1. 你好,看到你常貼程式碼,有個小建議,要不要考慮放到類似 gist.github.com 上,它有兩個好用的功能:1. 可以有版本控制。2. 能使用"Embed this gist"下方的 script 嵌入到blogspot 的html中,會有彩色的語法。另外近期也在研究STM32 cortex-M 的程式,可以多多交流。

    回覆刪除