E
E
Evgeny Trofimov2016-05-26 18:17:29
Programming
Evgeny Trofimov, 2016-05-26 18:17:29

Why is extra information written to palette images (bmp)?

If I understand correctly - when the color depth is 1.4 or 8, then after the file and image headers, a palette of 2 ^ BitCount (color depth) colors follows. And after this palette, the indexes of the colors of the palette follow.
Here is the read function

int Image::loadpaletteimg(FILE *f)
{
    int Palette_size = pow(2,BMInfoHeader.BitCount);//размер палитры - 2 в степени глубины цвета
    Palette=new RGBQUAD[Palette_size];
    
    for (int i = 0; i < Palette_size; i++) fread(&(Palette[i]),sizeof(RGBQUAD),1,f);//считываем палитру

    Rgbquad=new RGBQUAD*[BMInfoHeader.Height];//cтолбцы

    for (int i = 0; i < BMInfoHeader.Height; i++)//построчно записываем
    {
      Rgbquad[i]=new RGBQUAD[BMInfoHeader.Width];//строки
      fread(Rgbquad[i], sizeof(RGBQUAD), BMInfoHeader.Width, f);
    }
    if(Rgbquad) cout<<"\nИзображение успешно загружено!"<<endl;
    return 1;
}

And here it is for the record
void Image::writepaletteimg(FILE *f)
{
  int Palette_size = pow(2,BMInfoHeader.BitCount);
  for (int i = 0; i < Palette_size; i++)
    {
      fwrite(&(Palette[i]),sizeof(RGBQUAD),1,f);
      //cout<<i<<endl;
    }
  cout<<"Палитра записана!"<<endl;
  int size = BMInfoHeader.Height*BMInfoHeader.Width;

  for (int i = 0; i < BMInfoHeader.Height; i++)
  {
    fwrite(Rgbquad[i], sizeof(RGBQUAD), BMInfoHeader.Width, f);
  }
  fclose(f);
  return;
}

As I understand it, the problem is in my two-dimensional array, because it seems to be not needed for indexes, but I don’t quite understand what exactly needs to be changed ... The picture is written almost correctly, that is, it can be read, but at the end file some kind of muck is written, scoring 70% of the picture. That is, if the original picture weighs 9.05 KB, then after rewriting in my program - 32.8 KB.
40846d3c51164e788f9eac6859a94480.jpg
How can I rewrite so that I don't write too much?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey, 2016-05-26
@deadrime

From

Rgbquad=new RGBQUAD*[BMInfoHeader.Height];//cтолбцы
and did not understand below.
Where does RGBQUAD and its size come from... We
have already read the palette at the previous step.

A
AtomKrieg, 2016-05-27
@AtomKrieg

https://ru.wikipedia.org/wiki/BMP#BITMAPFILEHEADER
I'm scrolling through Wikipedia and I see that there is such a field "Color table size in cells." the latest versions of the bmp format. Those. it may be worth determining the size of the table from the header, and not raising it to the power of two.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question