图像格式和无符号字符数组

发布于 2024-08-17 22:50:18 字数 456 浏览 5 评论 0原文

我正在开发成像功能(是的,出于各种原因我真的想重新发明轮子)。

我正在将位图复制到无符号字符数组中,但在字节大小与图像像素格式方面遇到一些问题。

例如,很多图像以每像素 24 位的 RGB 表示,所以这相当简单,每个像素都有 3 个无符号字符(字节),每个人都很高兴,

但有时 RGB 有更奇怪的类型,例如每像素 48 位,每种颜色 16 位渠道。将整个图像复制到字节数组中效果很好,但是当我想检索数据时,事情变得模糊

现在我有以下代码来获取灰度图像的单个像素

unsigned char NImage::get_pixel(int i, int j)
   {
   return this->data[j * pitch + i];
   }

NImage::data 是无符号字符数组

这返回一个字节。如何访问具有不同像素格式的数据数组?

I'm developping imaging functions (yes I REALLY want to reinvent the wheel for various reasons).

I'm copying bitmaps into unsigned char arrays but I'm having some problem with byte size versus image pixel format.

for example a lot of images come as 24 bits per pixel for RGB representation so that's rather easy, every pixel has 3 unsigned chars (bytes) and everyone is happy

however sometimes the RGB has weirder types like 48 bits per pixel so 16 bits per color channel. Copying the whole image into the byte array works fine but its when I want to retrieve the data that things get blurry

Right now I have the following code to get a single pixel for grayscale images

unsigned char NImage::get_pixel(int i, int j)
   {
   return this->data[j * pitch + i];
   }

NImage::data is unsigned char array

This returns a single byte. How can I access my data array with different pixel formats?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

葮薆情 2024-08-24 22:50:18

你应该这样做:

unsigned short NImage::get_pixel(int i, int j)
   {
       int offset = 2 * (j * pitch + i);
      // image pixels are usually stored in big-endian format
      return data[offset]*256 + data[offset+1];
   }

You should do it like this:

unsigned short NImage::get_pixel(int i, int j)
   {
       int offset = 2 * (j * pitch + i);
      // image pixels are usually stored in big-endian format
      return data[offset]*256 + data[offset+1];
   }
看海 2024-08-24 22:50:18

在每像素 48 位、每种颜色 16 位的情况下,您无法返回 8 位值,必须返回 16 位 shortunsigned Short,否则数据会被截断。

您可以尝试开发重载函数来处理这个问题。

At 48 bits per pixel, with 16 bit per color, you can't return an 8 bit value, you must return a 16 bit short or unsigned short otherwise the data gets truncated.

You might try developing overloaded functions to handle this.

夜雨飘雪 2024-08-24 22:50:18

你必须知道你的像素有多大。

如果它是 RGB,那么你的 100x100 像素图像(比如说)将有 30,000 个无符号字符。

unsigned char NImage::get_red_component(int i, int j) 
{ 
    return this->data[3*(j * pitch + i)]; 
}

unsigned char NImage::get_green_component(int i, int j) 
{ 
    return this->data[3*(j * pitch + i) + 1]; 
}

unsigned char NImage::get_blue_component(int i, int j) 
{ 
    return this->data[3*(j * pitch + i) + 2]; 
}

或者对于 48 位 RGB,

unsigned char NImage::get_red_MSB(int i, int j) 
{ 
    return this->data[6*(j * pitch + i)]; 
}

unsigned char NImage::get_red_LSB(int i, int j) 
{ 
    return this->data[6*(j * pitch + i) + 1]; 
}

... etc etc ...

You have to know how big your pixels are.

If it's RGB then your 100x100 pixel image (say) will have 30,000 unsigned chars.

unsigned char NImage::get_red_component(int i, int j) 
{ 
    return this->data[3*(j * pitch + i)]; 
}

unsigned char NImage::get_green_component(int i, int j) 
{ 
    return this->data[3*(j * pitch + i) + 1]; 
}

unsigned char NImage::get_blue_component(int i, int j) 
{ 
    return this->data[3*(j * pitch + i) + 2]; 
}

Or for 48-bit RGB,

unsigned char NImage::get_red_MSB(int i, int j) 
{ 
    return this->data[6*(j * pitch + i)]; 
}

unsigned char NImage::get_red_LSB(int i, int j) 
{ 
    return this->data[6*(j * pitch + i) + 1]; 
}

... etc etc ...
冷…雨湿花 2024-08-24 22:50:18

每像素 48 位有什么问题?只需将数据读取为 uint16_t 或 unsigned Short,即可正确提取 16 位数据。

对于更复杂的位模式(即 rgb565),情况会变得更糟,您需要使用位掩码提取数据。

What's the problem with 48bits per pixel? Simply read your data as uint16_t or unsigned short and you get the 16 bit extracted properly.

It gets worse for more complicated bit pattern, i.e. rgb565 where you'll need to extract data using bitmasks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文