Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8483793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:12:13+00:00 2026-06-10T20:12:13+00:00

I have a text file being saved by a matrix library containing a 2D

  • 0

I have a text file being saved by a matrix library containing a 2D matrix as such:

1 0 0 
6 0 4
0 1 1

Where each number is represented with a colored pixel. I am looking for some insight as to how I’d go about solving this problem. If any more information is required, do not hesitate to ask.

EDIT: Another approach I’ve tried is: fwrite(&intmatrix, size,1, bmp_ptr); where I pass in the matrix pointer, which does not seem to output a readable BMP file. The value of size is the rows*cols of course, and the type of matrix is arma::Mat<int> which is a matrix from the Armadillo Linear Algebra Library.

EDIT II: Reading this indicated that my size should probably be rows*cols*4 given the size of the rows if I am not mistaken, any guidance on this point as well would be great.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T20:12:15+00:00Added an answer on June 10, 2026 at 8:12 pm

    Here’s an app which generates a text file of random integers, reads them back, and writes them to disk as a (roughly square) 32-bit-per-pixel .BMP image.

    Note, I made a number of assumptions on things like the format of the original text file, the range of numbers, etc., but they are documented in the code. With this working example you should be able to tweak them easily, if necessary.

    // IntToBMP.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include <cstdint>
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <vector>
    #include <random>
    #include <ctime>
    #include <memory>
    
    #pragma pack( push, 1 ) 
    struct BMP
    {
        BMP();
        struct
        {
            uint16_t ID;
            uint32_t fileSizeInBytes;
            uint16_t reserved1;
            uint16_t reserved2;
            uint32_t pixelArrayOffsetInBytes;
        } FileHeader;
    
        enum class CompressionMethod : uint32_t {   BI_RGB              = 0x00, 
                                                    BI_RLE8             = 0x01,
                                                    BI_RLE4             = 0x02,
                                                    BI_BITFIELDS        = 0x03,
                                                    BI_JPEG             = 0x04,
                                                    BI_PNG              = 0x05,
                                                    BI_ALPHABITFIELDS   = 0x06 };
    
        struct
        {
            uint32_t headerSizeInBytes;
            uint32_t bitmapWidthInPixels;
            uint32_t bitmapHeightInPixels;
            uint16_t colorPlaneCount;
            uint16_t bitsPerPixel;
            CompressionMethod compressionMethod;
            uint32_t bitmapSizeInBytes;
            int32_t horizontalResolutionInPixelsPerMeter;
            int32_t verticalResolutionInPixelsPerMeter;
            uint32_t paletteColorCount;
            uint32_t importantColorCount;
        } DIBHeader;
    };
    #pragma pack( pop )
    
    BMP::BMP()
    {
        //Initialized fields
        FileHeader.ID                                   = 0x4d42; // == 'BM' (little-endian)
        FileHeader.reserved1                            = 0;
        FileHeader.reserved2                            = 0;
        FileHeader.pixelArrayOffsetInBytes              = sizeof( FileHeader ) + sizeof( DIBHeader );
        DIBHeader.headerSizeInBytes                     = 40;
        DIBHeader.colorPlaneCount                       = 1;
        DIBHeader.bitsPerPixel                          = 32;
        DIBHeader.compressionMethod                     = CompressionMethod::BI_RGB;
        DIBHeader.horizontalResolutionInPixelsPerMeter  = 2835; // == 72 ppi
        DIBHeader.verticalResolutionInPixelsPerMeter    = 2835; // == 72 ppi
        DIBHeader.paletteColorCount                     = 0;
        DIBHeader.importantColorCount                   = 0;
    }
    
    void Exit( void )
    {
        std::cout << "Press a key to exit...";
        std::getchar();
    
        exit( 0 );
    }
    
    void MakeIntegerFile( const std::string& integerFilename )
    {
        const uint32_t intCount = 1 << 20; //Generate 1M (2^20) integers
        std::unique_ptr< int32_t[] > buffer( new int32_t[ intCount ] ); 
    
        std::mt19937 rng;
        uint32_t rngSeed = static_cast< uint32_t >( time( NULL ) );
        rng.seed( rngSeed );
    
        std::uniform_int_distribution< int32_t > dist( INT32_MIN, INT32_MAX );
    
        for( size_t i = 0; i < intCount; ++i )
        {
            buffer[ i ] = dist( rng );
        }
    
        std::ofstream writeFile( integerFilename, std::ofstream::binary );
    
        if( !writeFile )
        {
            std::cout << "Error writing " << integerFilename << ".\n";
            Exit();
        }
    
        writeFile << buffer[ 0 ];
        for( size_t i = 1; i < intCount; ++i )
        {
            writeFile << " " << buffer[ i ];
        }
    }
    
    int _tmain(int argc, _TCHAR* argv[])  //Replace with int main( int argc, char* argv[] ) if you're not under Visual Studio
    {
        //Assumption: 32-bit signed integers
        //Assumption: Distribution of values range from INT32_MIN through INT32_MAX, inclusive
        //Assumption: number of integers contained in file are unknown
        //Assumption: source file of integers is a series of space-delimitied strings representing integers
        //Assumption: source file's contents are valid
        //Assumption: non-rectangular numbers of integers yield non-rectangular bitmaps (final scanline may be short)
        //            This may cause some .bmp parsers to fail; others may pad with 0's.  For simplicity, this implementation
        //            attempts to render square bitmaps.
    
        const std::string integerFilename = "integers.txt";
        const std::string bitmapFilename = "bitmap.bmp";
    
        std::cout << "Creating file of random integers...\n";
        MakeIntegerFile( integerFilename );
    
        std::vector< int32_t >integers; //If quantity of integers being read is known, reserve or resize vector or use array
    
        //Read integers from file
        std::cout << "Reading integers from file...\n";
        {   //Nested scope will release ifstream resource when no longer needed
            std::ifstream readFile( integerFilename );
    
            if( !readFile )
            {
                std::cout << "Error reading " << integerFilename << ".\n";
                Exit();
            }
    
            std::string number;
            while( readFile.good() )
            {
                std::getline( readFile, number, ' ' );
                integers.push_back( std::stoi( number ) );
            }
    
            if( integers.size() == 0 )
            {
                std::cout << "No integers read from " << integerFilename << ".\n";
                Exit();
            }
        }
    
        //Construct .bmp
        std::cout << "Constructing .BMP...\n";
        BMP bmp;
        size_t intCount = integers.size();
        bmp.DIBHeader.bitmapSizeInBytes = intCount * sizeof( integers[ 0 ] );
        bmp.FileHeader.fileSizeInBytes = bmp.FileHeader.pixelArrayOffsetInBytes + bmp.DIBHeader.bitmapSizeInBytes;
        bmp.DIBHeader.bitmapWidthInPixels = static_cast< uint32_t >( ceil( sqrt( intCount ) ) );
        bmp.DIBHeader.bitmapHeightInPixels = static_cast< uint32_t >( ceil( intCount / static_cast< float >( bmp.DIBHeader.bitmapWidthInPixels ) ) );
    
        //Write integers to .bmp file
        std::cout << "Writing .BMP...\n";
        {
            std::ofstream writeFile( bitmapFilename, std::ofstream::binary );
    
            if( !writeFile )
            {
                std::cout << "Error writing " << bitmapFilename << ".\n";
                Exit();
            }
    
            writeFile.write( reinterpret_cast< char * >( &bmp ), sizeof( bmp ) );
            writeFile.write( reinterpret_cast< char * >( &integers[ 0 ] ), bmp.DIBHeader.bitmapSizeInBytes );
        }
    
        //Exit
        Exit();
    } 
    

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I' ve got a problem with Haskell. I have text file looking like this:
I have text file with some stuff that i would like to put into
I have text file with something like first line line nr 2 line three
I have Text file that contains data separated with a comma , . How
I have text file, like FILED AS OF DATE: 20090209 DATE AS OF CHANGE:
I have text file with some text information and i need to split this
I have text file with entries like 123 112 3333 44 2 How to
I have a text file that I want to edit by rewriting it to
I have a text file and I would like to parse it using regular
I have a text file exported from a Foxpro (Dos-based) program, but this text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.