libpng主動翻轉像素數據錯誤導致OpenGL ES濾鏡效果變樣

這實際并非libpng核心代碼的問題,是我們項目會用的包裝代碼存在的行為。
我們的Android NDK項目中使用了libpng,在讀取一個顏色查找表圖像時,發現效果不對,比iOS實現存在顏色偏差。從png.h看,版本為libpng version 1.2.33 - October 31, 2008。在包裝libpng的代碼中,有這么一段特殊處理的代碼:

unsigned char *rgba = new unsigned char[width * height * 4];  //each pixel(RGBA) has 4 bytes
png_bytep *row_pointers = png_get_rows(png_ptr, info_ptr);
//unlike store the pixel data from top-left corner, store them from bottom-left corner for OGLES Texture drawing...
int pos = (width * height * 4) - (4 * width);
for (int row = 0; row < height; row++) {
    for (int col = 0; col < (4 * width); col += 4) {
        rgba[pos++] = row_pointers[row][col + 0] * row_pointers[row][col + 3] / 255; // blue
        rgba[pos++] = row_pointers[row][col + 1] * row_pointers[row][col + 3] / 255; // green
        rgba[pos++] = row_pointers[row][col + 2] * row_pointers[row][col + 3] / 255; // red
        rgba[pos++] = row_pointers[row][col + 3]; // alpha
    }
    pos = (pos - (width * 4) * 2); //move the pointer back two rows
}

顯然,這對圖像作了垂直鏡像(Flip Vertical),即第一行像素變成最后一行像素。而我提供的png圖像是RGB24,上述代碼將返回錯誤的結果。

Input #0, png_pipe, from 'lookup_crenshaw.png':
  Duration: N/A, bitrate: N/A
    Stream #0:0: Video: png, rgb24(pc), 128x128, 25 tbr, 25 tbn, 25 tbc

將其修改為讀取RGB三通道值即可。

而使用UIImage(返回RGBA)或Android SDK的Bitmap類(返回ARGB)都是從左上角開始讀取圖像數據,故它們除了Alpha通道位置不同,RGB數據都相同。

值得注意的是,Bitmap的getPixels接口中有個stride參數,如果是讀取整張圖像,應該傳遞圖像的寬度值,示例如下。

try {
    InputStream = new FileInputStream("/storage/emulated/0/lookup_dark.png");
    Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
    int[] argb = new int[bitmap.getWidth() * bitmap.getHeight()];
    bitmap.getPixels(argb, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
    int[] rgba = new int[bitmap.getWidth() * bitmap.getHeight()];
    for (int row = 0; row < bitmap.getHeight(); ++row) {
        int index = row * bitmap.getWidth();
        for (int col = 0; col < bitmap.getWidth(); ++col) {
            int srcARGB = argb[col + index];
            rgba[col + index] = ((srcARGB & 0xFF000000) >> 24) + ((srcARGB & 0xFFFFFF) << 8);
        }
    }
} catch (Exception e) {
    e.printStackTrace();
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容