簡體   English   中英

如何使用 libpng 庫獲取 16 位深度的灰度圖像的像素值?

[英]How to get pixel value of a grayscale image with 16 bits depth using libpng library?

我有一個 16 位深度的灰度圖像。 我想使用 libpng 庫獲取圖像的像素值。 但是,我的程序的輸出是該圖像的所有像素值在 0 到 255 之間。 我用 ImageJ 軟件用那個圖像檢查了它,事實上它們在 0 到 65535 之間。我不知道我哪里出錯了。 這是我的代碼。

#include <stdio.h>
#include <png.h>

int main(void) {
    FILE *fp = fopen("sample.png", "r");

    png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
    png_infop png_info = png_create_info_struct(png_ptr);

    png_init_io(png_ptr, fp);

    png_bytepp rows;

    png_read_png(png_ptr, png_info, PNG_TRANSFORM_IDENTITY, NULL);

    int WIDTH      = png_get_image_width(png_ptr, png_info);
    int HEIGHT     = png_get_image_height(png_ptr, png_info);

    rows = png_get_rows(png_ptr, png_info);

    for (int h = 0; h < HEIGHT; h++) {
        for (int w = 0; w < WIDTH; w++) {
            printf("%d ", rows[h][w]);
        }   
        printf("\n");
    }
    return 0;
}

請幫我。 非常感謝。

如果您更改以下幾行的類型,它會起作用:

unsigned short **rows;

rows = (unsigned short**)png_get_rows(png_ptr, png_info);

暫無
暫無

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

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