利用libpng保存YUV NV21格式的图片及优化

本文探讨了从YUV格式转换到RGB格式,并使用libpng库将RGB数据保存为PNG图片的过程。通过不同的优化策略,如整数运算、移位操作和查找表,尝试减少转换和编码的时间开销。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

将YUV格式转换为RGB格式后,利用libpng保存RGB为png即可,关于YUV格式的详细情况和与RGB的转换关系在网上找找就可以了。目前的做法耗时比较多。
704*576 480ms

bool CompressNV21ToPng(int width, int height, uint8_t * yuv_data)
{
    FILE *fp;
    png_structp png_ptr = NULL;
    png_infop info_ptr = NULL;
    std::string path;
    GenFileName(path, file_path_, mLocal_);
    fp = fopen(path.c_str(), "wb");
    if (fp){
        png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL, NULL, NULL);
        info_ptr = png_create_info_struct(png_ptr);
        if(png_ptr == NULL || info_ptr == NULL){
            fclose(fp);
            return false;
        }
        mFileName_ = path;
        png_init_io(png_ptr, fp);
        png_set_IHDR(png_ptr, info_ptr, width, height, 8, PNG_COLOR_TYPE_RGB_ALPHA,PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
        png_write_info(png_ptr, info_ptr);
        png_set_packing(png_ptr);
        uint8_t * data = new uint8_t [32 * width];
        int nv_start = width * height ;
        uint32_t rgb_index = 0;
        int r, g, b, nv_index,y, ru, rv;
        for(uint32_t i = 0; i < height ; ++i){
            for(uint32_t j = 0; j < width; ++j){
                nv_index = i / 2 * width + j - j % 2;
                y = yuv_data[rgb_index];
                ru = yuv_data[nv_start + nv_index] - 128;
                rv = yuv_data[nv_start + nv_index + 1] - 128;
                r = y + 1.4 * rv; //r
                g = y - 0.34 * ru - 0.71 * rv; //g
                b = y + 1.77 * ru; //b
                if(r > 255) r = 255;
                if(g > 255) g = 255;
                if(b > 255) b = 255;
                if(r < 0) r = 0;
                if(g < 0) g = 0;
                if(b < 0) b = 0;
                data[j * 4 + 0] = b;
                data[j * 4 + 1] = g;
                data[j * 4 + 2] = r;
                data[j * 4 + 3] = 255;
                ++rgb_index;
            }
            png_write_row(png_ptr,data);
        }
        delete data;
        png_write_end(png_ptr, info_ptr);
        png_destroy_write_struct(&png_ptr, &info_ptr);
        fclose(fp);
        return true;
    }
    return false;
}

改成整形计算:370ms左右

bool CompressNV21ToPng(int width, int height, uint8_t * yuv_data)
{
    FILE *fp;
    png_structp png_ptr = NULL;
    png_infop info_ptr = NULL;
    std::string path;
    GenFileName(path, file_path_, mLocal_);
    fp = fopen(path.c_str(), "wb");
    if (fp){
        png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL, NULL, NULL);
        info_ptr = png_create_info_struct(png_ptr);
        if(png_ptr == NULL || info_ptr == NULL){
            fclose(fp);
            return false;
        }
        mFileName_ = path;
        png_init_io(png_ptr, fp);
        png_set_IHDR(png_ptr, info_ptr, width, height, 8, PNG_COLOR_TYPE_RGB_ALPHA,PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
        png_write_info(png_ptr, info_ptr);
        png_set_packing(png_ptr);
        uint8_t * data = new uint8_t [width << 2];
        memset(data,255,width << 2);
        int nv_start = width * height ;
        uint32_t rgb_index = 0,pos;
        int r, g, b, nv_index,y, ru, rv;
        for(uint32_t i = 0; i < height ; ++i){
            for(uint32_t j = 0; j < width; ++j){
                nv_index = (i >> 1) * width + j - (j & 1) + nv_start;
                y = yuv_data[rgb_index];
                ru = yuv_data[nv_index] - 128;
                rv = yuv_data[nv_index + 1] - 128;
                r = y + 140 * rv / 100; //r
                g = y - 34 * ru / 100 - 71 * rv / 100; //g
                b = y + 177 * ru / 100; //b
                if(r > 255) r = 255;
                if(g > 255) g = 255;
                if(b > 255) b = 255;
                if(r < 0 ) r = 0;
                if(g < 0 ) g = 0;
                if(b < 0 ) b = 0;
                pos = j << 2;
                data[pos ] = b;
                data[pos + 1] = g;
                data[pos + 2] = r;
                ++rgb_index;
            }
            png_write_row(png_ptr,data);
        }
        delete data;
        png_write_end(png_ptr, info_ptr);
        png_destroy_write_struct(&png_ptr, &info_ptr);
        fclose(fp);
        return true;
    }
    return false;
}

