音視頻入門-13-使用開源庫生成PNG圖片

* 音視頻入門文章目錄 *

RGB-to-PNG 回顧

上一篇 【手動(dòng)生成一張PNG圖片】 根據(jù) 【PNG文件格式詳解】 一步一步地手動(dòng)實(shí)現(xiàn)了將 RGB 數(shù)據(jù)生成了一張 PNG 圖片鸠真。

generate-png-by-hand.jpg

有許多開源的 PNG 相關(guān)的庫可以簡(jiǎn)化開發(fā):

使用開源庫的方式

svpng

Demo 例子:

void test_rgb(void) {
    unsigned char rgb[256 * 256 * 3], *p = rgb;
    unsigned x, y;
    FILE *file = fopen("/Users/staff/Desktop/svpng-rgb.png", "wb");
    // FILE *file = fopen("C:\\Users\\Administrator\\Desktop\\svpng-rgb.png", "wb+");
    for (y = 0; y < 256; y++)
        for (x = 0; x < 256; x++) {
            *p++ = (unsigned char)x;    /* R */
            *p++ = (unsigned char)y;    /* G */
            *p++ = 128;                 /* B */
        }
    svpng(file, 256, 256, rgb, 0);
    fclose(file);
}

Demo 例子:

void test_rgba(void) {
    unsigned char rgba[256 * 256 * 4], *p = rgba;
    unsigned x, y;
    FILE *file = fopen("/Users/staff/Desktop/svpng-rgba.png", "wb");
    // FILE *file = fopen("C:\\Users\\Administrator\\Desktop\\svpng-rgba.png", "wb+");
    for (y = 0; y < 256; y++)
        for (x = 0; x < 256; x++) {
            *p++ = (unsigned char)x;                /* R */
            *p++ = (unsigned char)y;                /* G */
            *p++ = 128;                             /* B */
            *p++ = (unsigned char)((x + y) / 2);    /* A */
        }
    svpng(file, 256, 256, rgba, 1);
    fclose(file);
}

自定義 rainbow -> PNG:

// 彩虹的七種顏色
uint32_t rainbowColors[] = {
        0XFF0000, // 紅
        0XFFA500, // 橙
        0XFFFF00, // 黃
        0X00FF00, // 綠
        0X007FFF, // 青
        0X0000FF, // 藍(lán)
        0X8B00FF  // 紫
};

uint8_t* getRainbowRGB24Data(uint8_t *rgb24Data, int width, int height) {
    for (int i = 0; i < width; ++i) {

        // 當(dāng)前顏色
        uint32_t currentColor = rainbowColors[0];
        if(i < 100) {
            currentColor = rainbowColors[0];
        } else if(i < 200) {
            currentColor = rainbowColors[1];
        } else if(i < 300) {
            currentColor = rainbowColors[2];
        } else if(i < 400) {
            currentColor = rainbowColors[3];
        } else if(i < 500) {
            currentColor = rainbowColors[4];
        } else if(i < 600) {
            currentColor = rainbowColors[5];
        } else if(i < 700) {
            currentColor = rainbowColors[6];
        }
        // 當(dāng)前顏色 R 分量
        uint8_t R = (currentColor & 0xFF0000) >> 16;
        // 當(dāng)前顏色 G 分量
        uint8_t G = (currentColor & 0x00FF00) >> 8;
        // 當(dāng)前顏色 B 分量
        uint8_t B = currentColor & 0x0000FF;

        for (int j = 0; j < height; ++j) {
            int currentPixelIndex = 3*(i*height + j);
            // 按 BGR 順序?qū)懭胍粋€(gè)像素 RGB24 到文件中
            rgb24Data[currentPixelIndex] = R;
            rgb24Data[currentPixelIndex+1] = G;
            rgb24Data[currentPixelIndex+2] = B;
        }
    }
    return rgb24Data;
}

void rainbow_rgb() {
    int width = 700, height = 700;
    uint8_t rgb24Data[width*height*3];
    FILE *file = fopen("/Users/staff/Desktop/svpng-rgb-rainbow.png", "wb");
    // FILE *file = fopen("C:\\Users\\Administrator\\Desktop\\svpng-rgb-rainbow.png", "wb+");

    getRainbowRGB24Data(rgb24Data, width, height);
    svpng(file, width, height, rgb24Data, 0);
    fclose(file);
}
svpng-example-png.jpg

libattopng

Demo 例子:

