Image Editing
Tizen offers various image processing features.
The main features of the ImageUtil API include the following:
-
Converting
You can change the image color space among the supported formats.
-
Resizing
You can change the image resolution.
-
Rotating
You can change the image angle around the X or Y axis.
-
Cropping
You can crop the outer parts of the image.
-
Decoding or encoding from a file or memory and encoding to a file or memory
You can decode images and encode them with the following formats:
- Bitmap formats: YUV420, YUV422, RGB888, RGBA8888, BGRA8888, and ARGB8888
- Input image formats for decoding: JPEG, PNG, GIF, BMP, WEBP, HEIF, and JPEG XL
- Output image formats for encoding: JPEG, PNG, GIF, BMP, WEBP, and JPEG XL
Note
You must pay attention to how the image quality depends on the size and compression ratio.
Prerequisites
To enable your application to use the image util functionality, follow these steps:
-
To find out the color spaces supported on the device, use ImageUtil.GetSupportedColorSpaces:
C#Copyforeach (var colorSpace in ImageUtil.GetSupportedColorSpaces(ImageFormat.Jpeg)) { ... } or var colorSpace = ImageUtil.GetSupportedColorSpaces(ImageFormat.Jpeg);
The supported color space will be one of the
Tizen.Multimedia.ColorSpace
enumeration. -
To transform image, create ImageTransformer and MediaPacket for image source:
C#Copyvar imageTransformer = new ImageTransformer(); var packet = MediaPacket.Create(new VideoMediaFormat(MediaFormatVideoMimeType.I420, 640, 480));
Convert image color space
To convert one color space of an image to another, follow these steps:
-
Create a ColorSpaceTransform instance:
C#Copyvar colorSpaceTransform = new ColorSpaceTransform(ColorSpace.I420);
or, the color space of target image can be set by
ColorSpace
property afterColorSpaceTransform
is created.C#CopycolorSpaceTransform.ColorSpace = ColorSpace.NV12;
-
Execute the transformation using
TransformAsync()
of ImageTransformer class:C#Copyvar resultMediaPacket = await imageTransformer.TransformAsync(packet, colorSpaceTransform);
Note
TransformAsync
usingColorSpaceTransform
only converts the color space. These functions do not change the image width, height, or any other image property. Due to these restrictions of the image processing library, not all color space combinations are supported for conversion.
Resize image
To resize an image, follow these steps:
-
Create a ResizeTransform instance:
C#Copyvar resizeTransform = new ResizeTransform(new Size(320, 240));
or, the size of target image can be set by
Size
property afterResizeTransform
is created.C#CopyresizeTransform.Size = new Size(1280, 730);
-
Execute the transformation using
TransformAsync()
of ImageTransformer class:C#Copyvar resultMediaPacket = await imageTransformer.TransformAsync(packet, resizeTransform);
Note
The image format has no effect on the transformation. If the color space is YUV, then the width and height of the target image must be multiples of eight. This restriction does not apply to the RGB images.
Rotate image
To rotate an image, follow these steps:
-
Create a RotateTransform instance:
C#Copyvar rotateTransform = new RotateTransform(Rotation.Rotate90);
or, the rotation of target image can be set by
Rotation
property afterRotateTransform
is created.C#CopyrotateTransform.Rotation = Rotation.Rotate180;
-
Execute the transformation using
TransformAsync()
of ImageTransformer class:C#Copyvar resultMediaPacket = await imageTransformer.TransformAsync(packet, rotateTransform);
Note
The image format has no effect on the transformation. If the color space is YUV, then the width and height of the target image must be multiples of eight. This restriction does not apply to the RGB images.
Crop image
To crop an image, follow these steps:
-
Create a CropTransform instance:
C#Copyvar cropTransform = new CropTransform(new Rectangle(0, 0, 100, 100));
or, the area to be cropped can be set by
Location
orSize
property afterCropTransform
is created.C#CopycropTransform.Location = new Point(10, 10); cropTransform.Size = new Size(200, 200);
-
Execute the transformation using
TransformAsync()
of ImageTransformer class:C#Copyvar resultMediaPacket = await imageTransformer.TransformAsync(packet, cropTransform);
Note
As there is a YUV restriction and the crop start position can be set arbitrarily, the cropped image width and height must be even.
Decode from a file or memory
To decode a JPEG, PNG, GIF, BMP, WEBP, HEIF, or JPEG XL image, follow these steps:
-
Create a decoder instance as image format:
C#Copyvar bmpDecoder = new BmpDecoder();
or
C#Copyvar pngDecoder = new PngDecoder();
or
C#Copyvar jpegDecoder = new JpegDecoder();
or
C#Copyvar gifDecoder = new GifDecoder();
or
C#Copyvar webPDecoder = new WebPDecoder();
or
C#Copyvar heifDecoder = new HeifDecoder();
or
C#Copyvar jpegXlDecoder = new JpegXlDecoder();
Copy -
Additionally, you can set the color space and JPEG downscale using
SetColorSpace()
method andDownscale
property:C#CopyjpegDecoder.SetColorSpace(ColorSpace.NV12); jpegDecoder.Downscale = JpegDownscale.OneHalf;
Note
Due to the decoder limitations, the color space setting is only supported for decoding the JPEG, the WEBP, the HEIF, and the JPEG XL images. The default color space is
ColorSpace.Rgba8888
. PNG, GIF, and BMP images are decoded withColorSpace.Rgba8888
. -
Execute the decoding using
DecodeAsync()
:C#Copyvar result = await jpegDecoder.DecodeAsync("input_file_path");
or
C#Copyvar sourceImage = new byte[100]; // set source image to sourceImage buffer var result = await jpegDecoder.DecodeAsync(sourceImage);
Encode to a file or memory
To encode a raw image, follow these steps:
-
Create an encoder instance as target image format:
C#Copyvar bmpEncoder = new BmpEncoder();
or
C#Copyvar pngEncoder = new PngEncoder(); var pngEncoder = new PngEncoder(PngCompression.Level1);
or
C#Copyvar jpegEncoder = new JpegEncoder(); var jpegEncoder = new JpegEncoder(95);
or
C#Copyvar gifEncoder = new GifEncoder();
or
C#Copyvar webPEncoder = new WebPEncoder(); var webPEncoder = new WebPEncoder(true);
or
C#Copyvar jpegXlEncoder = new JpegXlEncoder(); var jpegXlEncoder = new JpegXlEncoder(true);
-
Additionally, you can set the JPEG quality or PNG compression using
Quality
orCompression
property:C#CopyjpegEncoder.Quality = 90;
C#CopypngEncoder.Compression = PngCompression.Level3;
Note
The compression is only supported for the PNG images. The default JPEG quality is 75. The default PNG compression is
PngCompression.Level6
. -
Execute the encoding using
EncodeAsync()
:C#Copyvar inputImage = new byte[100]; // set source image to encode using (FileStream outStream = File.Create("output_path")) { await jpegEncoder.EncodeAsync(inputImage, outStream); }
Note
Due to the encoder limitations, the color space setting is only supported for encoding the JPEG, the WEBP, and the JPEG XL images. The default color space is
ColorSpace.Rgba8888
. PNG, GIF, and BMP images are encoded withColorSpace.Rgba8888
.
Supported color space formats
The following tables define the supported color space formats.
Table: RGB pixel formats
Label | FOURCC in hex | Bits per pixel | Description |
---|---|---|---|
RGB | 0x32424752 | 1, 4, 8, 16, 24, 32 | Alias for BI_RGB. |
RGBA | 0x41424752 | 16, 32 | Raw RGB with alpha. Sample precision and packing is arbitrary and determined using bit masks for each component, as for BI_BITFIELDS. |
Table: Packed YUV formats
Label | FOURCC in hex | Bits per pixel | Description |
---|---|---|---|
UYVY | 0x59565955 | 16 | YUV 4:2:2 (Y sample at every pixel, U and V sampled at every second pixel horizontally on each line). A macropixel contains 2 pixels in 1 u_int32. |
YUYV | 0x56595559 | 16 | Duplicate of YUY2. |
Table: Planar YUV formats
Label | FOURCC in hex | Bits per pixel | Description |
---|---|---|---|
YV16 | 0x36315659 | 16 | 8-bit Y plane followed by 8-bit 2x1 subsampled V and U planes. |
YV12 | 0x32315659 | 12 | 8-bit Y plane followed by 8-bit 2x2 subsampled V and U planes. |
I420 | 0x30323449 | 12 | 8-bit Y plane followed by 8-bit 2x2 subsampled U and V planes. |
NV12 | 0x3231564E | 12 | 8-bit Y plane followed by an interleaved U/V plane with 2x2 subsampling. |
NV21 | 0x3132564E | 12 | As NV12 with U and V reversed in the interleaved plane. |
Quality and size comparison
The following table shows the effect on the image quality and file sizes when using different compression ratios.
Table: Quality and size comparison
Image | Quality | Size (bytes) | Compression ratio | Description |
---|---|---|---|---|
Highest quality (Q = 100) | 83,261 | 2.6:1 | Extremely minor artifacts | |
High quality (Q = 50) | 15,138 | 15:1 | Initial signs of subimage artifacts | |
Medium quality (Q = 25) | 9,553 | 23:1 | Stronger artifacts; loss of high-frequency information | |
Low quality (Q = 10) | 4,787 | 46:1 | Severe high frequency loss; artifacts on subimage boundaries (“macroblocking”) are obvious | |
Lowest quality | - | - | - |
Related information
- Dependencies
- Tizen 4.0 and Higher