2024-04-02 13:10:04 -04:00
.. meta ::
:description: Using rocJPEG
:keywords: parse JPEG, parse, decode, JPEG decoder, JPEG decoding, rocJPEG, AMD, ROCm
********************************************************************
Using rocJPEG
********************************************************************
2024-10-22 17:23:18 -04:00
This document provides a high-level overview of how to do common operations using the rocJPEG APIs exposed in the `` rocjpeg.h `` header file.
2024-04-02 13:10:04 -04:00
2024-10-22 17:23:18 -04:00
Creating handles
==================
2024-04-02 13:10:04 -04:00
2024-10-22 17:23:18 -04:00
Handles need to be created to decode and parse JPEG streams.
2024-04-02 13:10:04 -04:00
2024-10-22 17:23:18 -04:00
`` rocJpegCreate() `` returns an instance of a `` RocJpegHandle `` based on the specified backend and GPU device ID. The `` RocJpegHandle `` instance must be retained for the entire decode session.
2024-04-03 14:55:56 -04:00
.. code :: cpp
RocJpegStatus rocJpegCreate (
RocJpegBackend backend ,
int device_id ,
RocJpegHandle * handle ) ;
2024-10-22 17:23:18 -04:00
`` rocJpegStreamCreate() `` returns a `` rocJpegStreamHandle `` , which is a pointer used to represent an instance of a JPEG stream. The instance of `` rocJpegStreamHandle `` is used to parse the JPEG stream and to store the JPEG stream parameters.
2024-04-02 13:10:04 -04:00
2024-10-22 17:23:18 -04:00
.. code :: cpp
2024-05-13 16:11:15 -04:00
2024-10-22 17:23:18 -04:00
RocJpegStatus rocJpegStreamCreate ( RocJpegStreamHandle * jpeg_stream_handle ) ;
2024-05-13 16:11:15 -04:00
2024-10-22 17:23:18 -04:00
For example:
2024-05-13 16:11:15 -04:00
.. code :: cpp
2024-10-22 17:23:18 -04:00
// Read the JPEG image file
std : : ifstream input ( " mug_420.jpg " , std : : ios : : in | std : : ios : : binary | std : : ios : : ate ) ;
// Get the JPEG image file size
std : : streamsize file_size = input . tellg ( ) ;
input . seekg ( 0 , std : : ios : : beg ) ;
std : : vector < char > file_data ;
// resize if buffer is too small
if ( file_data . size ( ) < file_size ) {
file_data . resize ( file_size ) ;
}
// Read the JPEG stream
if ( ! input . read ( file_data . data ( ) , file_size ) ) {
std : : cerr < < " ERROR: cannot read from file: " < < std : : endl ;
return EXIT_FAILURE ;
}
// Initialize rocJPEG
RocJpegHandle handle ;
RocJpegStatus status = rocJpegCreate ( ROCJPEG_BACKEND_HARDWARE , 0 , & handle ) ;
if ( status ! = ROCJPEG_STATUS_SUCCESS ) {
std : : cerr < < " Failed to create rocJPEG handle with error code: " < < rocJpegGetErrorName ( status ) < < std : : endl ;
return EXIT_FAILURE ;
}
// Create a JPEG stream
RocJpegStreamHandle rocjpeg_stream_handle ;
status = rocJpegStreamCreate ( & rocjpeg_stream_handle ) ;
if ( status ! = ROCJPEG_STATUS_SUCCESS ) {
std : : cerr < < " Failed to create JPEG stream with error code: " < < rocJpegGetErrorName ( status ) < < std : : endl ;
rocJpegDestroy ( handle ) ;
return EXIT_FAILURE ;
}
2024-05-13 16:11:15 -04:00
2024-10-22 17:23:18 -04:00
`` rocJpegGetErrorName() `` returns error codes in text format from rocJPEG APIs.
2024-05-13 16:11:15 -04:00
2024-10-22 17:23:18 -04:00
Parsing a stream
=================
2024-05-13 16:11:15 -04:00
2024-10-22 17:23:18 -04:00
`` rocJpegStreamParse() `` is used to parse a JPEG stream.
2024-05-13 16:11:15 -04:00
2024-10-22 17:23:18 -04:00
The stream data buffer is passed through the `` data `` input parameter. The length of the buffer is passed through the `` length `` input parameter. The parsed stream is returned through the `` jpeg_stream_handle `` provided. `` jpeg_stream_handle `` must have already been created with `` rocJpegStreamCreate() `` .
2024-05-13 16:11:15 -04:00
.. code :: cpp
2024-10-22 17:23:18 -04:00
RocJpegStatus rocJpegStreamParse ( const unsigned char * data ,
size_t length ,
RocJpegStreamHandle jpeg_stream_handle ) ;
2024-05-13 16:11:15 -04:00
2024-10-22 17:23:18 -04:00
For example:
2024-05-13 16:11:15 -04:00
2024-10-22 17:23:18 -04:00
.. code :: cpp
// Parse the JPEG stream
status = rocJpegStreamParse ( reinterpret_cast < uint8_t * > ( file_data . data ( ) ) , file_size , rocjpeg_stream_handle ) ;
if ( status ! = ROCJPEG_STATUS_SUCCESS ) {
std : : cerr < < " Failed to parse JPEG stream with error code: " < < rocJpegGetErrorName ( status ) < < std : : endl ;
rocJpegStreamDestroy ( rocjpeg_stream_handle ) ;
rocJpegDestroy ( handle ) ;
return EXIT_FAILURE ;
}
Getting image information
===========================
2024-04-03 14:55:56 -04:00
2024-10-22 17:23:18 -04:00
`` rocJpegGetImageInfo() `` is used to retrieve the number of components, the chroma subsampling, and the width and height of the JPEG image.
2024-04-03 14:55:56 -04:00
.. code :: cpp
RocJpegStatus rocJpegGetImageInfo (
RocJpegHandle handle ,
2024-05-13 16:11:15 -04:00
RocJpegStreamHandle jpeg_stream_handle ,
2024-04-03 14:55:56 -04:00
uint8_t * num_components ,
RocJpegChromaSubsampling * subsampling ,
uint32_t * widths ,
uint32_t * heights ) ;
2024-10-22 17:23:18 -04:00
For more information on `` rocJpegGetImageInfo() `` , see `Retrieving image information with rocJPEG <./rocjpeg-retrieve-image-info.html> `_ .
2024-04-03 14:55:56 -04:00
2024-10-22 17:23:18 -04:00
Decoding a stream
====================
2024-04-02 13:10:04 -04:00
2024-10-22 17:23:18 -04:00
`` rocJpegDecode() `` takes the image passed to it through the `` jpeg_stream_handle `` input parameter and decodes it based on the backend used to create `` handle `` input parameter.
2024-04-03 14:55:56 -04:00
2024-10-22 17:23:18 -04:00
The `` decode_params `` input parameter is used to specify the decoding parameters. Memory needs to be allocated for each channel of the destination image.
2024-04-03 14:55:56 -04:00
.. code :: cpp
RocJpegStatus rocJpegDecode (
RocJpegHandle handle ,
2024-05-13 16:11:15 -04:00
RocJpegStreamHandle jpeg_stream_handle ,
2024-05-08 11:30:36 -04:00
const RocJpegDecodeParams * decode_params ,
2024-04-03 14:55:56 -04:00
RocJpegImage * destination ) ;
2024-10-22 17:23:18 -04:00
For more information on decoding streams, see `Decoding a JPEG stream with rocJPEG <./rocjpeg-decoding-a-jpeg-stream.html> `_ .
2024-04-03 14:55:56 -04:00
2024-10-22 17:23:18 -04:00
Destroying handles and freeing resources
==========================================
2024-04-03 14:55:56 -04:00
2024-10-22 17:23:18 -04:00
Once the JPEG stream is decoded, resources need to be freed.
2024-04-03 14:55:56 -04:00
2024-10-22 17:23:18 -04:00
Use |hipfree|_ to release the memory previously allocated by `` hipMalloc() `` for each channel of the destination `` rocJpegImage `` .
2024-04-03 14:55:56 -04:00
2024-10-22 17:23:18 -04:00
.. |hipfree| replace :: `` hipFree() ``
.. _hipfree: https://rocm.docs.amd.com/projects/HIP/en/latest/how-to/virtual_memory.html
2024-04-03 14:55:56 -04:00
2024-10-22 17:23:18 -04:00
Use `` rocJpegStreamDestroy() `` to release the `` rocJpegStreamHandle `` and its resources, and use `` rocJPegDestroy() `` to release `` RocJpegHandle `` and destroy the session.
2024-07-08 13:29:01 -04:00
.. code :: cpp
2024-10-22 17:23:18 -04:00
RocJpegStatus rocJpegStreamDestroy ( RocJpegStreamHandle jpeg_stream_handle )
2024-07-22 12:12:35 -04:00
2024-10-22 17:23:18 -04:00
RocJpegStatus rocJpegDestroy ( RocJpegHandle handle )
2024-07-22 12:12:35 -04:00
2024-10-22 17:23:18 -04:00
For example:
2024-07-22 12:12:35 -04:00
.. code :: cpp
2024-10-22 17:23:18 -04:00
hipFree ( ( void * ) output_image . channel [ 0 ] ) ;
hipFree ( ( void * ) output_image . channel [ 1 ] ) ;
rocJpegStreamDestroy ( rocjpeg_stream_handle ) ;
rocJpegDestroy ( handle ) ;