void test01() {
    #define RGBA(r, g, b, a) ((r) | ((g) << 8) | ((b) << 16) | ((a) << 24))
    libattopng_t* png = libattopng_new(250, 200, PNG_RGBA);
    int x, y;
    for (y = 0; y < 200; y++) {
        for (x = 0; x < 250; x++) {
            libattopng_set_pixel(png, x, y, RGBA(x & 255, y & 255, 128, (255 - ((x / 2) & 255))));
        }
    }

    // FILE *file = fopen("/Users/staff/Desktop/libattopng-test-rgba.png", "wb");
    // FILE *file = fopen("C:\\Users\\Administrator\\Desktop\\libattopng-test-rgba", "wb+");
    libattopng_save(png, "/Users/staff/Desktop/libattopng-test-rgba.png");
    libattopng_destroy(png);
}

Demo 例子:

void test02() {
    #define RGBA(r, g, b, a) ((r) | ((g) << 8) | ((b) << 16) | ((a) << 24))
    // ceate palette image
    libattopng_t *png = libattopng_new(256, 256, PNG_PALETTE);
    uint32_t palette[] = {RGBA(0, 0, 0xff, 0xff), RGBA(0, 0xff, 0, 0x80), RGBA(0xff, 0, 0, 0xff), RGBA(0xff, 0, 0xff, 0x80)};
    // 4 colors: blue, green (50% alpha), red, cyan (50% alpha)
    libattopng_set_palette(png, palette, 4);

    int x, y;
    for (y = 0; y < 256; y++) {
        for (x = 0; x < 256; x++) {
            libattopng_set_pixel(png, x, y, (x % 16) / 4);
        }
    }
    // FILE *file = fopen("/Users/staff/Desktop/libattopng-test-rgba.png", "wb");
    // FILE *file = fopen("C:\\Users\\Administrator\\Desktop\\libattopng-test-rgba", "wb+");
    libattopng_save(png, "/Users/staff/Desktop/libattopng-test-palette.png");
    libattopng_destroy(png);
}

自定義 rainbow -> PNG:

// 彩虹的七種顏色
uint32_t rainbowColors[] = {
        0XFF0000, // 紅
        0XFFA500, // 橙
        0XFFFF00, // 黃
        0X00FF00, // 綠
        0X007FFF, // 青
        0X0000FF, // 藍(lán)
        0X8B00FF  // 紫
};


void testRainbow() {
    #define RGB(r, g, b) ((r) | ((g) << 8) | ((b) << 16))
    int width = 700, height = 700;
    // uint8_t rgb24Data[width*height*3];
    libattopng_t* png = libattopng_new(width, height, PNG_RGB);

    for (int i = 0; i < width; ++i) {

        // 當(dāng)前顏色
        uint32_t currentColor = rainbowColors[0];
        if(i < 100) {
            currentColor = rainbowColors[0];
        } else if(i < 200) {
            currentColor = rainbowColors[1];
        } else if(i < 300) {
            currentColor = rainbowColors[2];
        } else if(i < 400) {
            currentColor = rainbowColors[3];
        } else if(i < 500) {
            currentColor = rainbowColors[4];
        } else if(i < 600) {
            currentColor = rainbowColors[5];
        } else if(i < 700) {
            currentColor = rainbowColors[6];
        }
        // 當(dāng)前顏色 R 分量
        uint8_t R = (currentColor & 0xFF0000) >> 16;
        // 當(dāng)前顏色 G 分量
        uint8_t G = (currentColor & 0x00FF00) >> 8;
        // 當(dāng)前顏色 B 分量
        uint8_t B = currentColor & 0x0000FF;

        for (int j = 0; j < height; ++j) {
            int currentPixelIndex = 3*(i*height + j);
            // 按 BGR 順序?qū)懭胍粋€(gè)像素 RGB24 到文件中
            // rgb24Data[currentPixelIndex] = R;
            // rgb24Data[currentPixelIndex+1] = G;
            // rgb24Data[currentPixelIndex+2] = B;
            libattopng_set_pixel(png, j, i, RGB(R, G, B));
        }
    }

    // FILE *file = fopen("/Users/staff/Desktop/libattopng-rgb-rainbow.png", "wb");
    // FILE *file = fopen("C:\\Users\\Administrator\\Desktop\\libattopng-rgb-rainbow.png", "wb+");
    libattopng_save(png, "/Users/staff/Desktop/libattopng-rgb-rainbow.png");
    libattopng_destroy(png);

}
libattopng-example-png.jpg

lodepng

Demo 例子:

void test01() {
    /*generate some image*/
    unsigned width = 512, height = 512;
    unsigned char* image = malloc(width * height * 4);
    unsigned x, y;
    for(y = 0; y < height; y++)
        for(x = 0; x < width; x++) {
            image[4 * width * y + 4 * x + 0] = 255 * !(x & y);
            image[4 * width * y + 4 * x + 1] = x ^ y;
            image[4 * width * y + 4 * x + 2] = x | y;
            image[4 * width * y + 4 * x + 3] = 255;
        }

    /*Encode the image*/
    // FILE *file = fopen("/Users/staff/Desktop/lodepng-test.png", "wb");
    // FILE *file = fopen("C:\\Users\\Administrator\\Desktop\\lodepng-test.png", "wb+");
    unsigned error = lodepng_encode32_file("/Users/staff/Desktop/lodepng-test.png", image, width, height);

    /*if there's an error, display it*/
    if(error) printf("error %u: %s\n", error, lodepng_error_text(error));

    free(image);
}

自定義 rainbow -> PNG:

// 彩虹的七種顏色
uint32_t rainbowColors[] = {
        0XFF0000, // 紅
        0XFFA500, // 橙
        0XFFFF00, // 黃
        0X00FF00, // 綠
        0X007FFF, // 青
        0X0000FF, // 藍(lán)
        0X8B00FF  // 紫
};


void testRainbow() {
    int width = 700, height = 700;
    uint8_t rgb24Data[width*height*3];

    for (int i = 0; i < width; ++i) {
        // 當(dāng)前顏色
        uint32_t currentColor = rainbowColors[0];
        if(i < 100) {
            currentColor = rainbowColors[0];
        } else if(i < 200) {
            currentColor = rainbowColors[1];
        } else if(i < 300) {
            currentColor = rainbowColors[2];
        } else if(i < 400) {
            currentColor = rainbowColors[3];
        } else if(i < 500) {
            currentColor = rainbowColors[4];
        } else if(i < 600) {
            currentColor = rainbowColors[5];
        } else if(i < 700) {
            currentColor = rainbowColors[6];
        }
        // 當(dāng)前顏色 R 分量
        uint8_t R = (currentColor & 0xFF0000) >> 16;
        // 當(dāng)前顏色 G 分量
        uint8_t G = (currentColor & 0x00FF00) >> 8;
        // 當(dāng)前顏色 B 分量
        uint8_t B = currentColor & 0x0000FF;

        for (int j = 0; j < height; ++j) {
            int currentPixelIndex = 3*(i*height + j);
            // 按 RGB 順序?qū)懭胍粋€(gè)像素 RGB24 到文件中
             rgb24Data[currentPixelIndex] = R;
             rgb24Data[currentPixelIndex+1] = G;
             rgb24Data[currentPixelIndex+2] = B;
        }
    }

    // FILE *file = fopen("/Users/staff/Desktop/lodepng-rgb-rainbow.png", "wb");
    // FILE *file = fopen("C:\\Users\\Administrator\\Desktop\\lodepng-rgb-rainbow.png", "wb+");
    unsigned error = lodepng_encode24_file("/Users/staff/Desktop/lodepng-rgb-rainbow.png", rgb24Data, width, height);
    if(error) printf("error %u: %s\n", error, lodepng_error_text(error));
}
lodepng-example-png.jpg

libpng

Demo 例子:

void test01() {
    int width = 512, height = 512, bit_depth = 8;
    png_structp png_ptr;
    png_infop info_ptr;

    // FILE *file = fopen("C:\\Users\\Administrator\\Desktop\\libpng-test01.png", "wb+");
    FILE *png_file = fopen("/Users/staff/Desktop/libpng-test01.png", "wb");
    if (!png_file) {
        return ;
    }

    png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
    if(png_ptr == NULL) {
        printf("ERROR:png_create_write_struct/n");
        fclose(png_file);
        return ;
    }
    info_ptr = png_create_info_struct(png_ptr);
    if(info_ptr == NULL) {
        printf("ERROR:png_create_info_struct/n");
        png_destroy_write_struct(&png_ptr, NULL);
        return ;
    }
    png_init_io(png_ptr, png_file);
    png_set_IHDR(
            png_ptr,
            info_ptr,
            width,
            height,
            bit_depth,
            PNG_COLOR_TYPE_RGB_ALPHA,
            PNG_INTERLACE_NONE,
            PNG_COMPRESSION_TYPE_BASE,
            PNG_FILTER_TYPE_BASE);


    png_colorp palette = (png_colorp)png_malloc(png_ptr, PNG_MAX_PALETTE_LENGTH * sizeof(png_color));
    if (!palette) {
        fclose(png_file);
        png_destroy_write_struct(&png_ptr, &info_ptr);
        return ;
    }
    png_set_PLTE(png_ptr, info_ptr, palette, PNG_MAX_PALETTE_LENGTH);
    png_write_info(png_ptr, info_ptr);
    png_set_packing(png_ptr);

    /*generate some image*/
    unsigned char* image = malloc(width * height * 4);
    unsigned x, y;
    for(y = 0; y < height; y++)
        for(x = 0; x < width; x++) {
            image[4 * width * y + 4 * x + 0] = 255 * !(x & y);
            image[4 * width * y + 4 * x + 1] = x ^ y;
            image[4 * width * y + 4 * x + 2] = x | y;
            image[4 * width * y + 4 * x + 3] = 255;
        }
    //這里就是圖像數(shù)據(jù)了
    png_bytepp rows = (png_bytepp)png_malloc(png_ptr, height * sizeof(png_bytep));
    for (int i = 0; i < height; ++i) {
        rows[i] = (png_bytep)(image + (i) * width * 4);
    }

    png_write_image(png_ptr, rows);

    // delete[] rows;
    png_write_end(png_ptr, info_ptr);

    png_free(png_ptr, palette);
    palette=NULL;
    png_destroy_write_struct(&png_ptr, &info_ptr);
    fclose(png_file);
}

自定義 rainbow -> PNG:

// 彩虹的七種顏色
uint32_t rainbowColors[] = {
        0XFF0000, // 紅
        0XFFA500, // 橙
        0XFFFF00, // 黃
        0X00FF00, // 綠
        0X007FFF, // 青
        0X0000FF, // 藍(lán)
        0X8B00FF  // 紫
};

uint8_t* getRainbowRGB24Data(uint8_t *rgb24Data, int width, int height) {
    for (int i = 0; i < width; ++i) {
        // 當(dāng)前顏色
        uint32_t currentColor = rainbowColors[0];
        if(i < 100) {
            currentColor = rainbowColors[0];
        } else if(i < 200) {
            currentColor = rainbowColors[1];
        } else if(i < 300) {
            currentColor = rainbowColors[2];
        } else if(i < 400) {
            currentColor = rainbowColors[3];
        } else if(i < 500) {
            currentColor = rainbowColors[4];
        } else if(i < 600) {
            currentColor = rainbowColors[5];
        } else if(i < 700) {
            currentColor = rainbowColors[6];
        }
        // 當(dāng)前顏色 R 分量
        uint8_t R = (currentColor & 0xFF0000) >> 16;
        // 當(dāng)前顏色 G 分量
        uint8_t G = (currentColor & 0x00FF00) >> 8;
        // 當(dāng)前顏色 B 分量
        uint8_t B = currentColor & 0x0000FF;

        for (int j = 0; j < height; ++j) {
            int currentPixelIndex = 3*(i*height + j);
            // 按 BGR 順序?qū)懭胍粋€(gè)像素 RGB24 到文件中
            rgb24Data[currentPixelIndex] = R;
            rgb24Data[currentPixelIndex+1] = G;
            rgb24Data[currentPixelIndex+2] = B;
        }
    }
    return rgb24Data;
}

void testRainbow() {
    int width = 700, height = 700, bit_depth = 8;
    uint8_t rgb24Data[width*height*3];
    png_structp png_ptr;
    png_infop info_ptr;

    // FILE *file = fopen("C:\\Users\\Administrator\\Desktop\\libpng-rgb-rainbow.png", "wb+");
    FILE *png_file = fopen("/Users/staff/Desktop/libpng-rgb-rainbow.png", "wb");
    if (!png_file) {
        return ;
    }

    png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
    if(png_ptr == NULL) {
        printf("ERROR:png_create_write_struct/n");
        fclose(png_file);
        return ;
    }
    info_ptr = png_create_info_struct(png_ptr);
    if(info_ptr == NULL) {
        printf("ERROR:png_create_info_struct/n");
        png_destroy_write_struct(&png_ptr, NULL);
        return ;
    }
    png_init_io(png_ptr, png_file);
    png_set_IHDR(
            png_ptr,
            info_ptr,
            width,
            height,
            bit_depth,
            PNG_COLOR_TYPE_RGB,
            PNG_INTERLACE_NONE,
            PNG_COMPRESSION_TYPE_BASE,
            PNG_FILTER_TYPE_BASE);


    png_colorp palette = (png_colorp)png_malloc(png_ptr, PNG_MAX_PALETTE_LENGTH * sizeof(png_color));
    if (!palette) {
        fclose(png_file);
        png_destroy_write_struct(&png_ptr, &info_ptr);
        return ;
    }
    png_set_PLTE(png_ptr, info_ptr, palette, PNG_MAX_PALETTE_LENGTH);
    png_write_info(png_ptr, info_ptr);
    png_set_packing(png_ptr);

    getRainbowRGB24Data(rgb24Data, width, height);
    //這里就是圖像數(shù)據(jù)了
    png_bytepp rows = (png_bytepp)png_malloc(png_ptr, height * sizeof(png_bytep));
    for (int i = 0; i < height; ++i)
    {
        rows[i] = (png_bytep)(rgb24Data + (i) * width * 3);
    }

    png_write_image(png_ptr, rows);

    // delete[] rows;
    png_write_end(png_ptr, info_ptr);

    png_free(png_ptr, palette);
    palette=NULL;
    png_destroy_write_struct(&png_ptr, &info_ptr);
    fclose(png_file);
}
libpng-example-png.jpg

Congratulations!


代碼:
13-rgb-to-png-library

參考資料:

如何把圖片數(shù)據(jù)流使用libpng保存成png圖片

PNG格式學(xué)習(xí)以及使用libpng解析


本文由博客一文多發(fā)平臺(tái) OpenWrite 發(fā)布侵歇!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末暇检,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子渠退,更是在濱河造成了極大的恐慌,老刑警劉巖脐彩,帶你破解...
    沈念sama閱讀 218,607評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件碎乃,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡惠奸,警方通過查閱死者的電腦和手機(jī)梅誓,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,239評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來佛南,“玉大人梗掰,你說我怎么就攤上這事⌒峄兀” “怎么了及穗?”我有些...
    開封第一講書人閱讀 164,960評(píng)論 0 355
  • 文/不壞的土叔 我叫張陵,是天一觀的道長绵载。 經(jīng)常有香客問我埂陆,道長苛白,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,750評(píng)論 1 294
  • 正文 為了忘掉前任焚虱,我火速辦了婚禮购裙,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘著摔。我一直安慰自己缓窜,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,764評(píng)論 6 392
  • 文/花漫 我一把揭開白布谍咆。 她就那樣靜靜地躺著禾锤,像睡著了一般。 火紅的嫁衣襯著肌膚如雪摹察。 梳的紋絲不亂的頭發(fā)上恩掷,一...
    開封第一講書人閱讀 51,604評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音供嚎,去河邊找鬼黄娘。 笑死,一個(gè)胖子當(dāng)著我的面吹牛克滴,可吹牛的內(nèi)容都是我干的逼争。 我是一名探鬼主播,決...
    沈念sama閱讀 40,347評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼劝赔,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼誓焦!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起着帽,我...
    開封第一講書人閱讀 39,253評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤杂伟,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后仍翰,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體赫粥,經(jīng)...
    沈念sama閱讀 45,702評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,893評(píng)論 3 336
  • 正文 我和宋清朗相戀三年予借,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了越平。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,015評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡蕾羊,死狀恐怖喧笔,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情龟再,我是刑警寧澤书闸,帶...
    沈念sama閱讀 35,734評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站利凑,受9級(jí)特大地震影響浆劲,放射性物質(zhì)發(fā)生泄漏嫌术。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,352評(píng)論 3 330
  • 文/蒙蒙 一牌借、第九天 我趴在偏房一處隱蔽的房頂上張望度气。 院中可真熱鬧,春花似錦膨报、人聲如沸磷籍。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,934評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽院领。三九已至,卻和暖如春够吩,著一層夾襖步出監(jiān)牢的瞬間比然,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,052評(píng)論 1 270
  • 我被黑心中介騙來泰國打工周循, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留强法,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,216評(píng)論 3 371
  • 正文 我出身青樓湾笛,卻偏偏與公主長得像饮怯,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子嚎研,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,969評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容