去掉除法,改成移位,没多大改善:

bool CompressNV21ToPng(int width, int height, uint8_t * yuv_data)
{
    FILE *fp;
    png_structp png_ptr = NULL;
    png_infop info_ptr = NULL;
    std::string path;
    GenFileName(path, file_path_, mLocal_);
    fp = fopen(path.c_str(), "wb");
    if (fp){
        png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL, NULL, NULL);
        info_ptr = png_create_info_struct(png_ptr);
        if(png_ptr == NULL || info_ptr == NULL){
            fclose(fp);
            return false;
        }
        mFileName_ = path;
        png_init_io(png_ptr, fp);
        png_set_IHDR(png_ptr, info_ptr, width, height, 8, PNG_COLOR_TYPE_RGB_ALPHA,PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
        png_write_info(png_ptr, info_ptr);
        png_set_packing(png_ptr);
        uint8_t * data = new uint8_t [width << 2];
        memset(data,255,width << 2);
        int nv_start = width * height ;
        uint32_t rgb_index = 0,pos;
        int r, g, b, nv_index,y, ru, rv;
        for(uint32_t i = 0; i < height ; ++i){
            for(uint32_t j = 0; j < width; ++j){
                nv_index = (i >> 1) * width + j - (j & 1) + nv_start;
                y = yuv_data[rgb_index];
                ru = yuv_data[nv_index] - 128;
                rv = yuv_data[nv_index + 1] - 128;
                r = y + ((103 * rv) >> 8); //r
                g = y - ((88 * ru) >> 8) - ((183 * rv) >> 8); //g
                b = y + ((198 * ru) >> 8); //b
                if(r > 255) r = 255;
                if(g > 255) g = 255;
                if(b > 255) b = 255;
                if(r < 0 ) r = 0;
                if(g < 0 ) g = 0;
                if(b < 0 ) b = 0;
                pos = j << 2;
                data[pos ] = b;
                data[pos + 1] = g;
                data[pos + 2] = r;
                ++rgb_index;
            }
            png_write_row(png_ptr,data);
        }
        delete data;
        png_write_end(png_ptr, info_ptr);
        png_destroy_write_struct(&png_ptr, &info_ptr);
        fclose(fp);
        return true;
    }
    return false;
}

部分查表,多消耗1kb内存,没啥改善:

