1. NodeBox 1
    1. Homepage
    2. NodeBox 3Node-based app for generative design and data visualization
    3. NodeBox OpenGLHardware-accelerated cross-platform graphics library
    4. NodeBox 1Generate 2D visuals using Python code (Mac OS X only)
  2. Gallery
  3. Documentation
  4. Forum
  5. Blog

Simple Bitmapper

Posted by Duane Bailey on Dec 28, 2007



I made a simple bitmapper, using the coreimage support. Keep in mind it's VERY slow. Damn python :P

you pass it a 1d array of Colors, a width and height, and it gives you a layer back.

import Numeric
from AppKit import *
coreimage = ximport("coreimage")
canvas = coreimage.canvas(WIDTH, HEIGHT)
 
width = 50#0 # I wouldn't uncomment this
height = 50#0 # or this
# it took about thirty seconds to render
# python is damn slow :P
 
d = [color(0.0, 0.0, 0.0)] * width * height
 
colormode(HSB)
for x in range(width):
    c = color(float(x + 1) / width, 1.0, 1.0)
    print width, x, c
    for y in range(height):
        d[(y * width) + x] = c
colormode(RGB)
 
def better_layer_bytes(color_data, w, h, x=None, y=None, name=''):
    data = Numeric.array([0] * len(color_data) * 4, typecode='c')
    for i in range(len(color_data)):
        data[(i*4)] = color_data[i].r * 255
        data[(i*4)+1] = color_data[i].g * 255
        data[(i*4)+2] = color_data[i].b * 255
        data[(i*4)+3] = color_data[i].a * 255
    data = (data, None, None, None, None)
    img = NSBitmapImageRep.alloc().initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bytesPerRow_bitsPerPixel_(data, w, h, 8, 4, True, False, NSDeviceRGBColorSpace, w * 4, 32)
    img = img.TIFFRepresentation()
    return canvas.layer_bytes(img, w=w, h=h, x=x, y=y, name=name)
 
l = better_layer_bytes(d, width, height, x=width, y=height)
 
canvas.draw(0, 0)


 
Posted by Tom De Smedt on Dec 30, 2007

Hi Duane,

Great work! I'll integrate the code into the Core Image library.