How do I access elements of an array that is stored in program memory?

Hello,
I am trying to store the pixel data for a 106x49 image in an arduino nano by indexing the image colors into 237 unique colors and then referencing those colors in a pixel array. However, even after indexing, the array is still too long to store in RAM so I have to store it in progmem.

This is my code to just print each pixel color to the serial monitor (I used "..." to indicate more elements as I could not fit it all on here):

const long int clr[237] = {0x78A9B2, 0x58A3BA, 0xC69F7E....};
const byte pixel[106][49] PROGMEM = {{0, 1, 2, 3, 4, 5, 6, 7, 8, 2, 2, 1, 3, 2, 2, 2, 9, 2, 2, 2, 3, 1, 2...},...};

void setup() {
Serial.begin(9600);
}

void loop() {
for (int x = 0; x < 106; x++)
{
  for (int y = 0; y < 49; y++)
  {
    Serial.println(clr[pgm_read_byte_near(pixel[x][y])], HEX);
    delay(1000);
  }
}
}

However, when I run it this is the output I get:

A7CCD7
A7CCD7
CED6D7
A7CCD7
A7CCD7
A7CCD7
6B744A
A7CCD7
A7CCD7
CED6D7
CED6D7
CED6D7
CED6D7
CED6D7

It is not reading the correct data that I want it to read. I am unfamiliar with progmem and I could not find much helpful documentation on how to access stuff. I would appreciate it if anyone could help me correct my code.
Thanks.

Serial.println(clr[pgm_read_byte_near(&pixel[x][y])], HEX);

You have to get the address of the pixel's color index. You're not allowed to actually dereference it, since it's in progmem.

Pieter

PieterP:

Serial.println(clr[pgm_read_byte_near(&pixel[x][y])], HEX);

You have to get the address of the pixel's color index. You're not allowed to actually dereference it, since it's in progmem.

Pieter

Thank you, it worked!