简体   繁体   English

如何从jpg图像中获取RGB888(24位)和RGB565(16位)帧缓冲转储?

[英]how to get RGB888 (24 - bit) and RGB565 (16-bit) framebuffer dump from a jpg image?

I need to resize ( either upscale or downscale) my image in jpg/png format. 我需要以jpg / png格式调整图像的尺寸(放大或缩小)。 I'm using Bilinear interpolation to resize. 我正在使用双线性插值来调整大小。 My code works fine with values I gave in an array. 我的代码可以很好地处理我在数组中提供的值。 But to test the result with an image, I need RGB565 and RGB888 dump. 但是要用图像测试结果,我需要RGB565和RGB888转储。

As my task is to just resize the image, I would appreciate if I can get a dump with width and height of the image or just an algorithm would also do. 由于我的任务是只是调整图像的大小,因此,如果我可以得到具有图像宽度和高度的转储文件,或者仅使用算法也可以。

I am working on C. Please help me. 我正在研究C。请帮助我。

Thanks 谢谢

If all you're missing is a conversion from RGB888 to RGB565, then it's easy. 如果您所缺少的只是从RGB888到RGB565的转换,那么这很容易。 Call this following function for each pixel: 为每个像素调用以下函数:

unsigned short int rgb888Torgb565(unsigned int rgb888Pixel)
{
    int red   = (rgb888Pixel >> 16) & 0xff;
    int green = (rgb888Pixel >> 8 ) & 0xff;
    int blue  =  rgb888Pixel        & 0xff;

    unsigned short  b =   (blue  >> 3) & 0x001f;
    unsigned short  g = ( (green >> 2) & 0x003f ) << 6;
    unsigned short  r = ( (red   >> 3) & 0x001f ) << 11;

    return (unsigned short int) (r | g | b);
}

Apply the function to all pixels. 将功能应用于所有像素。 Each one will shrink from 3 bytes to 2 bytes. 每一个将从3个字节缩小到2个字节。

My solution to convert a png to raw RGB 565 format in Python (called png2fb.py): 我在Python中将png转换为原始RGB 565格式的解决方案(称为png2fb.py):

#!/usr/bin/python

import sys
from PIL import Image

if len(sys.argv) == 3:
    # print "\nReading: " + sys.argv[1]
    out = open(sys.argv[2], "wb")
elif len(sys.argv) == 2:
    out = sys.stdout
else:
    print "Usage: png2fb.py infile [outfile]"
    sys.exit(1)

im = Image.open(sys.argv[1])

if im.mode == "RGB":
    pixelSize = 3
elif im.mode == "RGBA":
    pixelSize = 4
else:
    sys.exit('not supported pixel mode: "%s"' % (im.mode))

pixels = im.tostring()
pixels2 = ""
for i in range(0, len(pixels) - 1, pixelSize):
    pixels2 += chr(ord(pixels[i + 2]) >> 3 | (ord(pixels[i + 1]) << 3 & 0xe0))
    pixels2 += chr(ord(pixels[i]) & 0xf8 | (ord(pixels[i + 1]) >> 5 & 0x07))
out.write(pixels2)
out.close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM