Skip to content

Commit

Permalink
implement cvEncodeImage
Browse files Browse the repository at this point in the history
  • Loading branch information
nakkaya committed Apr 5, 2011
1 parent b969c27 commit 3fd374e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 86 deletions.
91 changes: 33 additions & 58 deletions resources/lib/vision.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,72 +11,47 @@ int* image_size(void* m){
return vals;
}

// 1 - BGR
// 2 - BINARY
// 3 - HSV
// 4 - RGB
// 5 - GRAYSCALE
int* pixels(void* m, int type){
IplImage* img = (IplImage*)m;
int encoded_image_size(void* m){
CvMat* mat = (CvMat*)m;
return mat->cols;
}

int* vals = malloc(img->height * img->width * sizeof(int));
int i,j;
int index = 0;

int release = 0;
if(type == 3){
IplImage* tmp = cvCreateImage(cvGetSize((IplImage*)img), img->depth, img->nChannels);
cvCvtColor(img, tmp, CV_HSV2BGR);
img = tmp;
release = 1;
type = 1;
}else if(type == 4){
IplImage* tmp = cvCreateImage(cvGetSize((IplImage*)img), img->depth, img->nChannels);
cvCvtColor(img, tmp, CV_RGB2BGR);
img = tmp;
release = 1;
type = 1;
}else if(type == 5){
IplImage* tmp = cvCreateImage(cvGetSize((IplImage*)img), img->depth, 3);
cvCvtColor(img, tmp, CV_GRAY2BGR);
img = tmp;
release = 1;
type = 1;
}
char* encoded_image_rerieve(void* m){
CvMat* buf = (CvMat*)m;
char* ret = malloc(buf->cols * sizeof(char));

if(type == 1){
for (i = 0; i < img->height; i++){
for (j = 0; j < img->width; j++){
int col;
int k;

unsigned char red = CV_IMAGE_ELEM(img, uchar, i, (j)*3+2);
unsigned char green = CV_IMAGE_ELEM(img, uchar, i, (j)*3+1);
unsigned char blue = CV_IMAGE_ELEM(img, uchar, i, (j)*3);
for(col = 0, k= 0; col < buf->cols; col++ , k++) {
char* ptr = (char*)(buf->data.ptr + col);
ret[k] = ptr[0];
}

vals[index++] =
((255 & 0xFF) << 24) | //alpha
(((int)red & 0xFF) << 16) |
(((int)green & 0xFF) << 8) |
(((int)blue & 0xFF) << 0);
}
}
}else if(type == 2){
for (i = 0; i < img->height; i++){
for (j = 0; j < img->width; j++){
cvReleaseMat(&buf);
return ret;
}

uchar pixel = CV_IMAGE_ELEM(img, uchar, i, j);
void* encode_image(void* m, int ext, int comp){
IplImage* img = (IplImage*)m;
int params[3];
char* type;

if(pixel == 0)
vals[index++] = 0xFF000000;
else
vals[index++] = 0xFFFFFFFF;
}
}
}
params[1] = comp;
params[2] = 0;

if(release == 1)
cvReleaseImage(&img);
switch(ext) {
case 1:
params[0] = CV_IMWRITE_PNG_COMPRESSION;
type = ".png";
break;
case 2:
params[0] = CV_IMWRITE_JPEG_QUALITY;
type = ".jpeg";
break;
}

return vals;
return (void*)cvEncodeImage(type, img, params);
}

void* capture_from_cam(int i){
Expand Down
47 changes: 19 additions & 28 deletions src/vision/core.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(ns vision.core
(:use [clojure.contrib.def :only [defmacro-]])
(:import (com.sun.jna Function Pointer)
(com.sun.jna.ptr ByReference IntByReference FloatByReference)
(com.sun.jna.ptr ByReference IntByReference FloatByReference ByteByReference)
(java.awt.image BufferedImage DirectColorModel Raster DataBufferInt)))

(defrecord IplImage [#^Pointer pointer
Expand Down Expand Up @@ -49,33 +49,21 @@
(defmethod release ByReference [p]
(call :release_memory [p]))

(defmulti image-size
(defn image-size
"Get image width, height."
class)

(defmethod image-size Pointer [p]
(with-pointer [p (call :image_size IntByReference [p])]
(seq (.getIntArray p 0 2))))

(defmethod image-size IplImage [{p :pointer}]
(with-pointer [p (call :image_size IntByReference [p])]
(seq (.getIntArray p 0 2))))

(defn- pixels [p t]
(with-pointer [ptr (call :pixels IntByReference [p t])]
(let [[width height] (image-size p)]
(.getIntArray ptr 0 (* width height)))))

(defn- buffered-image [pxs t]
(delay
(let [[width height] (image-size pxs)
pxs (pixels pxs t)
masks (int-array [0xFF0000 0xFF00 0xFF])]
(BufferedImage.
(DirectColorModel. 32 (first masks) (second masks) (last masks))
(Raster/createPackedRaster
(DataBufferInt. pxs (* width height)) width height width masks nil)
false nil))))
[{p :pointer}]
(with-pointer [p (call :image_size IntByReference [p])]
(seq (.getIntArray p 0 2))))

(defn encode-image [{p :pointer} ext comp]
(let [ext (cond (= :png ext) 1
(= :jpg ext) 2
(= :jpeg ext) 2
:default (throw (Exception. "Unknown extension.")))
mat (call :encode_image Pointer [p ext comp])
size (call :encoded_image_size Integer [mat])]
(with-pointer [ptr (call :encoded_image_rerieve ByteByReference [mat])]
(.getByteArray ptr 0 size))))

(defn- ipl-image [ref cs]
(let [type (cond (= cs :bgr) 1
Expand All @@ -84,7 +72,10 @@
(= cs :rgb) 4
(= cs :grayscale) 5
:default (throw (Exception. "Unknown Color Space.")))]
(IplImage. ref cs (buffered-image ref type))))
(IplImage. ref cs (delay
(javax.imageio.ImageIO/read
(java.io.ByteArrayInputStream.
(encode-image {:pointer ref} :jpg 80)))))))

(defn load-image
"Loads an image from file
Expand Down

0 comments on commit 3fd374e

Please sign in to comment.