2017-05-27 278 views
0

我試圖將圖像轉換爲這個以下DDS格式:針對每個信道的4位轉換RGBA到ARGB像素格式

| Resource Format | dwFlags | dwRGBBitCount | dwRBitMask | dwGBitMask | dwBBitMask | dwABitMask | 
+-----------------+----------+---------------+------------+------------+------------+------------+ 
| D3DFMT_A4R4G4B4 | DDS_RGBA | 16   | 0xf00  | 0xf0  | 0xf  | 0xf000  | 

D3DFMT_A4R4G4B4 16位ARGB像素格式。

我有(使用法杖LIB)這個Python代碼:

# source is jpeg converted to RGBA format (wand only supports RGBA not ARGB) 
blob = img.make_blob(format="RGBA") 

for x in range(0, img.width * img.height * 4, 4): 
    r = blob[x] 
    g = blob[x + 1] 
    b = blob[x + 2] 
    a = blob[x + 3] 

    # a=255 r=91 g=144 b=72 
    pixel = (a << 12 | r << 8 | g << 4 | b) & 0xffff 

第一像素我得到的是64328但我期待62868

問:

  • 是我的RGBA到ARGB轉換錯了嗎?
  • 爲什麼我沒有得到想要的結果?

預期輸出(左)與我的代碼的實際輸出(右): enter image description hereenter image description here

+1

將R,G,B值是在字節(8位),則需要按比例下來,4位(0-16 )在重新組裝像素之前。 –

+1

ps如果你這樣做是爲了與圖像一起工作,而不僅僅是作業練習使用opencv(import cv2)它更快更容易 –

+0

@MartinBeckett謝謝,最後想通了。雖然似乎有一點區別,但在產出和預期產出方面存在非常細微的差異。 – majidarif

回答

0

隨着@ MartinBeckett的評論關於scaling down源像素8位到4位。我試圖搜索如何做到這一點,並最終找到了解決方案。

只需向右移4位即可8-4=4。最終的代碼是:

r = blob[x]  >> 4 
g = blob[x + 1] >> 4 
b = blob[x + 2] >> 4 
a = blob[x + 3] >> 4 

pixel = (a << 12 | r << 8 | g << 4 | b) & 0xffff 

儘管輸出與預期輸出之間仍然有非常小的差異。 (具有差部分)

輸出:enter image description here
預期:enter image description here
來源:enter image description here