Cross-platform C SDK logo

Cross-platform C SDK

Pixel Buffer

❮ Back
Next ❯
This page has been automatically translated using the Google Translate API services. We are working on improving texts. Thank you for your understanding and patience.

Functions

Pixbuf*pixbuf_create (...)
Pixbuf*pixbuf_copy (...)
Pixbuf*pixbuf_convert (...)
Pixbuf*pixbuf_rotate (...)
Pixbuf*pixbuf_scale (...)
Pixbuf*pixbuf_convolution (...)
voidpixbuf_destroy (...)
uint32_tpixbuf_size (...)
uint32_tpixbuf_width (...)
uint32_tpixbuf_height (...)
pixformat_tpixbuf_format (...)
const byte_t*pixbuf_cdata (...)
byte_t*pixbuf_data (...)
uint32_tpixbuf_format_bpp (...)
uint32_tpixbuf_get (...)
voidpixbuf_set (...)
voidpixbuf_fill (...)
voidpixbuf_fill_rect (...)
voidpixbuf_line (...)
voidpixbuf_circle (...)

A pixel buffer is memory area that represents a grid of color dots or pixels. They allow direct access to information but are not optimized for drawing on the screen, so we must create an Image object to view them. They are very efficient for procedural generation or the application of filters, since reading or writing a value does not require more than accessing its position within the buffer.

All operations on pixel buffers are performed on the CPU. They are efficient to the extent that we directly access memory, but they cannot be compared with alternatives that use the GPU for digital image processing.

1. Pixel formats

The format refers to how the value of each pixel is encoded within the buffer (Table 1) (Figure 1).

  • Use pixbuf_format to get the pixel format.
  • Table 1: Pixel formats.
    Value Description
    ekRGB24 True color +16 million simultaneous, 24 bits per pixel.
    ekRGBA32 True color with alpha channel (transparencies), 32 bits per pixel.
    ekGRAY8 256 shades of gray, 8 bits per pixel.
    ekINDEX1 Indexed, 1 bit per pixel.
    ekINDEX2 Indexed, 2 bits per pixel.
    ekINDEX4 Indexed, 4 bits per pixel.
    ekINDEX8 Indexed, 8 bits per pixel.
    Different byte arrays with pixel data.
    Figure 1: (a) True color, (b) shades of gray, (c) indexed.

2. Procedural images

One way to "fill" buffers is through algorithms that calculate the value of each pixel. A clear example is found in the representation of fractal sets (Figure 2), an area of ​​mathematics dedicated to the study of certain dynamic systems. In Fractals you have the complete application.

  • Use pixbuf_data to get a pointer to the contents of the buffer.
  • Use pixbuf_set to write the value of a pixel.
  • Use pixbuf_get to read the value of a pixel.
  • Representation of a Julia set.
    Figure 2: Julia set. Pixel-pixel generated image using fractal algorithms.

3. Filters and convolution

Convolution is an operation on the image where the resulting value of each pixel is calculated based on its neighbors (Formula 1) (Figure 3). The operator is encoded by an array that scrolls sequentially through all points in the original buffer. The values ​​of the said matrix will determine the nature of the filter and develop in detail within the theory of digital image processing. In Filters you have an example application.

Convolution does not support indexed formats, because it averages between true color values. But it works with shades of gray.

4. Geometric transformations

  • Use pixbuf_rotate to rotate the buffer.
  • Use pixbuf_scale to stretch or narrow the image.
  • Operation of an image filter.
    Figure 4: Basic process of an image filter.

5. Drawing primitives


6. Copy and conversion

During the digital processing of an image, we may have to chain several operations, so it will be useful to be able to make copies of the buffers or format conversions.

  • Use pixbuf_copy to make a copy.
  • Use pixbuf_convert to convert to another format (Table 2).
  • Table 2: Conversion between formats.
    Source Destiny Observations
    RGB24 RGB32 Alpha channel is added with the value 255
    RGB32 RGB24 Alpha channel is removed with possible loss of information.
    RGB Gray RGB channels are weighted at a ratio of 77/255, 148/255, 30/255. Alpha channel is lost.
    Gray RGB RGB channels (gray, gray, gray) are duplicated. Alpha channel to 255.
    RGB Indexed If a palette is specified, the shortest distance between each pixel and the palette is calculated. If not, the Optimal palette is calculated. Possible loss of information.
    Indexed RGB A Predefined palette will be used if no other is specified.
    Indexed Indexed If the destination has a lower number of bits, out = in % bpp will be applied with possible loss of information.
    Gray Indexed The Gray8 format will be considered indexed for all purposes.
    Indexed Gray The Gray8 format will be considered indexed for all purposes.

pixbuf_create ()

Create a new pixel buffer.

Pixbuf*
pixbuf_create(const uint32_t width,
              const uint32_t height,
              const pixformat_t format);
width

Width.

height

Height.

format

Pixel format.

Return

The buffer pixel.

Remarks

Initial content will be undefined.


pixbuf_copy ()

Create a copy of the pixel buffer.

Pixbuf*
pixbuf_copy(const Pixbuf *pixbuf);
pixbuf

The original buffer.

Return

The copy.


pixbuf_convert ()

Change the format of a buffer pixel.

Pixbuf*
pixbuf_convert(const Pixbuf *pixbuf,
               const Palette *palette,
               const pixformat_t oformat);
pixbuf

The original buffer.

palette

Color palette required for certain conversions.

oformat

Result buffer format.

Return

The converted buffer.

Remarks

See Copy and conversion.


pixbuf_rotate ()

Create a buffer pixel by rotating an input buffer.

Pixbuf*
pixbuf_rotate(const Pixbuf *pixbuf,
              const real32_t cx,
              const real32_t cy,
              const real32_t angle,
              const bool_t clip,
              const uint32_t background);
pixbuf

The original buffer.

cx

X coordinate of the center of rotation (in original image).

cy

Coordinate y of the center of rotation (in original image).

angle

Radian angle.

clip

TRUE the resulting image will have the same dimensions as the original, losing part of the content. FALSE the resulting image will be resized to fit the entire original.

background

Background value with which to fill those pixels that do not correspond to any of the original.

Return

The new rotated buffer.

Remarks

See Geometric transformations.


pixbuf_scale ()

Create a pixel buffer by scaling an input buffer.

Pixbuf*
pixbuf_scale(const Pixbuf *pixbuf,
             const uint32_t nwidth,
             const uint32_t nheight);
pixbuf

The original buffer.

nwidth

The width of the result buffer.

nheight

The height of the result buffer.

Return

The new scaled buffer.

Remarks

See Geometric transformations.


pixbuf_convolution ()

Apply a convolution filter to the input buffer.

Pixbuf*
pixbuf_convolution(const Pixbuf *pixbuf,
                   const real32_t *matrix,
                   const uint32_t mwidth,
                   const uint32_t mheight,
                   const real32_t factor,
                   const real32_t bias);
pixbuf

The original buffer.

matrix

The convolution matrix.

mwidth

Number of columns in the array.

mheight

Number of rows in the array.

factor

Scale factor to apply to the result.

bias

Offset or offset to apply to the result.

Return

The new buffer with the result.

Remarks

See Filters and convolution.


pixbuf_destroy ()

Destroy the buffer.

void
pixbuf_destroy(Pixbuf **pixbuf);
pixbuf

The buffer. It will be set to NULL after the destruction.


pixbuf_size ()

Get the buffer size (in pixels).

uint32_t
pixbuf_size(const Pixbuf *pixbuf);
pixbuf

The buffer.

Return

Width x height.


pixbuf_width ()

Get the width of the buffer.

uint32_t
pixbuf_width(const Pixbuf *pixbuf);
pixbuf

The buffer.

Return

Width.


pixbuf_height ()

Get the height of the buffer.

uint32_t
pixbuf_height(const Pixbuf *pixbuf);
pixbuf

The buffer.

Return

Height.


pixbuf_format ()

Get the pixel format.

pixformat_t
pixbuf_format(const Pixbuf *pixbuf);
pixbuf

The buffer.

Return

The format.

Remarks

See Pixel formats.


pixbuf_cdata ()

Gets a read-only pointer to the contents of the buffer.

const byte_t*
pixbuf_cdata(const Pixbuf *pixbuf);
pixbuf

The buffer.

Return

Pointer to the first element.

Remarks

Correctly manipulating the buffer requires knowing the Pixel formats and sometimes using the operators at the bit level. Use pixbuf_get to correctly read a pixel.


pixbuf_data ()

Gets a read/write pointer to the contents of the buffer.

byte_t*
pixbuf_data(Pixbuf *pixbuf);
pixbuf

The buffer.

Return

Pointer to the first element.

Remarks

Correctly manipulating the buffer requires knowing the Pixel formats and sometimes using the operators at the bit level. Use pixbuf_get to correctly read a pixel.


pixbuf_format_bpp ()

Gets bits per pixel based on format.

uint32_t
pixbuf_format_bpp(const pixformat_t format);
format

The format.

Return

Bits per pixel.

Remarks

See Pixel formats.


pixbuf_get ()

Get the value of a pixel.

uint32_t
pixbuf_get(const Pixbuf *pixbuf,
           const uint32_t x,
           const uint32_t y);
pixbuf

The buffer.

x

Pixel x-coordinate.

y

Pixel y coordinate.

Return

The color value.

Remarks

See Pixel formats to correctly interpret the value.


pixbuf_set ()

Sets the value of a pixel.

void
pixbuf_set(Pixbuf *pixbuf,
           const uint32_t x,
           const uint32_t y,
           const uint32_t value);
pixbuf

The buffer.

x

Pixel x-coordinate.

y

Pixel y coordinate.

value

The color value.

Remarks

See Pixel formats to correctly interpret the value.


pixbuf_fill ()

Fill the entire buffer with the same value.

void
pixbuf_fill(Pixbuf *pixbuf,
            const uint32_t value);
pixbuf

The buffer.

value

The color value.

Remarks

See Drawing primitives.


pixbuf_fill_rect ()

Fill a rectangular region with the same value.

void
pixbuf_fill_rect(Pixbuf *pixbuf,
                 const uint32_t x,
                 const uint32_t y,
                 const uint32_t width,
                 const uint32_t height,
                 const uint32_t value);
pixbuf

The buffer.

x

X coordinate of upper-left corner.

y

Y coordinate of upper-left corner.

width

Rectangle width.

height

Rectangle height.

value

The color value.

Remarks

See Drawing primitives.


pixbuf_line ()

Draw a line, using the Bresenham algorithm.

void
pixbuf_line(Pixbuf *pixbuf,
            const uint32_t x0,
            const uint32_t y0,
            const uint32_t x1,
            const uint32_t y1,
            const uint32_t value);
pixbuf

The buffer.

x0

X coordinate of the first point.

y0

Y coordinate of the first point.

x1

X coordinate of the second point.

y1

Y coordinate of the second point.

value

The color value.

Remarks

See Drawing primitives.


pixbuf_circle ()

Draw a circle, using the Bresenham algorithm.

void
pixbuf_circle(Pixbuf *pixbuf,
              const uint32_t x,
              const uint32_t y,
              const uint32_t r,
              const uint32_t value);
pixbuf

The buffer.

x

X coordinate of the center.

y

Y coordinate of center.

r

Radius.

value

The color value.

Remarks

See Drawing primitives.

❮ Back
Next ❯