bool CompressNV21ToPng(int width, int height, uint8_t * yuv_data)
{
    const static int Table_fv1[256]={ -180, -179, -177, -176, -174, -173, -172, -170, -169, -167, -166, -165, -163, -162, -160, -159, -158, -156, -155, -153, -152, -151, -149, -148, -146, -145, -144, -142, -141, -139, -138, -137, -135, -134, -132, -131, -130, -128, -127, -125, -124, -123, -121, -120, -118, -117, -115, -114, -113, -111, -110, -108, -107, -106, -104, -103, -101, -100, -99, -97, -96, -94, -93, -92, -90, -89, -87, -86, -85, -83, -82, -80, -79, -78, -76, -75, -73, -72, -71, -69, -68, -66, -65, -64, -62, -61, -59, -58, -57, -55, -54, -52, -51, -50, -48, -47, -45, -44, -43, -41, -40, -38, -37, -36, -34, -33, -31, -30, -29, -27, -26, -24, -23, -22, -20, -19, -17, -16, -15, -13, -12, -10, -9, -8, -6, -5, -3, -2, 0, 1, 2, 4, 5, 7, 8, 9, 11, 12, 14, 15, 16, 18, 19, 21, 22, 23, 25, 26, 28, 29, 30, 32, 33, 35, 36, 37, 39, 40, 42, 43, 44, 46, 47, 49, 50, 51, 53, 54, 56, 57, 58, 60, 61, 63, 64, 65, 67, 68, 70, 71, 72, 74, 75, 77, 78, 79, 81, 82, 84, 85, 86, 88, 89, 91, 92, 93, 95, 96, 98, 99, 100, 102, 103, 105, 106, 107, 109, 110, 112, 113, 114, 116, 117, 119, 120, 122, 123, 124, 126, 127, 129, 130, 131, 133, 134, 136, 137, 138, 140, 141, 143, 144, 145, 147, 148, 150, 151, 152, 154, 155, 157, 158, 159, 161, 162, 164, 165, 166, 168, 169, 171, 172, 173, 175, 176, 178 };
    const static int Table_fv2[256]={ -92, -91, -91, -90, -89, -88, -88, -87, -86, -86, -85, -84, -83, -83, -82, -81, -81, -80, -79, -78, -78, -77, -76, -76, -75, -74, -73, -73, -72, -71, -71, -70, -69, -68, -68, -67, -66, -66, -65, -64, -63, -63, -62, -61, -61, -60, -59, -58, -58, -57, -56, -56, -55, -54, -53, -53, -52, -51, -51, -50, -49, -48, -48, -47, -46, -46, -45, -44, -43, -43, -42, -41, -41, -40, -39, -38, -38, -37, -36, -36, -35, -34, -33, -33, -32, -31, -31, -30, -29, -28, -28, -27, -26, -26, -25, -24, -23, -23, -22, -21, -21, -20, -19, -18, -18, -17, -16, -16, -15, -14, -13, -13, -12, -11, -11, -10, -9, -8, -8, -7, -6, -6, -5, -4, -3, -3, -2, -1, 0, 0, 1, 2, 2, 3, 4, 5, 5, 6, 7, 7, 8, 9, 10, 10, 11, 12, 12, 13, 14, 15, 15, 16, 17, 17, 18, 19, 20, 20, 21, 22, 22, 23, 24, 25, 25, 26, 27, 27, 28, 29, 30, 30, 31, 32, 32, 33, 34, 35, 35, 36, 37, 37, 38, 39, 40, 40, 41, 42, 42, 43, 44, 45, 45, 46, 47, 47, 48, 49, 50, 50, 51, 52, 52, 53, 54, 55, 55, 56, 57, 57, 58, 59, 60, 60, 61, 62, 62, 63, 64, 65, 65, 66, 67, 67, 68, 69, 70, 70, 71, 72, 72, 73, 74, 75, 75, 76, 77, 77, 78, 79, 80, 80, 81, 82, 82, 83, 84, 85, 85, 86, 87, 87, 88, 89, 90, 90 };
    const static int Table_fu1[256]={ -44, -44, -44, -43, -43, -43, -42, -42, -42, -41, -41, -41, -40, -40, -40, -39, -39, -39, -38, -38, -38, -37, -37, -37, -36, -36, -36, -35, -35, -35, -34, -34, -33, -33, -33, -32, -32, -32, -31, -31, -31, -30, -30, -30, -29, -29, -29, -28, -28, -28, -27, -27, -27, -26, -26, -26, -25, -25, -25, -24, -24, -24, -23, -23, -22, -22, -22, -21, -21, -21, -20, -20, -20, -19, -19, -19, -18, -18, -18, -17, -17, -17, -16, -16, -16, -15, -15, -15, -14, -14, -14, -13, -13, -13, -12, -12, -11, -11, -11, -10, -10, -10, -9, -9, -9, -8, -8, -8, -7, -7, -7, -6, -6, -6, -5, -5, -5, -4, -4, -4, -3, -3, -3, -2, -2, -2, -1, -1, 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 11, 11, 11, 12, 12, 12, 13, 13, 13, 14, 14, 14, 15, 15, 15, 16, 16, 16, 17, 17, 17, 18, 18, 18, 19, 19, 19, 20, 20, 20, 21, 21, 22, 22, 22, 23, 23, 23, 24, 24, 24, 25, 25, 25, 26, 26, 26, 27, 27, 27, 28, 28, 28, 29, 29, 29, 30, 30, 30, 31, 31, 31, 32, 32, 33, 33, 33, 34, 34, 34, 35, 35, 35, 36, 36, 36, 37, 37, 37, 38, 38, 38, 39, 39, 39, 40, 40, 40, 41, 41, 41, 42, 42, 42, 43, 43 };
    const static int Table_fu2[256]={ -227, -226, -224, -222, -220, -219, -217, -215, -213, -212, -210, -208, -206, -204, -203, -201, -199, -197, -196, -194, -192, -190, -188, -187, -185, -183, -181, -180, -178, -176, -174, -173, -171, -169, -167, -165, -164, -162, -160, -158, -157, -155, -153, -151, -149, -148, -146, -144, -142, -141, -139, -137, -135, -134, -132, -130, -128, -126, -125, -123, -121, -119, -118, -116, -114, -112, -110, -109, -107, -105, -103, -102, -100, -98, -96, -94, -93, -91, -89, -87, -86, -84, -82, -80, -79, -77, -75, -73, -71, -70, -68, -66, -64, -63, -61, -59, -57, -55, -54, -52, -50, -48, -47, -45, -43, -41, -40, -38, -36, -34, -32, -31, -29, -27, -25, -24, -22, -20, -18, -16, -15, -13, -11, -9, -8, -6, -4, -2, 0, 1, 3, 5, 7, 8, 10, 12, 14, 15, 17, 19, 21, 23, 24, 26, 28, 30, 31, 33, 35, 37, 39, 40, 42, 44, 46, 47, 49, 51, 53, 54, 56, 58, 60, 62, 63, 65, 67, 69, 70, 72, 74, 76, 78, 79, 81, 83, 85, 86, 88, 90, 92, 93, 95, 97, 99, 101, 102, 104, 106, 108, 109, 111, 113, 115, 117, 118, 120, 122, 124, 125, 127, 129, 131, 133, 134, 136, 138, 140, 141, 143, 145, 147, 148, 150, 152, 154, 156, 157, 159, 161, 163, 164, 166, 168, 170, 172, 173, 175, 177, 179, 180, 182, 184, 186, 187, 189, 191, 193, 195, 196, 198, 200, 202, 203, 205, 207, 209, 211, 212, 214, 216, 218, 219, 221, 223, 225 };
    FILE *fp;
    png_structp png_ptr = NULL;
    png_infop info_ptr = NULL;
    std::string path;
    GenFileName(path, file_path_, mLocal_);
    fp = fopen(path.c_str(), "wb");
    if (fp){
        png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL, NULL, NULL);
        info_ptr = png_create_info_struct(png_ptr);
        if(png_ptr == NULL || info_ptr == NULL){
            fclose(fp);
            return false;
        }
        mFileName_ = path;
        png_init_io(png_ptr, fp);
        png_set_IHDR(png_ptr, info_ptr, width, height, 8, PNG_COLOR_TYPE_RGB_ALPHA,PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
        png_write_info(png_ptr, info_ptr);
        png_set_packing(png_ptr);
        uint8_t * data = new uint8_t [width << 2];
        memset(data,255,width << 2);
        int nv_start = width * height ;
        uint32_t rgb_index = 0,pos;
        int r, g, b, nv_index,y, u, v;
        for(uint32_t i = 0; i < height ; ++i){
            for(uint32_t j = 0; j < width; ++j){
                nv_index = (i >> 1) * width + j - (j & 1) + nv_start;
                y = yuv_data[rgb_index];
                u = yuv_data[nv_index];
                v = yuv_data[nv_index + 1];
                r = y + Table_fv1[v]; //r
                g = y - Table_fu1[u] - Table_fv2[v]; //g
                b = y + Table_fu2[u]; //b
                if(r > 255) r = 255;
                if(g > 255) g = 255;
                if(b > 255) b = 255;
                if(r < 0 ) r = 0;
                if(g < 0 ) g = 0;
                if(b < 0 ) b = 0;
                pos = j << 2;
                data[pos ] = b;
                data[pos + 1] = g;
                data[pos + 2] = r;
                ++rgb_index;
            }
            png_write_row(png_ptr,data);
        }
        delete data;
        png_write_end(png_ptr, info_ptr);
        png_destroy_write_struct(&png_ptr, &info_ptr);
        fclose(fp);
        return true;
    }
    return false;
}

多消耗内存,将png_write_row改成png_write_image,也没啥改善,时间主要都消耗在png_write_image的操作上了,而不是从YUV转换为RGB。。。

bool CompressNV21ToPng(int width, int height, uint8_t * yuv_data)
{
    const static int Table_fv1[256]={ -180, -179, -177, -176, -174, -173, -172, -170, -169, -167, -166, -165, -163, -162, -160, -159, -158, -156, -155, -153, -152, -151, -149, -148, -146, -145, -144, -142, -141, -139, -138, -137, -135, -134, -132, -131, -130, -128, -127, -125, -124, -123, -121, -120, -118, -117, -115, -114, -113, -111, -110, -108, -107, -106, -104, -103, -101, -100, -99, -97, -96, -94, -93, -92, -90, -89, -87, -86, -85, -83, -82, -80, -79, -78, -76, -75, -73, -72, -71, -69, -68, -66, -65, -64, -62, -61, -59, -58, -57, -55, -54, -52, -51, -50, -48, -47, -45, -44, -43, -41, -40, -38, -37, -36, -34, -33, -31, -30, -29, -27, -26, -24, -23, -22, -20, -19, -17, -16, -15, -13, -12, -10, -9, -8, -6, -5, -3, -2, 0, 1, 2, 4, 5, 7, 8, 9, 11, 12, 14, 15, 16, 18, 19, 21, 22, 23, 25, 26, 28, 29, 30, 32, 33, 35, 36, 37, 39, 40, 42, 43, 44, 46, 47, 49, 50, 51, 53, 54, 56, 57, 58, 60, 61, 63, 64, 65, 67, 68, 70, 71, 72, 74, 75, 77, 78, 79, 81, 82, 84, 85, 86, 88, 89, 91, 92, 93, 95, 96, 98, 99, 100, 102, 103, 105, 106, 107, 109, 110, 112, 113, 114, 116, 117, 119, 120, 122, 123, 124, 126, 127, 129, 130, 131, 133, 134, 136, 137, 138, 140, 141, 143, 144, 145, 147, 148, 150, 151, 152, 154, 155, 157, 158, 159, 161, 162, 164, 165, 166, 168, 169, 171, 172, 173, 175, 176, 178 };
    const static int Table_fv2[256]={ -92, -91, -91, -90, -89, -88, -88, -87, -86, -86, -85, -84, -83, -83, -82, -81, -81, -80, -79, -78, -78, -77, -76, -76, -75, -74, -73, -73, -72, -71, -71, -70, -69, -68, -68, -67, -66, -66, -65, -64, -63, -63, -62, -61, -61, -60, -59, -58, -58, -57, -56, -56, -55, -54, -53, -53, -52, -51, -51, -50, -49, -48, -48, -47, -46, -46, -45, -44, -43, -43, -42, -41, -41, -40, -39, -38, -38, -37, -36, -36, -35, -34, -33, -33, -32, -31, -31, -30, -29, -28, -28, -27, -26, -26, -25, -24, -23, -23, -22, -21, -21, -20, -19, -18, -18, -17, -16, -16, -15, -14, -13, -13, -12, -11, -11, -10, -9, -8, -8, -7, -6, -6, -5, -4, -3, -3, -2, -1, 0, 0, 1, 2, 2, 3, 4, 5, 5, 6, 7, 7, 8, 9, 10, 10, 11, 12, 12, 13, 14, 15, 15, 16, 17, 17, 18, 19, 20, 20, 21, 22, 22, 23, 24, 25, 25, 26, 27, 27, 28, 29, 30, 30, 31, 32, 32, 33, 34, 35, 35, 36, 37, 37, 38, 39, 40, 40, 41, 42, 42, 43, 44, 45, 45, 46, 47, 47, 48, 49, 50, 50, 51, 52, 52, 53, 54, 55, 55, 56, 57, 57, 58, 59, 60, 60, 61, 62, 62, 63, 64, 65, 65, 66, 67, 67, 68, 69, 70, 70, 71, 72, 72, 73, 74, 75, 75, 76, 77, 77, 78, 79, 80, 80, 81, 82, 82, 83, 84, 85, 85, 86, 87, 87, 88, 89, 90, 90 };
    const static int Table_fu1[256]={ -44, -44, -44, -43, -43, -43, -42, -42, -42, -41, -41, -41, -40, -40, -40, -39, -39, -39, -38, -38, -38, -37, -37, -37, -36, -36, -36, -35, -35, -35, -34, -34, -33, -33, -33, -32, -32, -32, -31, -31, -31, -30, -30, -30, -29, -29, -29, -28, -28, -28, -27, -27, -27, -26, -26, -26, -25, -25, -25, -24, -24, -24, -23, -23, -22, -22, -22, -21, -21, -21, -20, -20, -20, -19, -19, -19, -18, -18, -18, -17, -17, -17, -16, -16, -16, -15, -15, -15, -14, -14, -14, -13, -13, -13, -12, -12, -11, -11, -11, -10, -10, -10, -9, -9, -9, -8, -8, -8, -7, -7, -7, -6, -6, -6, -5, -5, -5, -4, -4, -4, -3, -3, -3, -2, -2, -2, -1, -1, 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 11, 11, 11, 12, 12, 12, 13, 13, 13, 14, 14, 14, 15, 15, 15, 16, 16, 16, 17, 17, 17, 18, 18, 18, 19, 19, 19, 20, 20, 20, 21, 21, 22, 22, 22, 23, 23, 23, 24, 24, 24, 25, 25, 25, 26, 26, 26, 27, 27, 27, 28, 28, 28, 29, 29, 29, 30, 30, 30, 31, 31, 31, 32, 32, 33, 33, 33, 34, 34, 34, 35, 35, 35, 36, 36, 36, 37, 37, 37, 38, 38, 38, 39, 39, 39, 40, 40, 40, 41, 41, 41, 42, 42, 42, 43, 43 };
    const static int Table_fu2[256]={ -227, -226, -224, -222, -220, -219, -217, -215, -213, -212, -210, -208, -206, -204, -203, -201, -199, -197, -196, -194, -192, -190, -188, -187, -185, -183, -181, -180, -178, -176, -174, -173, -171, -169, -167, -165, -164, -162, -160, -158, -157, -155, -153, -151, -149, -148, -146, -144, -142, -141, -139, -137, -135, -134, -132, -130, -128, -126, -125, -123, -121, -119, -118, -116, -114, -112, -110, -109, -107, -105, -103, -102, -100, -98, -96, -94, -93, -91, -89, -87, -86, -84, -82, -80, -79, -77, -75, -73, -71, -70, -68, -66, -64, -63, -61, -59, -57, -55, -54, -52, -50, -48, -47, -45, -43, -41, -40, -38, -36, -34, -32, -31, -29, -27, -25, -24, -22, -20, -18, -16, -15, -13, -11, -9, -8, -6, -4, -2, 0, 1, 3, 5, 7, 8, 10, 12, 14, 15, 17, 19, 21, 23, 24, 26, 28, 30, 31, 33, 35, 37, 39, 40, 42, 44, 46, 47, 49, 51, 53, 54, 56, 58, 60, 62, 63, 65, 67, 69, 70, 72, 74, 76, 78, 79, 81, 83, 85, 86, 88, 90, 92, 93, 95, 97, 99, 101, 102, 104, 106, 108, 109, 111, 113, 115, 117, 118, 120, 122, 124, 125, 127, 129, 131, 133, 134, 136, 138, 140, 141, 143, 145, 147, 148, 150, 152, 154, 156, 157, 159, 161, 163, 164, 166, 168, 170, 172, 173, 175, 177, 179, 180, 182, 184, 186, 187, 189, 191, 193, 195, 196, 198, 200, 202, 203, 205, 207, 209, 211, 212, 214, 216, 218, 219, 221, 223, 225 };
    FILE *fp;
    png_structp png_ptr = NULL;
    png_infop info_ptr = NULL;
    std::string path;
    GenFileName(path, file_path_, mLocal_);
    fp = fopen(path.c_str(), "wb");
    if (fp){
        png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL, NULL, NULL);
        info_ptr = png_create_info_struct(png_ptr);
        if(png_ptr == NULL || info_ptr == NULL){
            fclose(fp);
            return false;
        }
        mFileName_ = path;
        png_init_io(png_ptr, fp);
        png_set_IHDR(png_ptr, info_ptr, width, height, 8, PNG_COLOR_TYPE_RGB_ALPHA,PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
        png_write_info(png_ptr, info_ptr);
        png_set_packing(png_ptr);
        uint32_t size = height * (width << 2);
        uint8_t * data = new uint8_t [size];
        png_bytepp row_pointers = new png_bytep[height];
        memset(data,255,size);
        int nv_start = width * height ;
        uint32_t rgb_index = 0,pos,row_pos;
        int r, g, b, nv_index,y, u, v;
        for(uint32_t i = 0; i < height ; ++i){
            row_pos = (i * width) << 2;
            for(uint32_t j = 0; j < width; ++j){
                nv_index = (i >> 1) * width + j - (j & 1) + nv_start;
                y = yuv_data[rgb_index];
                u = yuv_data[nv_index];
                v = yuv_data[nv_index + 1];
                r = y + Table_fv1[v]; //r
                g = y - Table_fu1[u] - Table_fv2[v]; //g
                b = y + Table_fu2[u]; //b
                if(r > 255) r = 255;
                if(g > 255) g = 255;
                if(b > 255) b = 255;
                if(r < 0 ) r = 0;
                if(g < 0 ) g = 0;
                if(b < 0 ) b = 0;
                pos = row_pos + (j << 2);
                data[pos ] = b;
                data[pos + 1] = g;
                data[pos + 2] = r;
                ++rgb_index;
            }
            row_pointers[i] = data + row_pos;
        }
        png_write_image(png_ptr,row_pointers);
        delete data;
        delete row_pointers;
        png_write_end(png_ptr, info_ptr);
        png_destroy_write_struct(&png_ptr, &info_ptr);
        fclose(fp);
        return true;
    }
    return false;
}

libpng编码是不是比较耗时?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值