Permalink
Browse files

added read raw memory to ccv format support

  • Loading branch information...
1 parent 656f3c3 commit df5025e9705e27b7c7d51a3387060d0026c79881 @liuliu committed Oct 1, 2012
Showing with 725 additions and 44 deletions.
  1. +2 −2 bin/swtvldtr.rb
  2. +5 −4 lib/ccv.h
  3. +32 −5 lib/ccv_io.c
  4. +16 −6 lib/ccv_memory.c
  5. +332 −26 lib/io/_ccv_io_raw.c
  6. +337 −0 test/functional/io.tests.c
  7. +1 −1 test/functional/makefile
View
@@ -10,7 +10,7 @@
center_diff_thr = 1.0
truth = Hash.new
-file = File.new(ARGV[0])
+file = File.new ARGV[0]
filename = nil
file.each do |line|
if line =~ /-{0,1}\d+\s-{0,1}\d+\s-{0,1}\d+\s-{0,1}\d+/
@@ -23,7 +23,7 @@
end
estimate = Hash.new
-file = File.new(ARGV[1])
+file = File.new ARGV[1]
file.each do |line|
if line =~ /-{0,1}\d+\s-{0,1}\d+\s-{0,1}\d+\s-{0,1}\d+/
estimate[filename] = Array.new unless estimate.has_key? filename
View
@@ -56,9 +56,10 @@ enum {
};
enum {
- CCV_GARBAGE = 0x80000000, // matrix is in cache (not used by any functions)
- CCV_REUSABLE = 0x40000000, // matrix can be recycled
- CCV_UNMANAGED = 0x20000000, // matrix is allocated by user, therefore, cannot be freed by ccv_matrix_free/ccv_matrix_free_immediately
+ CCV_GARBAGE = 0x80000000, // matrix is in cache (not used by any functions)
+ CCV_REUSABLE = 0x40000000, // matrix can be recycled
+ CCV_UNMANAGED = 0x20000000, // matrix is allocated by user, therefore, cannot be freed by ccv_matrix_free/ccv_matrix_free_immediately
+ CCV_NO_DATA_ALLOC = 0x10000000, // matrix is allocated as header only, but with no data section, therefore, you have to free the data section separately
};
typedef union {
@@ -298,7 +299,7 @@ enum {
CCV_IO_UNKNOWN,
};
-int ccv_read_impl(const char* in, ccv_dense_matrix_t** x, int type, int rows, int cols, int scanline);
+int ccv_read_impl(const void* in, ccv_dense_matrix_t** x, int type, int rows, int cols, int scanline);
#define ccv_read_n(in, x, type, rows, cols, scanline, ...) \
ccv_read_impl(in, x, type, rows, cols, scanline)
#define ccv_read(in, x, type, ...) \
View
@@ -74,8 +74,28 @@ static int _ccv_read_raw(ccv_dense_matrix_t** x, void* data, int type, int rows,
if (type & CCV_IO_NO_COPY)
{
// there is no conversion that we can apply if it is NO_COPY mode
- assert((type && 0xFF) == CCV_IO_ANY_RAW);
- *x = ccv_dense_matrix_new(rows, cols, type, data, 0);
+ // NO_COPY mode generate an "unreusable" matrix, which requires you to
+ // manually release its data block (which is, in fact the same data
+ // block you passed in)
+ int ctype = CCV_8U | CCV_C1;
+ switch (type & 0xFF)
+ {
+ case CCV_IO_RGB_RAW:
+ case CCV_IO_BGR_RAW:
+ ctype = CCV_8U | CCV_C3;
+ break;
+ case CCV_IO_RGBA_RAW:
+ case CCV_IO_ARGB_RAW:
+ case CCV_IO_BGRA_RAW:
+ case CCV_IO_ABGR_RAW:
+ ctype = CCV_8U | CCV_C4;
+ break;
+ case CCV_IO_GRAY_RAW:
+ default:
+ /* default one */
+ break;
+ }
+ *x = ccv_dense_matrix_new(rows, cols, ctype | CCV_NO_DATA_ALLOC, data, 0);
(*x)->step = scanline;
} else {
switch (type & 0xFF)
@@ -84,14 +104,19 @@ static int _ccv_read_raw(ccv_dense_matrix_t** x, void* data, int type, int rows,
_ccv_read_rgb_raw(x, data, type, rows, cols, scanline);
break;
case CCV_IO_RGBA_RAW:
+ _ccv_read_rgba_raw(x, data, type, rows, cols, scanline);
break;
case CCV_IO_ARGB_RAW:
+ _ccv_read_argb_raw(x, data, type, rows, cols, scanline);
break;
case CCV_IO_BGR_RAW:
+ _ccv_read_bgr_raw(x, data, type, rows, cols, scanline);
break;
case CCV_IO_BGRA_RAW:
+ _ccv_read_bgra_raw(x, data, type, rows, cols, scanline);
break;
case CCV_IO_ABGR_RAW:
+ _ccv_read_abgr_raw(x, data, type, rows, cols, scanline);
break;
case CCV_IO_GRAY_RAW:
_ccv_read_gray_raw(x, data, type, rows, cols, scanline);
@@ -103,21 +128,23 @@ static int _ccv_read_raw(ccv_dense_matrix_t** x, void* data, int type, int rows,
return CCV_IO_FINAL;
}
-int ccv_read_impl(const char* in, ccv_dense_matrix_t** x, int type, int rows, int cols, int scanline)
+int ccv_read_impl(const void* in, ccv_dense_matrix_t** x, int type, int rows, int cols, int scanline)
{
FILE* fd = 0;
if (type & CCV_IO_ANY_FILE)
{
- fd = fopen(in, "rb");
+ assert(rows == 0 && cols == 0 && scanline == 0);
+ fd = fopen((const char*)in, "rb");
if (!fd)
return CCV_IO_ERROR;
return _ccv_read_and_close_fd(fd, x, type);
} else if (type & CCV_IO_ANY_STREAM) {
- assert(rows > 8);
+ assert(rows > 8 && cols == 0 && scanline == 0);
assert((type & 0xFF) == CCV_IO_DEFLATE_STREAM); // deflate stream (compressed stream) is not supported yet
fd = fmemopen((void*)in, (size_t)rows, "rb");
if (!fd)
return CCV_IO_ERROR;
+ // mimicking itself as a "file"
type = (type & ~0x10) | 0x20;
return _ccv_read_and_close_fd(fd, x, type);
} else if (type & CCV_IO_ANY_RAW) {
View
@@ -16,7 +16,7 @@ __thread static int ccv_cache_opt = 0;
ccv_dense_matrix_t* ccv_dense_matrix_new(int rows, int cols, int type, void* data, uint64_t sig)
{
ccv_dense_matrix_t* mat;
- if (ccv_cache_opt && sig != 0)
+ if (ccv_cache_opt && sig != 0 && !data && !(type & CCV_NO_DATA_ALLOC))
{
uint8_t type;
mat = (ccv_dense_matrix_t*)ccv_cache_out(&ccv_cache, sig, &type);
@@ -28,15 +28,22 @@ ccv_dense_matrix_t* ccv_dense_matrix_new(int rows, int cols, int type, void* dat
return mat;
}
}
- mat = (ccv_dense_matrix_t*)(data ? data : ccmalloc(ccv_compute_dense_matrix_size(rows, cols, type)));
+ if (type & CCV_NO_DATA_ALLOC)
+ {
+ mat = (ccv_dense_matrix_t*)ccmalloc(sizeof(ccv_dense_matrix_t));
+ mat->type = (CCV_GET_CHANNEL(type) | CCV_GET_DATA_TYPE(type) | CCV_MATRIX_DENSE | CCV_NO_DATA_ALLOC) & ~CCV_GARBAGE;
+ mat->data.u8 = data;
+ } else {
+ mat = (ccv_dense_matrix_t*)(data ? data : ccmalloc(ccv_compute_dense_matrix_size(rows, cols, type)));
+ mat->type = (CCV_GET_CHANNEL(type) | CCV_GET_DATA_TYPE(type) | CCV_MATRIX_DENSE) & ~CCV_GARBAGE;
+ mat->type |= data ? CCV_UNMANAGED : CCV_REUSABLE; // it still could be reusable because the signature could be derived one.
+ mat->data.u8 = (unsigned char*)(mat + 1);
+ }
mat->sig = sig;
- mat->type = (CCV_GET_CHANNEL(type) | CCV_GET_DATA_TYPE(type) | CCV_MATRIX_DENSE) & ~CCV_GARBAGE;
- mat->type |= data ? CCV_UNMANAGED : CCV_REUSABLE; // it still could be reusable because the signature could be derived one.
mat->rows = rows;
mat->cols = cols;
mat->step = (cols * CCV_GET_DATA_TYPE_SIZE(type) * CCV_GET_CHANNEL(type) + 3) & -4;
mat->refcount = 1;
- mat->data.u8 = (unsigned char*)(mat + 1);
return mat;
}
@@ -161,7 +168,10 @@ void ccv_matrix_free(ccv_matrix_t* mat)
{
ccv_dense_matrix_t* dmt = (ccv_dense_matrix_t*)mat;
dmt->refcount = 0;
- if (!ccv_cache_opt || !(dmt->type & CCV_REUSABLE) || dmt->sig == 0)
+ if (!ccv_cache_opt || // e don't enable cache
+ !(dmt->type & CCV_REUSABLE) || // or this is not a reusable piece
+ dmt->sig == 0 || // or this doesn't have valid signature
+ (dmt->type & CCV_NO_DATA_ALLOC)) // or this matrix is allocated as header-only, therefore we cannot cache it
ccfree(dmt);
else {
assert(CCV_GET_DATA_TYPE(dmt->type) == CCV_8U ||
Oops, something went wrong.

0 comments on commit df5025e

Please sign in to comment.