Last active
June 12, 2023 08:39
-
-
Save louisswarren/8b6597f16187dedb642804b7078178a3 to your computer and use it in GitHub Desktop.
ctypes example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from ctypes import * | |
import sys | |
class Colour(Structure): | |
_fields_ = [(c, c_uint8) for c in "rgb"] | |
foo = cdll.LoadLibrary('./image.so') | |
# foo.setup_image | |
foo.add_point.argtypes = (c_int, c_int, Colour) | |
foo.colour.argtypes = (c_uint8, c_uint8, c_uint8) | |
foo.colour.restype = Colour | |
# foo.write_image | |
foo.setup_image() | |
# We can define colours either using the C function or the above class | |
red = foo.colour(255, 0, 0) | |
blue = Colour(0, 0, 255) | |
for i in range(10, 50): | |
for j in range(10, 50): | |
if foo.add_point(i, j, red): | |
print("Failed add point", file=sys.stderr) | |
for i in range(30, 70): | |
for j in range(30, 70): | |
if foo.add_point(i, j, blue): | |
print("Failed add point", file=sys.stderr) | |
if foo.write_image(): | |
print("Failed to write image", file=sys.stderr) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdint.h> | |
#include <stdio.h> | |
struct colour { | |
uint8_t r, g, b; | |
} image[80][80]; | |
void | |
setup_image(void) | |
{ | |
for (int y = 0; y < 80; y++) { | |
for (int x = 0; x < 80; x++) { | |
image[x][y].r = 0; | |
image[x][y].g = 0; | |
image[x][y].b = 0; | |
} | |
} | |
} | |
int | |
add_point(int x, int y, struct colour p) | |
{ | |
if (x < 0 || x >= 80 || y < 0 || y >= 80) | |
return 1; | |
image[x][y] = p; | |
return 0; | |
} | |
struct colour | |
colour(int r, int g, int b) | |
{ | |
return (struct colour){(uint8_t)r, (uint8_t)g, (uint8_t)b}; | |
} | |
int | |
write_image(void) | |
{ | |
int r = 0 > printf("P3\n%d %d\n%d\n", 80, 80, 255); | |
for (int y = 0; y < 80; y++) { | |
for (int x = 0; x < 80; x++) { | |
r |= 0 > printf("%d %d %d ", | |
image[x][y].r, | |
image[x][y].g, | |
image[x][y].b); | |
} | |
printf("\n"); | |
} | |
return r; | |
} | |
void | |
say_hello(int x) | |
{ | |
printf("Hello: %d\n", x); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.PHONY: default | |
default: out.png | |
%.png: %.ppm | |
convert $< $@ | |
out.ppm: draw.py image.so | |
python3 $< > $@ | |
image.so: image.c | |
$(CC) $(CFLAGS) $(LDFLAGS) -fPIC -shared -o $@ $< | |
.PHONY: clean | |
clean: | |
rm -f image.so out.png out.ppm |
Author
louisswarren
commented
Jun 12, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment