簡體   English   中英

BMP 16位圖像轉換為數組

[英]BMP 16-bit Image converting into array

我有一個BMP格式的圖片,該圖片以以下方式存檔

  for (j = 0; j < 240; j++) {
    for(i=0;i<320;i++) { 
      data_temp = LCD_ReadRAM();
      image_buf[i*2+1] = (data_temp&0xff00) >> 8;
      image_buf[i*2+0] = data_temp & 0x00ff;

    }
    ret = f_write(&file, image_buf, 640, &bw);

LCD_ReadRam函數一次從LCD屏幕讀取一個像素

我想知道,如何獲得此圖像文件的像素位置 以及如何將每個像素的值保存在[320] [240]矩陣中
任何幫助,將不勝感激,謝謝。

BMP文件閱讀器可以滿足您的需求。 您可以獲取任何優質的BMP文件閱讀器,並對其進行調整。 例如: 此問題和答案為BMP文件閱讀器提供了24位BMP格式。 您的格式為16位,因此需要進行一些調整。

這是我嘗試進行的操作(未經測試,因此您應該將一些硬編碼的細節放在一粒鹽上)。

int i;
FILE* f = fopen(filename, "rb");
unsigned char info[54];
fread(info, sizeof(unsigned char), 54, f); // read the 54-byte header

int width = 320, height = 240; // might want to extract that info from BMP header instead

int size_in_file = 2 * width * height;
unsigned char* data_from_file = new unsigned char[size_in_file];
fread(data_from_file, sizeof(unsigned char), size_in_file, f); // read the rest
fclose(f);

unsigned char pixels[240 * 320][3];

for(i = 0; i < width * height; ++i)
{
    unsigned char temp0 = data_from_file[i * 2 + 0];
    unsigned char temp1 = data_from_file[i * 2 + 1];
    unsigned pixel_data = temp1 << 8 | temp0;

    // Extract red, green and blue components from the 16 bits
    pixels[i][0] = pixel_data >> 11;
    pixels[i][1] = (pixel_data >> 5) & 0x3f;
    pixels[i][2] = pixel_data & 0x1f;
}

注意:這是假定您的LCD_ReadRAM函數(大概是從LCD內存中讀取內容)以標准5-6-5格式給出像素。

名稱5-6-5表示分配給每個顏色分量(紅色,綠色,藍色)的每個16位數字中的位數。 還有其他分配,例如5-5-5 ,但我從未在實踐中見過。

如果您正在談論BMP圖像,則存在現有的BMP格式

BMP圖像中,所有像素都以相反的順序(從圖像的最后一行開始)順序寫入。 大小是在BMP標頭中定義的,因此您必須閱讀它。

還有一點是,圖像中的每一行都有填充以使其乘以4。

您可以使用gimp。 在gimp中打開圖像,使用插件以16位模式導出C代碼,並將其放入導出C代碼的數組中:-)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM