音视频入门-13-使用开源库生成PNG图片

* 音视频入门文章目录 *

RGB-to-PNG 回顾

上一篇 【手动生成一张PNG图片】 根据 【PNG文件格式详解】 一步一步地手动实现了将 RGB 数据生成了一张 PNG 图片。

generate-png-by-hand.jpg

有许多开源的 PNG 相关的库可以简化开发:

使用开源库的方式

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, // 蓝
        0X8B00FF  // 紫
};

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

        // 当前颜色
        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];
        }
        // 当前颜色 R 分量
        uint8_t R = (currentColor & 0xFF0000) >> 16;
        // 当前颜色 G 分量
        uint8_t G = (currentColor & 0x00FF00) >> 8;
        // 当前颜色 B 分量
        uint8_t B = currentColor & 0x0000FF;

        for (int j = 0; j < height; ++j) {
            int currentPixelIndex = 3*(i*height + j);
            // 按 BGR 顺序写入一个像素 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, // 蓝
        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) {

        // 当前颜色
        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];
        }
        // 当前颜色 R 分量
        uint8_t R = (currentColor & 0xFF0000) >> 16;
        // 当前颜色 G 分量
        uint8_t G = (currentColor & 0x00FF00) >> 8;
        // 当前颜色 B 分量
        uint8_t B = currentColor & 0x0000FF;

        for (int j = 0; j < height; ++j) {
            int currentPixelIndex = 3*(i*height + j);
            // 按 BGR 顺序写入一个像素 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, // 蓝
        0X8B00FF  // 紫
};


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

    for (int i = 0; i < width; ++i) {
        // 当前颜色
        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];
        }
        // 当前颜色 R 分量
        uint8_t R = (currentColor & 0xFF0000) >> 16;
        // 当前颜色 G 分量
        uint8_t G = (currentColor & 0x00FF00) >> 8;
        // 当前颜色 B 分量
        uint8_t B = currentColor & 0x0000FF;

        for (int j = 0; j < height; ++j) {
            int currentPixelIndex = 3*(i*height + j);
            // 按 RGB 顺序写入一个像素 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;
        }
    //这里就是图像数据了
    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, // 蓝
        0X8B00FF  // 紫
};

uint8_t* getRainbowRGB24Data(uint8_t *rgb24Data, int width, int height) {
    for (int i = 0; i < width; ++i) {
        // 当前颜色
        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];
        }
        // 当前颜色 R 分量
        uint8_t R = (currentColor & 0xFF0000) >> 16;
        // 当前颜色 G 分量
        uint8_t G = (currentColor & 0x00FF00) >> 8;
        // 当前颜色 B 分量
        uint8_t B = currentColor & 0x0000FF;

        for (int j = 0; j < height; ++j) {
            int currentPixelIndex = 3*(i*height + j);
            // 按 BGR 顺序写入一个像素 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);
    //这里就是图像数据了
    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

参考资料:

如何把图片数据流使用libpng保存成png图片

PNG格式学习以及使用libpng解析


本文由博客一文多发平台 OpenWrite 发布!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,961评论 5 473
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,444评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 148,009评论 0 333
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,082评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,101评论 5 363
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,271评论 1 278
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,738评论 3 393
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,395评论 0 255
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,539评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,434评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,481评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,160评论 3 317
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,749评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,816评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,038评论 1 256
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,548评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,140评论 2 341

推荐阅读更多精彩内容