Bitmap to Array with Sd Card adapter

Hey guys.
This is my first entry here.

I tried to convert a bitmap to an array. if i only put the red values to the array it works perfectly but if i put the blue and green value to ananther array nothing happens or there are only false values!
Please help.

test_bmp.ino (1.91 KB)

Which Arduino are you using ? How big is the image, height and width ? Why are the R, G and B arrays of type int ?

The code, so no-one else has to download it.

#include <SD.h>
#include <SPI.h>


void setup()
{

    Serial.begin(9600);
    while (!Serial);

    Serial.print("Initializing SD card...");
    if (!SD.begin(53)) {
        Serial.println("initialization failed!");
        while (1); // <- this is how you should block execution, not with returns
    }
    Serial.println("initialization done.");


    // Open
    File bmpImage = SD.open("picture.bmp", FILE_READ);
    textFile = SD.open("test.txt", FILE_WRITE);

    int32_t dataStartingOffset = readNbytesInt(&bmpImage, 0x0A, 4);

    // Change their types to int32_t (4byte)
    int32_t width = readNbytesInt(&bmpImage, 0x12, 4);
    int32_t height = readNbytesInt(&bmpImage, 0x16, 4);
    Serial.println(width);
    Serial.println(height);

    int pixelsize = readNbytesInt(&bmpImage, 0x1C, 2);

    if (pixelsize != 24)
    {
        Serial.println("Image is not 24 bpp");
        while (1);
    }

    //skip bitmap header
     bmpImage.seek(dataStartingOffset);
    // 24bpp means you have three bytes per pixel, usually B G R

    byte RED, GREEN, BLUE;
    int R[height][width];
    int G[height][width];
    int B[height][width];

    for(int i = 0; i < height; i ++) {
      byte a;
      if(i>=3)
        {
          a = bmpImage.read();
        }

     for (int j = 0; j < width; j ++) {

            BLUE = bmpImage.read();
            GREEN = bmpImage.read();
            RED = bmpImage.read();

            R[height-1-i][j] = RED;
            G[height-1-i][j] = GREEN;
            B[height-1-i][j] = BLUE;
        }
        a = bmpImage.read();
        a = bmpImage.read();
    }
    bmpImage.close();
}

void loop(){}

int32_t readNbytesInt(File *p_file, int position, byte nBytes)
{
    if (nBytes > 4)
        return 0;

    p_file->seek(position);

    int32_t weight = 1;
    int32_t result = 0;
    for (; nBytes; nBytes--)
    {
        result += weight * p_file->read();
        weight <<= 8;
    }
    return result;
}

I am using the arduino Mega 2560,

the height ist 48 and the width is 71 pixel R, G & B are Integer because I later i am working with the entrys as RGB values

    int R[height][width];
    int G[height][width];
    int B[height][width];

the height ist 48 and the width is 71 pixel

And there you have your answer

The code, so no-one else has to download it.

It would help if the OP's code compiled without error, which makes me wonder if it is the right code anyway

Initializing SD card…initial⸮

this is the output I get. with the code I attached…

but if I do

byte RED, GREEN, BLUE;
int R[height][width];
// int G[height][width];
// int B[height][width];

for(int i = 0; i < height; i ++) {
byte a;
if(i>=3)
{
a = bmpImage.read();
}

for (int j = 0; j < width; j ++) {

BLUE = bmpImage.read();
GREEN = bmpImage.read();
RED = bmpImage.read();

R[height-1-i][j] = RED;
// G[height-1-i][j] = GREEN;
// B[height-1-i][j] = BLUE;
}
a = bmpImage.read();
a = bmpImage.read();
}

then every thing works fine.
I don´t understand it

test_bmp.ino (2.1 KB)

How much memory is available when compiled with and without those lines commented out ?

without commented out 4% is used of programming memory and 11% of dynamik memory with commented out its exactly the same

 int R[height][width]

Do the arithmetic.

Get back to us.

Please remember to use code tags when posting code.