Skip to main content
LibGfx is the core graphics library providing 2D rendering primitives, image handling, color management, and font rendering for SerenityOS. It serves as the foundation for the entire graphical system.

Overview

LibGfx provides essential graphics functionality:
  • Bitmap Operations: Image creation, loading, and manipulation
  • Color Management: RGB, HSV, YUV, Oklab color spaces
  • 2D Rendering: Shapes, lines, gradients, and anti-aliasing
  • Font Rendering: TrueType, bitmap fonts, and text layout
  • Image Codecs: PNG, JPEG, GIF, BMP, and more
  • Transformations: Affine transformations and scaling

Core Types

Bitmap

The fundamental image representation.
class
Raster image data with multiple pixel formats.

Bitmap Formats

Pixel Access

method
Read pixel color at coordinates.
method
Set pixel color at coordinates.
method
Direct scanline access for performance.

Color Management

Color Class

class
32-bit ARGB color with extensive color space support.

Color Spaces

Uses ITU-R BT.1700 standard conversion.
Perceptually uniform color space for better gradients and mixing.

Color Operations

method
Alpha blend two colors.
method
Interpolate between colors.
method
Calculate perceived brightness.
method
WCAG contrast ratio calculation.

Painter

The main 2D rendering interface.
class
2D drawing operations on bitmaps.

Shape Drawing

Gradient Fills

Line Drawing

method
Draw a line between two points.
Line Styles:
  • LineStyle::Solid: Continuous line
  • LineStyle::Dotted: Dotted pattern
  • LineStyle::Dashed: Dashed pattern

Bezier Curves

Bitmap Operations

Blitting

method
Copy bitmap data with alpha blending.
method
Draw bitmap with scaling.
Scaling Modes:
  • NearestNeighbor: Fast, pixelated
  • BilinearBlend: Smooth interpolation
  • SmoothPixels: High-quality scaling

Transformations

Font Rendering

class
Abstract font interface for text rendering.

Text Drawing

Text Alignment:
  • TopLeft, TopCenter, TopRight
  • CenterLeft, Center, CenterRight
  • BottomLeft, BottomCenter, BottomRight
Text Elision:
  • None: No elision
  • Right: Add ellipsis at end
Text Wrapping:
  • DontWrap: Single line
  • Wrap: Multi-line wrapping

Image Format Support

LibGfx includes loaders for many image formats:
Supported Formats:
  • Raster: PNG, JPEG, GIF, BMP, TGA, TIFF, WebP
  • Advanced: JPEG2000, JBIG2, JXL (JPEG XL)
  • Other: QOI, IFF/LBM, DDS, ICO
  • Portable: PBM, PGM, PPM, PAM
  • Vector: TVG (TinyVG)

Advanced Features

Anti-Aliasing

class
High-quality anti-aliased rendering.
Location: LibGfx/AntiAliasingPainter.h

Affine Transformations

Location: LibGfx/AffineTransform.h

Color Utilities

Geometry Types

LibGfx provides geometric primitives:
  • IntPoint, FloatPoint: 2D points
  • IntSize, FloatSize: Dimensions
  • IntRect, FloatRect: Rectangles
  • Line: Line segments
  • Triangle: 2D triangles
  • Path: Complex vector paths

Style Painting

class
Classic UI widget rendering.Provides themed drawing for:
  • Buttons (normal, pressed, disabled)
  • Checkboxes and radio buttons
  • Progress bars
  • Tabs and frames
Location: LibGfx/ClassicStylePainter.h

Source Location

Directory: Userland/Libraries/LibGfx/ Key Files:
  • Bitmap.h: Core bitmap functionality
  • Color.h: Color representation and manipulation
  • Painter.h: 2D rendering operations
  • Font/Font.h: Font interface and rendering
  • AffineTransform.h: 2D transformations
  • AntiAliasingPainter.h: Anti-aliased drawing