纯C++超分辨率重建DRRN --改编--(四)使用libjpeg加载图像和矩阵乘法并行化(多线程)

由于DRRN运行太慢,用OpenMP加速最简单易行,只要加一句 #pragma omp parallel for 就可以了

但是vs2008提示缺少一个链接Lib ,而mingw32 可以运行该句,所以改成mingw32来编译

easyX则改成libjpeg来加载jpg图。

按照《截图并使用libjpeg库压缩BMP为JPG与将JPG转换为BMP》一文中的方法修改一下来使用

用bmp_data来过渡一下:

struct bmp_data
{
	int		width;    //宽
	int     height;   //高
	int     depth;		  // 深度 非通道(混合RGB)
	unsigned char * data;
};

载入jpg 到内存 bmp_data:

/*
** 载入jpg 到内存 bmp_data
*/
struct bmp_data DeCompressJPGtobmp_data(FILE* input_file)
{
    struct jpeg_decompress_struct cinfo;
    struct jpeg_error_mgr jerr;
    JSAMPARRAY buffer;

    unsigned char *src_buff;
    unsigned char *point;

    cinfo.err = jpeg_std_error(&jerr);    //以下为libjpeg函数,具体参看相关文档
    jpeg_create_decompress(&cinfo);
    jpeg_stdio_src(&cinfo, input_file);
    jpeg_read_header(&cinfo, TRUE);
    jpeg_start_decompress(&cinfo);

    unsigned long width = cinfo.output_width;
    unsigned long height = cinfo.output_height;
    unsigned short depth = cinfo.output_components;

    src_buff = new unsigned char[width*height*depth];
    memset(src_buff, 0, sizeof(unsigned char)*width*height*depth);

    buffer = (*cinfo.mem->alloc_sarray)
        ((j_common_ptr)&cinfo, JPOOL_IMAGE, width*depth, 1);

    point = src_buff;
    while (cinfo.output_scanline < height)
    {
        jpeg_read_scanlines(&cinfo, buffer, 1);    //读取一行jpg图像数据到buffer
        memcpy(point, *buffer, width*depth);    //将buffer中的数据逐行给src_buff
        point += width*depth;            //一次改变一行
    }


    jpeg_finish_decompress(&cinfo);
    jpeg_destroy_decompress(&cinfo);

	struct bmp_data bmp;
	bmp.width = (int)width;   //宽
	bmp.height = (int)height;   //高
	bmp.depth = (int)depth;		  //通道 深度
	bmp.data = src_buff;


	return bmp;
}

 从内存 bmp_data 保存到 jpg 文件:

/*
** 压缩 BMP 图片为 JPG 图片
** 如果要进行对 JPG 图片的清晰度的调整,调宏 JPEG_QUALITY 的值即可,越大越清晰
*/
#define JPEG_QUALITY 80 // 根据这个值,来调整.jpg画质的清晰度
int CompressBMPtoJPG(char *filename, unsigned char *bits, int width, int height, int depth)
{
    struct jpeg_compress_struct cinfo;
    struct jpeg_error_mgr jerr;
    FILE * outfile;                 //target file   
    JSAMPROW row_pointer[1];        //pointer to JSAMPLE row[s]   
    int     row_stride;             //physical row width in image buffer   
    cinfo.err = jpeg_std_error(&jerr);
    jpeg_create_compress(&cinfo);

    if ((outfile = fopen(filename, "wb")) == NULL)
    {
        fprintf(stderr, "can't open %s/n", filename);
        return -1;
    }
    jpeg_stdio_dest(&cinfo, outfile);
    cinfo.image_width = width;      //image width and height, in pixels   
    cinfo.image_height = height;
    cinfo.input_components = 3;         //# of color components per pixel   
    cinfo.in_color_space = JCS_RGB;         //colorspace of input image   
    jpeg_set_defaults(&cinfo);
    jpeg_set_quality(&cinfo, JPEG_QUALITY, TRUE);//limit to baseline-JPEG values   
    jpeg_start_compress(&cinfo, TRUE);

    row_stride = width * depth; // JSAMPLEs per row in image_buffer  
	JSAMPLE * image_buffer=(JSAMPLE *)bits;
    while (cinfo.next_scanline < cinfo.image_height)
    {
		row_pointer[0] = & image_buffer[cinfo.next_scanline * row_stride];
		(void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
    }
    jpeg_finish_compress(&cinfo);
    fclose(outfile);
    jpeg_destroy_compress(&cinfo);
    return 0;
}


//保存 内存 bmp_data 到 jpg 文件
void bmp_dataToJpg(struct bmp_data & bmp,char* jpgname)
{
	if(bmp.depth==3)
		CompressBMPtoJPG(jpgname, bmp.data, bmp.width,bmp.height , 3);
{

其它一些也要修改,就不一一说明了

i686-w64-mingw32-g++ LoadSaveJpg.cpp nnconv.cpp vl_nnconv.cpp DRRN.cpp -o DRRN -ljpeg -fopenmp -O2 来编译。

下载地址:

win32位编译的超分辨率重建 DRRN(多核支持)

https://download.csdn.net/download/juebai123/10734284

 

你可能感兴趣的:(超分辨率重建)