This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

How to load an external file

I want to read a bmp file to my program, how can I do that?
Can I read it directly?

Parents
  • "Presumably this is just a byte-by-byte representation of the .BMP file's contents?"

    Correct

    "Before this could be used by the embedded device, the appropriate information would need to be extracted from the .BMP file and converted to a suitable form for the embedded device?"

    I suppose that depends on the embedded application. For example, one application's BMP rendering implementation might be flexible and would use the parameters contained in the BITMAPINFO structure, while another application's implementation might be designed to only support a fixed format with parameters established by a priori agreement. The latter case would require preprocessing the BMP, but seems more reasonable for applications where an 8051 would be used.

    The nifty thing about 010 Editor is that all you need to do is define a template (e.g., from the likes of file format descriptions on wotsit.org) describing the file's structure in a very C-like way and then you can write very C-like scripts to programmatically manipulate those files. The contents of SweetScape's BMP template below, which only defines how 010 Editor should display a file, might give you some idea how easy it would be to use built-in I/O functions (having similar C-like syntax) to perform custom file manipulation or data extraction.

    Of course one could just write a little C program to do it too ;-)

    //-----------------------------------
    //--- 010 Editor v1.1 Binary Template
    //
    // File:    BMPTemplate.bt
    // Author:  SweetScape Software
    // Purpose: Defines a template for
    //    parsing BMP files.
    //-----------------------------------
    
    // Define structures used in BMP files
    
    typedef struct {   // bmfh
        CHAR    bfType[2];
        DWORD   bfSize;
        WORD    bfReserved1;
        WORD    bfReserved2;
        DWORD   bfOffBits;
    } BITMAPFILEHEADER;
    
    typedef struct {    // bmih
        DWORD   biSize;
        LONG    biWidth;
        LONG    biHeight;
        WORD    biPlanes;
        WORD    biBitCount;
        DWORD   biCompression;
        DWORD   biSizeImage;
        LONG    biXPelsPerMeter;
        LONG    biYPelsPerMeter;
        DWORD   biClrUsed;
        DWORD   biClrImportant;
    } BITMAPINFOHEADER;
    
    typedef struct {   // rgbq
        UBYTE   rgbBlue;
        UBYTE   rgbGreen;
        UBYTE   rgbRed;
        UBYTE   rgbReserved;
    } RGBQUAD;
    
    typedef struct {   // rgbt
        UBYTE   rgbBlue;
        UBYTE   rgbGreen;
        UBYTE   rgbRed;
    } RGBTRIPLE;
    
    //---------------------------------------------
    
    // Define the headers
    LittleEndian();
    SetBackColor( cLtGray );
    BITMAPFILEHEADER bmfh;
    BITMAPINFOHEADER bmih;
    
    // Check for header
    if( bmfh.bfType != "BM" )
    {
        Warning( "File is not a bitmap. Template stopped." );
        return -1;
    }
    
    // Define the color table
    if( bmih.biBitCount != 24 )
    {
        SetBackColor( cLtAqua );
        if( bmih.biClrUsed > 0 )
            RGBQUAD aColors[ bmih.biClrUsed ];
        else
            RGBQUAD aColors[ 1 << bmih.biBitCount ];
    }
    
    // Define the bytes of the data
    SetBackColor( cNone );
    if( bmih.biCompression > 0 )
    {
        // Bytes are compressed
        if( bmih.biSizeImage > 0 )
            UBYTE rleData[ bmih.biSizeImage ];
        else
            UBYTE rleData[ bmfh.bfSize - FTell() ];
    }
    else
    {
        // Calculate bytes per line and padding required
        local int bytesPerLine = (int)Ceil( bmih.biWidth * bmih.biBitCount / 8.0 );
        local int padding      = 4 - (bytesPerLine % 4);
        if( padding == 4 )
            padding = 0;
    
        // Define each line of the image
        struct BITMAPLINE {
    
            // Define color data
            if( bmih.biBitCount < 8 )
                 UBYTE     imageData[ bytesPerLine ];
            else if( bmih.biBitCount == 8 )
                 UBYTE     colorIndex[ bmih.biWidth ];
            else if( bmih.biBitCount == 24 )
                 RGBTRIPLE colors[ bmih.biWidth ];
    
            // Pad if necessary
            if( padding != 0 )
                 UBYTE padBytes[ padding ];
    
        } lines[ bmih.biHeight ];
    }

