Skip to content

Commit

Permalink
Added blitting functions and generally improved things
Browse files Browse the repository at this point in the history
  • Loading branch information
bmcdorman committed Aug 10, 2012
1 parent 6488337 commit 4a4e3eb
Show file tree
Hide file tree
Showing 13 changed files with 208 additions and 53 deletions.
64 changes: 64 additions & 0 deletions CMakeLists.txt
@@ -0,0 +1,64 @@
#################################
# libkiss CMake Build File #
#################################

PROJECT(libkiss)

CMAKE_MINIMUM_REQUIRED(VERSION 2.6.0)

SET(INCLUDE ${libkiss_SOURCE_DIR}/include)
SET(SRC ${libkiss_SOURCE_DIR}/src)

INCLUDE_DIRECTORIES(${INCLUDE})

FILE(GLOB INCLUDES ${INCLUDE}/*.h ${SRC}/*.h)
SET(SOURCES
${SRC}/graphics_interface.c
${SRC}/kiss.c
${SRC}/kiss_graphics.c
${SRC}/kiss_graphics_draw.c
${SRC}/kiss_input.c
${SRC}/kiss_serial_buffer.c
${SRC}/kiss_util.c
${SRC}/serial_interface.c
${SRC}/thread_interface.c
${SRC}/timing_interface.c
)

IF(UNIX)
SET(SOURCES ${SOURCES}
${SRC}/kiss_serial_unix.c
)
ELSEIF(WIN32)
SET(SOURCES ${SOURCES}
${SRC}/kiss_serial_windows.c
)
ENDIF(UNIX)

SET(CMAKE_CXX_FLAGS "-Wall")

IF(APPLE)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -arch x86_64 -g")
ELSEIF(WIN32)
SET(CMAKE_CXX_FLAGS "-Wl,--enable-auto-import")
ELSEIF(UNIX)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
ENDIF()

SET(LIBRARY_OUTPUT_PATH ${libkiss_SOURCE_DIR}/lib)

ADD_LIBRARY(kiss STATIC ${SOURCES})
TARGET_LINK_LIBRARIES(kiss pthread glfw)

IF(APPLE)
FIND_LIBRARY(OPENGL_LIBRARY OpenGL)
target_link_libraries(kiss ${OPENGL_LIBRARY})
ELSE(APPLE)
target_link_libraries(kiss opengl)
ENDIF(APPLE)


install(FILES ${INCLUDES} DESTINATION include/kiss)
INSTALL(TARGETS kiss ARCHIVE DESTINATION lib)

add_subdirectory(test)
3 changes: 3 additions & 0 deletions include/kiss-graphics.h
Expand Up @@ -32,6 +32,9 @@ int graphics_init(int width, int height);
void graphics_quit();
void graphics_update();

void graphics_blit(unsigned char *data, int x, int y, int width, int height);
void graphics_blit_region(unsigned char *data, int sx, int sy, int ex, int ey, int width, int height, int dx, int dy);

void graphics_fill(int r, int g, int b);

void graphics_pixel(int x, int y, int r, int g, int b);
Expand Down
40 changes: 0 additions & 40 deletions kiss.pro

This file was deleted.

26 changes: 26 additions & 0 deletions src/graphics_interface.c
Expand Up @@ -58,6 +58,32 @@ void graphics_update()
kiss_unlock();
}

void graphics_blit(unsigned char *data, int x, int y, int width, int height)
{
if(!kiss_init())
return;

kiss_lock();
kiss_graphics_lock();
if(kiss_g_graphics_enabled)
kiss_graphics_blit(data, x, y, width, height);
kiss_graphics_unlock();
kiss_unlock();
}

void graphics_blit_region(unsigned char *data, int sx, int sy, int ex, int ey, int width, int height, int dx, int dy)
{
if(!kiss_init())
return;

kiss_lock();
kiss_graphics_lock();
if(kiss_g_graphics_enabled)
kiss_graphics_blit_region(data, sx, sy, ex, ey, width, height, dx, dy);
kiss_graphics_unlock();
kiss_unlock();
}

void graphics_fill(int r, int g, int b)
{
if(!kiss_init())
Expand Down
3 changes: 3 additions & 0 deletions src/graphics_interface.h
Expand Up @@ -25,6 +25,9 @@ int graphics_init(int width, int height);
void graphics_quit();
void graphics_update();

void graphics_blit(unsigned char *data, int x, int y, int width, int height);
void graphics_blit_region(unsigned char *data, int sx, int sy, int ex, int ey, int width, int height, int dx, int dy);

void graphics_fill(int r, int g, int b);

void graphics_pixel(int x, int y, int r, int g, int b);
Expand Down
2 changes: 1 addition & 1 deletion src/kiss.c
Expand Up @@ -141,7 +141,7 @@ void kiss_exec_op(int op, int arg1, int arg2)
kiss_g_op_arg2 = arg2;
kiss_g_op = op;

while(kiss_g_op);
while(kiss_g_op) usleep(1);
}

void kiss_lock()
Expand Down
13 changes: 8 additions & 5 deletions src/kiss_graphics.c
Expand Up @@ -45,15 +45,14 @@ void kiss_graphics_open_window(int width, int height)
else
kiss_g_graphics_info.texture_target=GL_TEXTURE_2D;

kiss_g_graphics_info.texture_format = GL_RGB;
kiss_g_graphics_info.texture_type = GL_UNSIGNED_BYTE;

if(major == 1 && minor == 1) {
printf("OpenGL Compatibility mode\n");
kiss_g_graphics_info.texture_format = GL_RGBA;
kiss_g_graphics_info.texture_type = GL_UNSIGNED_BYTE;
kiss_g_graphics_info.texture_wrap = GL_CLAMP;
}
else {
kiss_g_graphics_info.texture_format = GL_BGRA;
kiss_g_graphics_info.texture_type = GL_UNSIGNED_INT_8_8_8_8_REV;
kiss_g_graphics_info.texture_wrap = GL_CLAMP_TO_EDGE;
}

Expand Down Expand Up @@ -125,7 +124,7 @@ void kiss_graphics_create_buffer()
while(height > bheight) bheight *= 2;
}

kiss_g_graphics_info.data = malloc(bwidth*bheight*4);
kiss_g_graphics_info.data = malloc(bwidth * bheight * 3);

kiss_g_graphics_info.edge_buffer = (short**)malloc(height*sizeof(short*));
for(i = 0;i < height; i++)
Expand Down Expand Up @@ -182,6 +181,10 @@ void kiss_graphics_setup_gl()
glEnable(target);
glBindTexture(target, kiss_g_graphics_info.texture);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, kiss_g_graphics_info.width, 0, kiss_g_graphics_info.height, -1, 1);

return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/kiss_graphics.h
Expand Up @@ -39,7 +39,7 @@ struct graphics_info {
int texture_type;


uint32_t *data;
unsigned char *data;
int buffer_width;
int buffer_height;
float texture_width;
Expand Down
61 changes: 55 additions & 6 deletions src/kiss_graphics_draw.c
Expand Up @@ -23,6 +23,52 @@
#include <string.h>
#include <GL/GLee.h>

#define min(a,b) \
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a < _b ? _a : _b; })

inline unsigned char *pixel_ptr(unsigned char *data, unsigned int index)
{
return data + index *3;
}

inline unsigned char *kiss_pixel_ptr(unsigned int index)
{
return pixel_ptr(kiss_g_graphics_info.data, index);
}

inline void kiss_graphics_blit(unsigned char *data, int x, int y, int width, int height)
{
kiss_graphics_blit_region(data, 0, 0, width - 1, height - 1, width, height, x, y);
}

inline void kiss_graphics_blit_region(unsigned char *data, int sx, int sy, int ex, int ey, int width, int height, int dx, int dy)
{
ey = ey > height - 1 ? height - 1 : ey;

int i = 0;
const int hsize = ey - sy;
for(i = 0; i < hsize; ++i) {
int cols = ex - sx;
if(cols + dx > kiss_g_graphics_info.width) cols -= dx;
kiss_graphics_blit_section(data,
(dy + i) * kiss_g_graphics_info.width + dx,
(sy + i) * width + sx,
cols);
}
}

inline void kiss_graphics_blit_section(unsigned char *data, int index, int dindex, int length)
{
// Bounds check
int bufferSize = kiss_g_graphics_info.width * kiss_g_graphics_info.height;
int actualLength = length + dindex < bufferSize ? length : bufferSize - dindex;
if(actualLength <= 0) return;

memcpy(kiss_g_graphics_info.data + index * 3, data + dindex * 3, actualLength * 3);
}

inline void kiss_graphics_fill(int r, int g, int b)
{
int i, j;
Expand All @@ -38,13 +84,16 @@ inline void kiss_graphics_pixel(int x, int y, int r, int g, int b)
if(y < 0) return;
if(x >= kiss_g_graphics_info.width) return;
if(y >= kiss_g_graphics_info.height) return;

y = kiss_g_graphics_info.height - 1 - y;

if(kiss_g_graphics_info.texture_format == GL_BGRA)
kiss_g_graphics_info.data[x + y*kiss_g_graphics_info.buffer_width] = (r<<16) | (g<<8) | b;
else
kiss_g_graphics_info.data[x + y*kiss_g_graphics_info.buffer_width] = (b<<16) | (g<<8) | r;
unsigned char *data = kiss_pixel_ptr(x + y * kiss_g_graphics_info.buffer_width);
data[1] = g;
if(kiss_g_graphics_info.texture_format == GL_BGR) {
data[0] = b;
data[2] = r;
} else {
data[0] = r;
data[2] = b;
}
}

/* Bressenham algorithm drawn from: http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm */
Expand Down
4 changes: 4 additions & 0 deletions src/kiss_graphics_draw.h
Expand Up @@ -23,6 +23,10 @@

#include "kiss_graphics.h"

inline void kiss_graphics_blit(unsigned char *data, int x, int y, int width, int height);
inline void kiss_graphics_blit_region(unsigned char *data, int sx, int sy, int ex, int ey, int width, int height, int dx, int dy);
inline void kiss_graphics_blit_section(unsigned char *data, int index, int dindex, int length);

inline void kiss_graphics_fill(int r, int g, int b);
inline void kiss_graphics_pixel(int x, int y, int r, int g, int b);
inline void kiss_graphics_line(int x1, int y1, int x2, int y2, int r, int g, int b);
Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
@@ -0,0 +1 @@
add_subdirectory(camera)
2 changes: 2 additions & 0 deletions test/camera/CMakeLists.txt
@@ -0,0 +1,2 @@
ADD_EXECUTABLE(camera camera.c)
TARGET_LINK_LIBRARIES(camera kiss blobtastic)
40 changes: 40 additions & 0 deletions test/camera/camera.c
@@ -0,0 +1,40 @@
#include <kiss-graphics.h>
#include <kiss-input.h>
#include <blobtastic/camera.h>

void update()
{
camera_update();
graphics_blit(get_camera_frame(), 0, 0, get_camera_frame_width(), get_camera_frame_height());
graphics_update();
}

int main(int argc, char *argv[])
{
// Open the default camera
if(!camera_open()) {
printf("Unable to open camera. Is there one plugged in?\n");
return 1;
}

// Get the first frame
camera_update();

graphics_init(get_camera_frame_width(), get_camera_frame_height());

unsigned char ***myFrame;
myFrame = (unsigned char ***)get_camera_frame();

printf("R(100, 100) = %u\n", myFrame[100][100][0]);

while(!kiss_get_key_bit('A')) {
update();
}



graphics_quit();
camera_close();

return 0;
}

0 comments on commit 4a4e3eb

Please sign in to comment.