The CompressJPEG module is available from RISC OS 3.6 onwards. It provides SWIs with which you can compress raw image data into a JPEG image. It is a port of release 5 of the Independent JPEG Group's software.
The module is not in the RISC OS 3.6 ROM, but is instead held in the System application. If you wish to use the module in a program, you should first use the following command to ensure it is loaded:
RMEnsure CompressJPEG 0.00 RMLoad System:Modules.JCompMod
To compress raw image data into a JPEG image, you start by calling CompressJPEG_Start, which sets up the compression environment. You then compress each row of the source image with a separate call to CompressJPEG_WriteLine, or multiple rows at once with CompressJPEG_WriteLineExtended. Finally you finish the compression by calling CompressJPEG_Finish.
JPEG files encode colour pictures as YUV (Y = intensity, U and V are colour) data. Compressing involves the following steps:
(Incidentally, decompression involves reversing these steps.)
| R0 | = | pointer to buffer for JPEG data | ||||||||||||||||||||
| R1 | = | size of JPEG data buffer | ||||||||||||||||||||
| R2 | = | pointer to block of parameters:
| ||||||||||||||||||||
| R3 | = | pointer to workspace area, or 0 for the CompressJPEG module to allocate its own workspace from the RMA | ||||||||||||||||||||
| R4 | = | size of workspace area (if R3 <> 0) |
| R0 | = | JPEG tag, to be passed to other CompressJPEG SWIs |
| R1-3 | preserved | |
This call starts the JPEG compression process, setting up various parameters for it.
The buffer for the JPEG data should be as large as possible, since the JPEG compression routines cannot guarantee to compress the image by a fixed amount.
If you wish to supply your own workspace area, its required size for a colour (24 bit) image is:
20000 + ((image width rounded up to a multiple of 16) × 30)
and its required size for a greyscale (8bit) image is:
20000 + ((image width rounded up to a multiple of 16) × 9)
An error is returned if the workspace area becomes full.
| R0 | = | JPEG tag |
| R1 | = | pointer to row of pixels in form : R, G, B, R, G, B... (Red, Green, Blue, repeated) for colour images V, V, V, V, V, V... (grey values, repeated) for greyscale images |
| R0-1 | preserved | |
This call compresses one row of source pixels into the JPEG buffer. It should be called once for each row of the source data.
An error is returned if the JPEG buffer becomes full.
| R0 | = | JPEG tag |
| R0 | = | size of JPEG image written to buffer |
This call finishes the JPEG compression process, returning the size of the complete image. Any workspace claimed by the CompressJPEG module for the compression is released.
| R0 | = | JPEG tag | ||||||||
| R1 | = | flags :
| ||||||||
| R2 | = | pointer to comment | ||||||||
| R3 | = | length of comment, if bit 0 clear | ||||||||
| R0-3 | preserved | |
This call is used to add a comment to a JPEG as it is created. Comments should be added immediately after the call to CompressJPEG_Start. Comments may contain multiple lines by using LF as a separator, or any arbitrary binary data. Use of characters outside the range 32 - 126 is discouraged.
| R0 | = | JPEG tag | |||||||||||||||||||||||||
| R1 | = | flags :
| |||||||||||||||||||||||||
| R2 | = | pointer to image data to write | |||||||||||||||||||||||||
| R3 | = | pointer to palette data (for types 0-3) | |||||||||||||||||||||||||
| R4 | = | number of lines to write | |||||||||||||||||||||||||
| R5 | = | bytes between each line | |||||||||||||||||||||||||
| R0-1 | preserved | |
| R2 | = | pointer to end of image data written |
| R3 | preserved | |
| R4 | = | number of lines not written |
| R5 | preserved | |
This call is used to add multiple lines to a JPEG buffer, using a palette if required. It is intended that this call be used in preference to CompressJPEG_WriteLine where multiple source lines, or paletted source lines need compressing.
For Type 7 source data, the pixels should be supplied in the same form as expected by CompressJPEG_WriteLine (RGB,RGB,RGB, or V, V, V). No other flags take effect in this mode.
The pseudo-code below shows you how you might convert a 32 bpp sprite into a JPEG:
/* Pseudo C code for converting a 32bpp sprite to a JPEG */ JPEG_buffer = <Allocate buffer for JPEG>; workspace_buffer = <Allocate buffer for workspace>;
line_buffer = <Allocate buffer for line of source pixels>; argument_block arguments; arguments.width = sprite_width_in_pixels; arguments.height = sprite_height_in_pixels; arguments.quality = quality; arguments.components = 3; arguments.horizontal_dpi = 0; arguments.vertical_dpi = 0; sprite_pointer = start_of_data_within_sprite; JPEG_tag = CompressJPEG_Start(JPEG_buffer, JPEG_buffer_size, arguments, workspace_buffer, workspace_buffer_size); for loop = 1 to sprite_height_in_pixels { convert_sprite_data_to_rgb(sprite_pointer, line_buffer); CompressJPEG_WriteLine(JPEG_tag, line_buffer); sprite_pointer += sprite_width_in_words; } CompressJPEG_Finish(JPEG_tag);
| Maintainer(s): | RISCOS Ltd <developer@riscos.com> | ||||||||
|---|---|---|---|---|---|---|---|---|---|
| History: |
| ||||||||
| Disclaimer: |
Copyright © Pace Micro Technolgy plc, 2002. |