Reply
  • "Presumably this is just a byte-by-byte representation of the .BMP file's contents?"

    Correct

    "Before this could be used by the embedded device, the appropriate information would need to be extracted from the .BMP file and converted to a suitable form for the embedded device?"

    I suppose that depends on the embedded application. For example, one application's BMP rendering implementation might be flexible and would use the parameters contained in the BITMAPINFO structure, while another application's implementation might be designed to only support a fixed format with parameters established by a priori agreement. The latter case would require preprocessing the BMP, but seems more reasonable for applications where an 8051 would be used.

    The nifty thing about 010 Editor is that all you need to do is define a template (e.g., from the likes of file format descriptions on wotsit.org) describing the file's structure in a very C-like way and then you can write very C-like scripts to programmatically manipulate those files. The contents of SweetScape's BMP template below, which only defines how 010 Editor should display a file, might give you some idea how easy it would be to use built-in I/O functions (having similar C-like syntax) to perform custom file manipulation or data extraction.

    Of course one could just write a little C program to do it too ;-)

    //-----------------------------------
    //--- 010 Editor v1.1 Binary Template
    //
    // File:    BMPTemplate.bt
    // Author:  SweetScape Software
    // Purpose: Defines a template for
    //    parsing BMP files.
    //-----------------------------------
    
    // Define structures used in BMP files
    
    typedef struct {   // bmfh
        CHAR    bfType[2];
        DWORD   bfSize;
        WORD    bfReserved1;
        WORD    bfReserved2;
        DWORD   bfOffBits;
    } BITMAPFILEHEADER;
    
    typedef struct {    // bmih
        DWORD   biSize;
        LONG    biWidth;
        LONG    biHeight;
        WORD    biPlanes;
        WORD    biBitCount;
        DWORD   biCompression;
        DWORD   biSizeImage;
        LONG    biXPelsPerMeter;
        LONG    biYPelsPerMeter;
        DWORD   biClrUsed;
        DWORD   biClrImportant;
    } BITMAPINFOHEADER;
    
    typedef struct {   // rgbq
        UBYTE   rgbBlue;
        UBYTE   rgbGreen;
        UBYTE   rgbRed;
        UBYTE   rgbReserved;
    } RGBQUAD;
    
    typedef struct {   // rgbt
        UBYTE   rgbBlue;
        UBYTE   rgbGreen;
        UBYTE   rgbRed;
    } RGBTRIPLE;
    
    //---------------------------------------------
    
    // Define the headers
    LittleEndian();
    SetBackColor( cLtGray );
    BITMAPFILEHEADER bmfh;
    BITMAPINFOHEADER bmih;
    
    // Check for header
    if( bmfh.bfType != "BM" )
    {
        Warning( "File is not a bitmap. Template stopped." );
        return -1;
    }
    
    // Define the color table
    if( bmih.biBitCount != 24 )
    {
        SetBackColor( cLtAqua );
        if( bmih.biClrUsed > 0 )
            RGBQUAD aColors[ bmih.biClrUsed ];
        else
            RGBQUAD aColors[ 1 << bmih.biBitCount ];
    }
    
    // Define the bytes of the data
    SetBackColor( cNone );
    if( bmih.biCompression > 0 )
    {
        // Bytes are compressed
        if( bmih.biSizeImage > 0 )
            UBYTE rleData[ bmih.biSizeImage ];
        else
            UBYTE rleData[ bmfh.bfSize - FTell() ];
    }
    else
    {
        // Calculate bytes per line and padding required
        local int bytesPerLine = (int)Ceil( bmih.biWidth * bmih.biBitCount / 8.0 );
        local int padding      = 4 - (bytesPerLine % 4);
        if( padding == 4 )
            padding = 0;
    
        // Define each line of the image
        struct BITMAPLINE {
    
            // Define color data
            if( bmih.biBitCount < 8 )
                 UBYTE     imageData[ bytesPerLine ];
            else if( bmih.biBitCount == 8 )
                 UBYTE     colorIndex[ bmih.biWidth ];
            else if( bmih.biBitCount == 24 )
                 RGBTRIPLE colors[ bmih.biWidth ];
    
            // Pad if necessary
            if( padding != 0 )
                 UBYTE padBytes[ padding ];
    
        } lines[ bmih.biHeight ];
    }

Children
No data