Navigation C API Pages Python bindings Applications

Python Core module

The python binding maps mostly to the C API with the GP_ prefix stripped.

Structures like GP_Context are not created by the GP_ContextAlloc() function but have proper constructor and destructor to keep the Python reference counting happy.

There there are more complicated problems like GP_ProgressCallback which needs a proxy function to call the python callback from the C code.

Context

import gfxprim.core as core

    # Create 100x100 RGB888 context
    c = core.Context(100, 100, core.C.PIXEL_RGB888)

    print("w={} h={} bpp={}".format(c.w, c.h, c.bpp))

Creates a context of a particular size and pixel type.

First two parameters are width and height third is pixel type which is an enumeration

May raise OSError with ENOMEM errno if allocation has failed.

import gfxprim.core as core

    pixel = context.GetPixel(x, y)

Returns a pixel value at x and y. If coordinates are outside the image zero is returned.

import gfxprim.core as core

    context.PutPixel(x, y, pixel)

Puts a pixel at specified coordinates. If coordinates are outside of the image nothing is done.

Note You may want to see coordinate system description.
import gfxprim.core as core

    grayscale = context.Convert(core.C.PIXEL_G8)

Returns context converted into the desired pixel format.

The conversion is naive i.e. the values are just divided/multiplied.

import gfxprim.core as core

    # Blits context to target starting at
    # sx and sy in the source context
    # tx and ty in in the target
    context.Blit(sx, sy, target, tx, ty, w, h)

    # Alternatively the size can be described by
    # coordinates in the source or target
    context.Blit(sx, sy, target, tx, ty, sx2=, sy2=)
    context.Blit(sx, sy, target, tx, ty, tx2=, ty2=)

Copy a rectangle from self to target.

The blits can do simple conversions same as the Convert functions however such blits are slower.

Blit is clipped.

Tip See example Blit usage.

Colors and Pixels

Pixel in gfxprim is a number large enough to store a pixel value. Pixel is passed as a parameter to all drawing functions.

Color is a more abstract representation for example RGB triplet.

There are several functions to create a pixel value for a particular pixel type from color.

import gfxprim.core as core

    # You can create a pixel from RGB and pixel type
    black = core.RGBToPixel(0, 0, 0, core.C.PIXEL_G1)

    # Or using shortcut from context
    black = context.RGBToPixel(0, 0, 0)

These functions creates a pixel suitable for drawing into a bitmap with particular pixel type.

import gfxprim.core as core

    # Print all supported pixel types
    for i in core.PixelTypes:
            print("Pixel type '{}' size {}".format(i.name, i.size))

The PixelTypes array stores all supported pixel types

Progress Callback

Progress callback is a last parameter of loaders and filters. It can be either a python function or a touple with a function at the first position.

Progress callback must return an integer number. Returning non-zero will abort the operation and the function, called with the callback as a parameter, will return OSError with errno set to ECANCELED.

Debug Functions

import gfxprim.core as core

    core.SetDebugLevel(10)

    level = core.GetDebugLevel()

Sets and gets the GFXprim debug level. See debug messages description for more details.

These are basic Context methods from core module. Importing other modules will add some other (for example gfx module adds all drawing functions).