Table of Contents
- xtk_image.h
- Types
- Functions
- xtk_image_load_pixmap
- xtk_image_load_pixmap_from_memory
- xtk_image_load_rgba8_from_memory
- xtk_image_size_from_memory
- xtk_image_load_rgba8_scaled
- xtk_image_load_rgba8_scaled_from_memory
- xtk_image_pixmap_from_rgba8
- xtk_image_load_pixmap_scaled
- xtk_image_load_argb
- xtk_image_composite_argb
- xtk_image_free_argb
- xtk_image_load_rgba8
- xtk_image_argb_from_rgba8
- xtk_inflate_zlib
- xtk_image_scale_rgba8
- xtk_image_encode_png
- xtk_image_png_text
- xtk_deflate_zlib
xtk_image.h
Loads PNG, JPEG, GIF, BMP and XPM images as pixmaps, alpha pictures or raw pixels.
This module decodes raster image files with decoders of its own (no image library is needed) and gives the result in one of three forms:
- A flattened Pixmap, with
xtk_image_load_pixmap(),xtk_image_load_pixmap_scaled(),xtk_image_load_pixmap_from_memory()orxtk_image_pixmap_from_rgba8(). Transparent pixels are blended with a background color that the caller gives, and the result is an opaque Pixmap of the screen's default depth that is drawn withXCopyArea(). This is the cheapest form when the image always sits on one known flat color. Free it withXFreePixmap(). - A true alpha picture (
XtkImageARGB), withxtk_image_load_argb()orxtk_image_argb_from_rgba8(). The image keeps its alpha in a 32-bit XRender picture andxtk_image_composite_argb()blends it over whatever the drawable shows at that moment, so it stays correct when the theme or the background changes. It needs the XRender extension. Free it withxtk_image_free_argb(). - Raw RGBA8 pixels, with
xtk_image_load_rgba8(),xtk_image_load_rgba8_from_memory(),xtk_image_load_rgba8_scaled()andxtk_image_load_rgba8_scaled_from_memory(). These need no display, so a program can decode an untrusted file in a separate process and draw the pixels in the main one. Free them withfree().
Pixel layout. RGBA8 buffers hold 4 bytes for each pixel in the order R, G, B, A, row by row from the top, with straight (not premultiplied) alpha. xtk_svg.h and xtk_vector.h use the same layout.
Formats. The format is found from the first bytes of the data, not from the file name.
- PNG: all color types, bit depths 1 to 16, interlaced or not, with transparency from tRNS. Gamma and color profiles are ignored. A file with a chunk whose CRC is wrong is refused (the zlib checksum is not checked).
- JPEG: baseline and progressive, gray, YCbCr, CMYK and YCCK, turned upright by its EXIF orientation. Arithmetic coding, 12-bit samples, the other frame types, a file with two components or with a second frame header are not supported.
- GIF: 87a and 89a, the first frame only, with transparency and interlacing.
- BMP: uncompressed (BI_RGB) 8, 24 and 32 bits for each pixel, and RLE8 and RLE4. A file with BI_BITFIELDS, or with the old 12-byte OS/2 header, is refused. The fourth byte of a 32-bit BMP is not read as alpha.
- XPM: the text format, with
#rgb,#rrggbb,Noneand a few color names; another color name gives black.
Images larger than 64 megapixels, and files of 2 GB or more, are refused before they are decoded.
Example.
XtkImageARGB logo = {0};
int unsupported = 0;
int have_logo = xtk_image_load_argb(display, screen, "logo.png", &logo, &unsupported);
// in the Expose handler, after the background is drawn:
if (have_logo) xtk_image_composite_argb(display, screen, &logo, window, 10, 10);
// when the window closes:
if (have_logo) xtk_image_free_argb(display, &logo);
*
Limitations.
- The Pixmap functions need a TrueColor or DirectColor visual. On another visual they fail and print a warning to stderr.
- A Pixmap gets the depth of the drawable it is made for: the default visual's, or a TrueColor visual of the drawable's depth when that differs. Channels of any width are filled in full (a 30-bit visual gets 10 bits a channel).
xtk_image_composite_argb()draws into a window of any visual, or a pixmap of any depth that has a TrueColor visual. - Scaling down averages the pixels each new pixel covers (weighted by their alpha); scaling up, which only
xtk_image_scale_rgba8()does, picks the nearest pixel. The loaders never make an image larger than it is, and an image is never scaled up.
#include "xtk_image.h"
Types: XtkImageBackground, XtkImageARGB, XtkPngText
Functions: xtk_image_load_pixmap, xtk_image_load_pixmap_from_memory, xtk_image_load_rgba8_from_memory, xtk_image_size_from_memory, xtk_image_load_rgba8_scaled, xtk_image_load_rgba8_scaled_from_memory, xtk_image_pixmap_from_rgba8, xtk_image_load_pixmap_scaled, xtk_image_load_argb, xtk_image_composite_argb, xtk_image_free_argb, xtk_image_load_rgba8, xtk_image_argb_from_rgba8, xtk_inflate_zlib, xtk_image_scale_rgba8, xtk_image_encode_png, xtk_image_png_text, xtk_deflate_zlib
Types
XtkImageBackground
The flat color that transparent pixels are blended with when an image becomes an opaque Pixmap.
| Field | Type | Description |
|---|---|---|
r |
unsigned char |
Red, 0 to 255. |
g |
unsigned char |
Green, 0 to 255. |
b |
unsigned char |
Blue, 0 to 255. |
XtkImageARGB
An image with its alpha kept, as a 32-bit XRender picture.
Made by xtk_image_load_argb() or xtk_image_argb_from_rgba8(), drawn with xtk_image_composite_argb() and freed with xtk_image_free_argb(). The colors are stored premultiplied by alpha, as XRender expects.
| Field | Type | Description |
|---|---|---|
pixmap |
Pixmap |
The depth-32 pixmap that holds the pixels; freed by xtk_image_free_argb(). |
picture |
Picture |
The XRender picture of pixmap, in the standard ARGB32 format; freed by xtk_image_free_argb(). |
width |
int |
The width in pixels. |
height |
int |
The height in pixels. |
XtkPngText
A text chunk of a PNG file: a keyword (1 to 79 bytes, Latin-1) and its text.
| Field | Type | Description |
|---|---|---|
key |
const char * |
"Thumb::URI", "Software" ... |
value |
const char * |
The text, with no zero byte in it. |
Functions
xtk_image_load_pixmap
int xtk_image_load_pixmap(
Display *display,
int screen,
Drawable drawable,
const char *path,
XtkImageBackground background,
Pixmap *pixmap_out,
int *width_out,
int *height_out);
Loads an image file as a new opaque Pixmap at its own size.
Transparent and half-transparent pixels are blended with background. The Pixmap has the depth of drawable (usually the screen's default).
| Parameter | Description |
|---|---|
display |
The display connection. |
screen |
The screen number; its default visual decides the pixel format. |
drawable |
A drawable on that screen, used to create the Pixmap. |
path |
The image file. |
background |
The color that transparent pixels are blended with. |
pixmap_out |
Receives the new Pixmap. The caller frees it with XFreePixmap(). |
width_out |
Receives the width in pixels; may be NULL. |
height_out |
Receives the height in pixels; may be NULL. |
Returns: 1 when the Pixmap was made; 0 when an argument is NULL, the file cannot be read or decoded, the image is larger than 64 megapixels, or the default visual is not TrueColor or DirectColor. On 0 nothing is written.
xtk_image_load_pixmap_from_memory
int xtk_image_load_pixmap_from_memory(
Display *display,
int screen,
Drawable drawable,
const unsigned char *data,
int data_length,
XtkImageBackground background,
Pixmap *pixmap_out,
int *width_out,
int *height_out);
Loads an image from encoded bytes in memory as a new opaque Pixmap.
This works as xtk_image_load_pixmap() but reads the file's bytes from a buffer, for example data read from a database.
| Parameter | Description |
|---|---|
display |
The display connection. |
screen |
The screen number; its default visual decides the pixel format. |
drawable |
A drawable on that screen, used to create the Pixmap. |
data |
The encoded image (the bytes of a PNG, JPEG, GIF, BMP or XPM file). |
data_length |
The length of data in bytes. |
background |
The color that transparent pixels are blended with. |
pixmap_out |
Receives the new Pixmap. The caller frees it with XFreePixmap(). |
width_out |
Receives the width in pixels; may be NULL. |
height_out |
Receives the height in pixels; may be NULL. |
Returns: 1 when the Pixmap was made; 0 on the same failures as xtk_image_load_pixmap(), or when data_length is 0 or less.
xtk_image_load_rgba8_from_memory
int xtk_image_load_rgba8_from_memory(
const unsigned char *data,
int data_length,
unsigned char **rgba_out,
int *width_out,
int *height_out);
Decodes an image from encoded bytes in memory into RGBA8 pixels.
This works as xtk_image_load_rgba8() but reads the file's bytes from a buffer, for example an image compiled into the program as a byte array. It needs no display.
| Parameter | Description |
|---|---|
data |
The encoded image (the bytes of a PNG, JPEG, GIF, BMP or XPM file). |
data_length |
The length of data in bytes. |
rgba_out |
Receives a new buffer of width * height * 4 bytes. The caller frees it with free(). |
width_out |
Receives the width in pixels; may be NULL. |
height_out |
Receives the height in pixels; may be NULL. |
Returns: 1 when the pixels were written; 0 when data or rgba_out is NULL, data_length is 0 or less, the data cannot be decoded, or the image is larger than 64 megapixels. On 0 nothing is written.
xtk_image_size_from_memory
int xtk_image_size_from_memory(
const unsigned char *data,
int data_length,
int *width_out,
int *height_out);
Reads an image's size from its header, without decoding it.
The size the file says it has (a JPEG's before its EXIF orientation is applied): for deciding whether to decode it at all. Decoding may still fail.
| Parameter | Description |
|---|---|
data |
The encoded image (the bytes of a PNG, JPEG, GIF, BMP or XPM file). |
data_length |
The length of data in bytes. |
width_out |
Receives the width in pixels; may be NULL. |
height_out |
Receives the height in pixels; may be NULL. |
Returns: 1 when the size was read; 0 when the bytes are none of those formats or their header is cut short.
xtk_image_load_rgba8_scaled
int xtk_image_load_rgba8_scaled(
const char *path,
int max_width,
int max_height,
unsigned char **rgba_out,
int *width_out,
int *height_out);
Decodes an image file into RGBA8 pixels scaled down to fit a box.
The image keeps its aspect ratio and is never made larger than its own size. These are the pixels that xtk_image_load_pixmap_scaled() would use, but no display is needed: a program can decode a file it does not trust in a separate process and make the Pixmap in the main one with xtk_image_pixmap_from_rgba8().
| Parameter | Description |
|---|---|
path |
The image file. |
max_width |
The largest width allowed, in pixels; must be at least 1. |
max_height |
The largest height allowed, in pixels; must be at least 1. |
rgba_out |
Receives a new buffer of width * height * 4 bytes. The caller frees it with free(). |
width_out |
Receives the scaled width in pixels; may be NULL. |
height_out |
Receives the scaled height in pixels; may be NULL. |
Returns: 1 when the pixels were written; 0 when an argument is NULL or a maximum is less than 1, or on the failures of xtk_image_load_rgba8(). On 0 nothing is written.
xtk_image_load_rgba8_scaled_from_memory
int xtk_image_load_rgba8_scaled_from_memory(
const unsigned char *data,
int data_length,
int max_width,
int max_height,
unsigned char **rgba_out,
int *width_out,
int *height_out);
Decodes an image from encoded bytes in memory into RGBA8 pixels scaled down to fit a box.
This is xtk_image_load_rgba8_scaled() for a buffer instead of a file.
| Parameter | Description |
|---|---|
data |
The encoded image (the bytes of a PNG, JPEG, GIF, BMP or XPM file). |
data_length |
The length of data in bytes. |
max_width |
The largest width allowed, in pixels; must be at least 1. |
max_height |
The largest height allowed, in pixels; must be at least 1. |
rgba_out |
Receives a new buffer of width * height * 4 bytes. The caller frees it with free(). |
width_out |
Receives the scaled width in pixels; may be NULL. |
height_out |
Receives the scaled height in pixels; may be NULL. |
Returns: 1 when the pixels were written; 0 when an argument is NULL, a maximum is less than 1, or on the failures of xtk_image_load_rgba8_from_memory(). On 0 nothing is written.
xtk_image_pixmap_from_rgba8
Pixmap xtk_image_pixmap_from_rgba8(
Display *display,
int screen,
Drawable drawable,
const unsigned char *rgba,
int width,
int height,
XtkImageBackground background);
Makes a new opaque Pixmap from RGBA8 pixels.
Transparent and half-transparent pixels are blended with background, as xtk_image_load_pixmap() does. The pixels are only read.
| Parameter | Description |
|---|---|
display |
The display connection. |
screen |
The screen number; its default visual decides the pixel format. |
drawable |
A drawable on that screen, used to create the Pixmap. |
rgba |
The pixels: width * height * 4 bytes. |
width |
The width in pixels. |
height |
The height in pixels. |
background |
The color that transparent pixels are blended with. |
Returns: The new Pixmap, which the caller frees with XFreePixmap(); None when display or rgba is NULL, a size is 0 or less, the image is larger than 64 megapixels, or the default visual is not TrueColor or DirectColor.
xtk_image_load_pixmap_scaled
int xtk_image_load_pixmap_scaled(
Display *display,
int screen,
Drawable drawable,
const char *path,
XtkImageBackground background,
int max_width,
int max_height,
Pixmap *pixmap_out,
int *width_out,
int *height_out);
Loads an image file as a new opaque Pixmap scaled down to fit a box.
This works as xtk_image_load_pixmap(), but the image is scaled to fit max_width x max_height. It keeps its aspect ratio and is never made larger than its own size. Use the size it gives back for layout, not the size of the file.
| Parameter | Description |
|---|---|
display |
The display connection. |
screen |
The screen number; its default visual decides the pixel format. |
drawable |
A drawable on that screen, used to create the Pixmap. |
path |
The image file. |
background |
The color that transparent pixels are blended with. |
max_width |
The largest width allowed, in pixels; must be at least 1. |
max_height |
The largest height allowed, in pixels; must be at least 1. |
pixmap_out |
Receives the new Pixmap. The caller frees it with XFreePixmap(). |
width_out |
Receives the scaled width in pixels; may be NULL. |
height_out |
Receives the scaled height in pixels; may be NULL. |
Returns: 1 when the Pixmap was made; 0 when a maximum is less than 1 or on the failures of xtk_image_load_pixmap(). On 0 nothing is written.
xtk_image_load_argb
int xtk_image_load_argb(
Display *display,
int screen,
const char *path,
XtkImageARGB *out,
int *out_unsupported);
Loads an image file as a picture that keeps its alpha.
The colors of the half-transparent edge pixels are corrected from their opaque neighbors before upload, so that edges do not show a light or dark fringe.
| Parameter | Description |
|---|---|
display |
The display connection. |
screen |
The screen number; the pixmap is made on its root window. |
path |
The image file. |
out |
Receives the image. The caller frees it with xtk_image_free_argb(). It is not changed on failure. |
out_unsupported |
Receives 1 when the X server cannot hold alpha images at all (no XRender extension or no ARGB32 format), so no file will ever load; 0 in every other case, a bad file too, so the caller can try another image. May be NULL. |
Returns: 1 when the image was loaded; 0 when an argument is NULL, XRender is missing, the file cannot be read or decoded, or the image is larger than 64 megapixels.
xtk_image_composite_argb
void xtk_image_composite_argb(
Display *display,
int screen,
const XtkImageARGB *image,
Drawable dest,
int dest_x,
int dest_y);
Draws an alpha image over what a drawable shows now.
Each pixel is blended with the destination by its alpha (XRender's "over" operation). Because the blend happens at every call, the image is correct on any background. Call it where an XCopyArea() of a flattened Pixmap would go, usually after the background is drawn in the Expose handler. Nothing is drawn when display or image is NULL or the image has no picture.
| Parameter | Description |
|---|---|
display |
The display connection. |
screen |
The screen number. The destination must have this screen's default visual. |
image |
The image. |
dest |
The window or pixmap to draw into. |
dest_x |
The X position of the image's left edge in dest. |
dest_y |
The Y position of the image's top edge in dest. |
xtk_image_free_argb
void xtk_image_free_argb(Display *display, XtkImageARGB *image);
Frees an alpha image's picture and pixmap and sets them to None.
Calling it on an image that is all zeros, or twice, is allowed. The XtkImageARGB itself belongs to the caller.
| Parameter | Description |
|---|---|
display |
The display connection. NULL does nothing. |
image |
The image. NULL does nothing. |
xtk_image_load_rgba8
int xtk_image_load_rgba8(
const char *path,
unsigned char **rgba_out,
int *width_out,
int *height_out);
Decodes an image file into RGBA8 pixels at its own size.
Use it when the program needs the pixel values themselves, for example to set a window icon with _NET_WM_ICON. It needs no display.
| Parameter | Description |
|---|---|
path |
The image file. |
rgba_out |
Receives a new buffer of width * height * 4 bytes. The caller frees it with free(). |
width_out |
Receives the width in pixels; may be NULL. |
height_out |
Receives the height in pixels; may be NULL. |
Returns: 1 when the pixels were written; 0 when path or rgba_out is NULL, the file cannot be read or decoded, or the image is larger than 64 megapixels. On 0 nothing is written.
xtk_image_argb_from_rgba8
int xtk_image_argb_from_rgba8(
Display *display,
int screen,
const unsigned char *rgba,
int width,
int height,
XtkImageARGB *out);
Makes a picture that keeps its alpha from RGBA8 pixels.
Use it for pixels that do not come from a file, such as an SVG rendered with xtk_svg_render_memory() or pixels drawn with xtk_vector.h. The pixels are only read; the caller still owns them. Unlike xtk_image_load_argb(), the edge colors are not corrected.
| Parameter | Description |
|---|---|
display |
The display connection. |
screen |
The screen number; the pixmap is made on its root window. |
rgba |
The pixels: width * height * 4 bytes, straight alpha. |
width |
The width in pixels. |
height |
The height in pixels. |
out |
Receives the image. The caller frees it with xtk_image_free_argb(). It is not changed on failure. |
Returns: 1 when the image was made; 0 when an argument is NULL, a size is 0 or less, XRender or its ARGB32 format is missing, or memory runs out.
xtk_inflate_zlib
int xtk_inflate_zlib(
const unsigned char *source,
size_t source_length,
unsigned char *out,
size_t out_cap,
size_t *out_length);
Inflate a zlib stream (RFC 1950: its two-byte header, deflate data, and an Adler-32 that is not checked).
The decoder PNG pictures use, with every length and distance checked: for any other compressed data that comes from outside, such as a git repository's objects.
| Parameter | Description |
|---|---|
source |
The stream |
source_length |
Its length (more than the stream is fine) |
out |
Where the data goes |
out_cap |
Its size: data that would not fit is a failure |
out_length |
Set to the data's length |
Returns: 0, or -1 when it is no zlib stream, is broken, or does not fit
xtk_image_scale_rgba8
unsigned char *xtk_image_scale_rgba8(
const unsigned char *rgba,
int width,
int height,
int new_width,
int new_height);
Scales RGBA8 pixels to a new size.
Smaller: each new pixel is the average of the pixels it covers, weighted by their alpha, so a picture made small stays smooth and its transparent edges do not darken. Larger: the nearest pixel. The shape is the caller's: the pixels are stretched to exactly new_width x new_height.
| Parameter | Description |
|---|---|
rgba |
The pixels: width * height * 4 bytes, straight alpha. |
width |
Their width. |
height |
Their height. |
new_width |
The width wanted. |
new_height |
The height wanted. |
Returns: A new buffer of new_width * new_height * 4 bytes, freed with free(); NULL when an argument is NULL or a size is 0 or less, the result would be larger than 64 megapixels, or memory runs out.
xtk_image_encode_png
int xtk_image_encode_png(
const unsigned char *rgba,
int width,
int height,
const XtkPngText *texts,
int text_count,
unsigned char **png_out,
size_t *length_out);
Encodes RGBA8 pixels as a PNG file in memory, with text chunks.
Eight bits a channel, the alpha kept, each row filtered with the filter that suits it best and the whole compressed with xtk_deflate_zlib(). The text chunks (tEXt) come before the pixels, in the order given: what the freedesktop thumbnail cache needs (Thumb::URI, Thumb::MTime ...).
| Parameter | Description |
|---|---|
rgba |
The pixels: width * height * 4 bytes, straight alpha. |
width |
The width in pixels. |
height |
The height in pixels. |
texts |
The text chunks, or NULL when there are none. |
text_count |
How many. |
png_out |
Receives the file's bytes, freed with free(). |
length_out |
Receives how many. |
Returns: 1 when it was made; 0 when an argument is NULL or a size 0 or less, the image is larger than 64 megapixels, a keyword is empty or longer than 79 bytes, or memory runs out. On 0 nothing is written.
xtk_image_png_text
int xtk_image_png_text(
const unsigned char *png,
size_t length,
const char *key,
char *value,
size_t value_size);
Reads a text chunk (tEXt) of a PNG file in memory.
Every length is checked against the file's, so any bytes may be given.
| Parameter | Description |
|---|---|
png |
The file's bytes. |
length |
How many. |
key |
The keyword ("Thumb::MTime"). |
value |
Receives the text, ended by a zero byte. |
value_size |
Its size: a longer text is not read. |
Returns: 1 when the chunk was found and fits; 0 when it is not there, the bytes are no PNG file, or a chunk runs past the end.
xtk_deflate_zlib
int xtk_deflate_zlib(
const unsigned char *source,
size_t source_length,
unsigned char **out,
size_t *out_length);
Compress data as a zlib stream (RFC 1950): the reverse of xtk_inflate_zlib().
LZ77 over a 32 KB window with the fixed Huffman codes of DEFLATE, in one block, and an Adler-32 at the end: any zlib reader takes it.
| Parameter | Description |
|---|---|
source |
The data (may be NULL when source_length is 0) |
source_length |
Its length, under 2 GB |
out |
Receives the stream, freed with free() |
out_length |
Receives its length |
Returns: 0, or -1 when an argument is NULL, the data is too long or memory runs out
xtk API
Widgets
- button
- checkbox
- radiobutton
- label
- entry
- textview
- edit_history
- spinbutton
- slider
- progressbar
- combobox
- scrollbar
- swatch
- panel
- statusbar
- tooltip
- toolbar
- icongrid
- iconview
- pathbar
- treeview
- datagrid
- graph
- scheme_wheel
- zoom
- tray
Containers and menus
Dialogs
Rich text
Drawing and images
Color
Configuration and themes
Input and the X server
Safety
Data formats