nvJPEG压缩图像

定义用到的变量

nvjpegHandle_t nvjpeg_handle;         // nvjpeg句柄
nvjpegEncoderState_t encoder_state;   // 
nvjpegEncoderParams_t encoder_params; // 

准备图像数据,输入数据指针应该是显存指针,每个颜色分量分别存储

nvjpegImage_t input;
nvjpegInputFormat_t input_format = NVJPEG_INPUT_BGR;

int image_width = 2448;
int image_height = 2048;
int channel_size = image_width * image_height;
for (int i = 0; i < 3; i++)
{
	input.pitch[i] = image_width;
	cudaMalloc((void**)&(input.channel[i]), channel_size);
	// set value of this channel
	cudaMemset(input.channel[i], 50 * 40 * i, channel_size);
}

准备环境

nvjpegBackend_t backend = NVJPEG_BACKEND_DEFAULT;
nvjpegCreate(backend, nullptr, &nvjpeg_handle));
nvjpegEncoderStateCreate(nvjpeg_handle, &encoder_state, NULL);
nvjpegEncoderParamsCreate(nvjpeg_handle, &encoder_params, NULL);

设置参数

nvjpegEncoderParamsSetEncoding(encoder_params, nvjpegJpegEncoding_t::NVJPEG_ENCODING_PROGRESSIVE_DCT_HUFFMAN, NULL);
nvjpegEncoderParamsSetOptimizedHuffman(encoder_params, 1, NULL);
nvjpegEncoderParamsSetQuality(encoder_params, 70, NULL);
nvjpegEncoderParamsSetSamplingFactors(encoder_params, nvjpegChromaSubsampling_t::NVJPEG_CSS_420, NULL);

执行压缩

nvjpegEncodeImage(nvjpeg_handle, encoder_state, encoder_params, &input, input_format, image_width, image_height, NULL)

取出二进制数据

std::vector<unsigned char> obuffer;
size_t length;
nvjpegEncodeRetrieveBitstream( nvjpeg_handle, encoder_state, NULL, &length, NULL);

obuffer.resize(length);
nvjpegEncodeRetrieveBitstream( nvjpeg_handle, encoder_state, obuffer.data(), &length, NULL));

保存二进制数据

std::ofstream outputFile("result.jpg", std::ios::out | std::ios::binary);
outputFile.write(reinterpret_cast<const char *>(obuffer.data()), static_cast<int>(length));

完整代码

int testEncodeImage()
{
	nvjpegHandle_t nvjpeg_handle;
	nvjpegEncoderState_t encoder_state;
	nvjpegEncoderParams_t encoder_params;

	cudaEvent_t ev_start, ev_end;
	CHECK_CUDA(cudaEventCreate(&ev_start));
	CHECK_CUDA(cudaEventCreate(&ev_end));

	size_t pdata_len = 1024 * 1024;
	unsigned char *pdata_dev = nullptr;
	CHECK_CUDA(cudaMalloc((void**)&pdata_dev, pdata_len));
	

	nvjpegImage_t input;
	nvjpegInputFormat_t input_format = NVJPEG_INPUT_BGR;
	int image_width = 2448;
	int image_height = 2048;
	int channel_size = image_width * image_height;

	for (int i = 0; i < 3; i++)
	{
		input.pitch[i] = image_width;
		CHECK_CUDA(cudaMalloc((void**)&(input.channel[i]), channel_size));
		CHECK_CUDA(cudaMemset(input.channel[i], 50 * 40 * i, channel_size));
	}

	nvjpegBackend_t backend = NVJPEG_BACKEND_DEFAULT;

	CHECK_NVJPEG(nvjpegCreate(backend, nullptr, &nvjpeg_handle));
	
	CHECK_NVJPEG(nvjpegEncoderParamsCreate(nvjpeg_handle, &encoder_params, NULL));
	CHECK_NVJPEG(nvjpegEncoderStateCreate(nvjpeg_handle, &encoder_state, NULL));

	// set params
	nvjpegEncoderParamsSetEncoding(encoder_params, nvjpegJpegEncoding_t::NVJPEG_ENCODING_PROGRESSIVE_DCT_HUFFMAN, NULL);
	nvjpegEncoderParamsSetOptimizedHuffman(encoder_params, 1, NULL);
	nvjpegEncoderParamsSetQuality(encoder_params, 70, NULL);
	nvjpegEncoderParamsSetSamplingFactors(encoder_params, nvjpegChromaSubsampling_t::NVJPEG_CSS_420, NULL);

	CHECK_CUDA(cudaEventRecord(ev_start));
	CHECK_NVJPEG(nvjpegEncodeImage(nvjpeg_handle, encoder_state, encoder_params, &input, input_format, image_width, image_height, NULL));
	CHECK_CUDA(cudaEventRecord(ev_end));

	std::vector<unsigned char> obuffer;
	size_t length;
	CHECK_NVJPEG(nvjpegEncodeRetrieveBitstream(
		nvjpeg_handle,
		encoder_state,
		NULL,
		&length,
		NULL));

	obuffer.resize(length);
	CHECK_NVJPEG(nvjpegEncodeRetrieveBitstream(
		nvjpeg_handle,
		encoder_state,
		obuffer.data(),
		&length,
		NULL));

	std::ofstream outputFile("a.jpg", std::ios::out | std::ios::binary);
	outputFile.write(reinterpret_cast<const char *>(obuffer.data()), static_cast<int>(length));

	cudaEventSynchronize(ev_end);

	float ms;
	cudaEventElapsedTime(&ms, ev_start, ev_end);
	std::cout << "time spend " << ms << " ms" << std::endl;
	
	return 0;
}

 时间消耗

2440*2048大小的RGB三色图像压缩耗时4.33ms,使用CPU单线程压缩耗时98ms(opencv)。

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ango_Cango

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值