Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Tencent Ncnn Mat Pixel Drawing

From Leeroopedia
Revision as of 16:49, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Tencent_Ncnn_Mat_Pixel_Drawing.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


Knowledge Sources
Domains Computer Vision, Image Processing, Rendering
Last Updated 2026-02-09 19:00 GMT

Overview

Implements a self-contained pixel drawing library for ncnn that renders rectangles, circles, lines, and anti-aliased text directly onto raw pixel buffers, supporting 1-4 channel formats and YUV420SP.

Description

This file provides drawing primitives for each channel count (c1, c2, c3, c4), each with both implicit-stride and explicit-stride overloads. All functions are guarded by the NCNN_PIXEL_DRAWING compile-time flag.

draw_rectangle supports filled rectangles (when thickness == -1) and outlined rectangles with configurable thickness by drawing top, bottom, left, and right edges separately. draw_circle uses the midpoint circle algorithm to compute circle boundary pixels, supporting both filled and outlined modes with thickness control. draw_line uses Bresenham's line algorithm with thickness support by drawing perpendicular offsets.

draw_text renders text by looking up each printable ASCII character in the embedded mono_font_data bitmap array (from mat_pixel_drawing_font.h, which contains 3859 lines of bitmap font data). Glyphs are resized to the requested font pixel size using bilinear interpolation (resize_bilinear_font), and each glyph pixel is alpha-blended onto the destination buffer. get_text_drawing_size calculates the bounding box dimensions of a text string at a given font size without drawing.

YUV420SP variants (draw_rectangle_yuv420sp, draw_circle_yuv420sp, draw_line_yuv420sp, draw_text_yuv420sp) decompose drawing into separate Y-plane and UV-plane operations for NV12/NV21 formatted camera frames.

All functions include boundary checking to safely handle clipping against image edges.

Usage

Use these functions to visualize detection results (bounding boxes, labels, landmark points) directly on pixel buffers without depending on external libraries like OpenCV. This is particularly valuable on embedded and mobile platforms where minimizing dependencies is important, and for debug visualization in headless environments.

Code Reference

Source Location

Signature

// Rectangle drawing (implicit stride, c1-c4)
void draw_rectangle_c1(unsigned char* pixels, int w, int h,
    int rx, int ry, int rw, int rh, unsigned int color, int thickness);
void draw_rectangle_c2(unsigned char* pixels, int w, int h,
    int rx, int ry, int rw, int rh, unsigned int color, int thickness);
void draw_rectangle_c3(unsigned char* pixels, int w, int h,
    int rx, int ry, int rw, int rh, unsigned int color, int thickness);
void draw_rectangle_c4(unsigned char* pixels, int w, int h,
    int rx, int ry, int rw, int rh, unsigned int color, int thickness);

// Rectangle drawing (explicit stride, c1-c4)
void draw_rectangle_c1(unsigned char* pixels, int w, int h, int stride,
    int rx, int ry, int rw, int rh, unsigned int color, int thickness);
void draw_rectangle_c3(unsigned char* pixels, int w, int h, int stride,
    int rx, int ry, int rw, int rh, unsigned int color, int thickness);

// Circle drawing (implicit stride, c1-c4)
void draw_circle_c1(unsigned char* pixels, int w, int h,
    int cx, int cy, int radius, unsigned int color, int thickness);
void draw_circle_c3(unsigned char* pixels, int w, int h,
    int cx, int cy, int radius, unsigned int color, int thickness);

// Circle drawing (explicit stride, c1-c4)
void draw_circle_c1(unsigned char* pixels, int w, int h, int stride,
    int cx, int cy, int radius, unsigned int color, int thickness);

// Line drawing (implicit stride, c1-c4)
void draw_line_c1(unsigned char* pixels, int w, int h,
    int x0, int y0, int x1, int y1, unsigned int color, int thickness);
void draw_line_c3(unsigned char* pixels, int w, int h,
    int x0, int y0, int x1, int y1, unsigned int color, int thickness);

// Line drawing (explicit stride, c1-c4)
void draw_line_c1(unsigned char* pixels, int w, int h, int stride,
    int x0, int y0, int x1, int y1, unsigned int color, int thickness);

// Text drawing (implicit stride, c1-c4)
void draw_text_c1(unsigned char* pixels, int w, int h,
    const char* text, int x, int y, int fontpixelsize, unsigned int color);
void draw_text_c3(unsigned char* pixels, int w, int h,
    const char* text, int x, int y, int fontpixelsize, unsigned int color);

// Text drawing (explicit stride, c1-c4)
void draw_text_c1(unsigned char* pixels, int w, int h, int stride,
    const char* text, int x, int y, int fontpixelsize, unsigned int color);

// Text size measurement
void get_text_drawing_size(const char* text, int fontpixelsize, int* w, int* h);

// YUV420SP variants
void draw_rectangle_yuv420sp(unsigned char* yuv420sp, int w, int h,
    int rx, int ry, int rw, int rh, unsigned int color, int thickness);
void draw_circle_yuv420sp(unsigned char* yuv420sp, int w, int h,
    int cx, int cy, int radius, unsigned int color, int thickness);
void draw_line_yuv420sp(unsigned char* yuv420sp, int w, int h,
    int x0, int y0, int x1, int y1, unsigned int color, int thickness);
void draw_text_yuv420sp(unsigned char* yuv420sp, int w, int h,
    const char* text, int x, int y, int fontpixelsize, unsigned int color);

Import

#include "mat.h"

I/O Contract

Inputs

Name Type Required Description
pixels unsigned char* Yes Target pixel buffer to draw on
w int Yes Image width in pixels
h int Yes Image height in pixels
stride int No Row stride in bytes (defaults to w * channels)
rx, ry, rw, rh int For rectangles Rectangle origin (x,y) and size (w,h)
cx, cy, radius int For circles Circle center and radius
x0, y0, x1, y1 int For lines Line start and end points
text const char* For text ASCII text string to render
x, y int For text Text origin position
fontpixelsize int For text Font size in pixels
color unsigned int Yes Color value packed as bytes per channel
thickness int Yes (except text) Line thickness; -1 means filled

Outputs

Name Type Description
pixels / yuv420sp unsigned char* The pixel buffer is modified in-place with the drawn primitive
w, h (get_text_drawing_size) int* Output width and height of the rendered text bounding box

Usage Examples

Drawing Detection Results on a 3-Channel Image

#include "mat.h"

// Draw a bounding box (red for RGB)
unsigned int red = 0x000000FF; // R channel in lowest byte
ncnn::draw_rectangle_c3(pixels, width, height, 50, 50, 100, 80, red, 2);

// Draw a filled circle at center of the box
ncnn::draw_circle_c3(pixels, width, height, 100, 90, 5, red, -1);

// Draw a label above the box
ncnn::draw_text_c3(pixels, width, height, "person 0.95", 50, 35, 16, red);

// Measure text size before drawing
int tw, th;
ncnn::get_text_drawing_size("person 0.95", 16, &tw, &th);

Drawing on YUV420SP Camera Preview

#include "mat.h"

// Draw directly on NV21 camera frame
ncnn::draw_rectangle_yuv420sp(yuv_data, cam_w, cam_h,
    box_x, box_y, box_w, box_h, 0x00FF00FF, 2);
ncnn::draw_text_yuv420sp(yuv_data, cam_w, cam_h,
    "detected", box_x, box_y - 20, 12, 0x00FF00FF);

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment