Permalink
Browse files

added a support for more ccv_read mode

  • Loading branch information...
1 parent 3968fac commit 656f3c337e4a2243d93405e78cc753a058cc9b1d @liuliu committed Sep 30, 2012
Showing with 114 additions and 16 deletions.
  1. +2 −2 bin/dpmcreate.c
  2. +13 −11 lib/ccv.h
  3. +48 −3 lib/ccv_io.c
  4. +51 −0 lib/io/_ccv_io_raw.c
View
@@ -19,7 +19,7 @@ void exit_with_help()
" --base-dir : change the base directory so that the program can read images from there\n"
" --iterations : how many iterations are needed for stochastic gradient descent [DEFAULT TO 1000]\n"
" --root-relabels : how many relabel procedures are needed for root model optimization [DEFAULT TO 20]\n"
- " --data-minings : how many data mining procedures are needed for discovering hard examples [DEFAULT TO 100]\n"
+ " --data-minings : how many data mining procedures are needed for discovering hard examples [DEFAULT TO 50]\n"
" --relabels : how many relabel procedures are needed for part model optimization [DEFAULT TO 10]\n"
" --alpha : the step size for stochastic gradient descent [DEFAULT TO 0.01]\n"
" --alpha-ratio : decrease the step size for each iteration [DEFAULT TO 0.995]\n"
@@ -78,7 +78,7 @@ int main(int argc, char** argv)
.balance = 1.5,
.alpha_ratio = 0.995,
.iterations = 1000,
- .data_minings = 100,
+ .data_minings = 50,
.root_relabels = 20,
.relabels = 10,
.negative_cache_size = 2000,
View
@@ -267,10 +267,11 @@ enum {
enum {
// read self-describe in-memory data
CCV_IO_ANY_STREAM = 0x010,
- CCV_IO_PLAIN_STREAM = 0x011,
- CCV_IO_DEFLATE_STREAM = 0x012,
- CCV_IO_JPEG_STREAM = 0x013,
- CCV_IO_PNG_STREAM = 0x014,
+ CCV_IO_BMP_STREAM = 0x011,
+ CCV_IO_JPEG_STREAM = 0x012,
+ CCV_IO_PNG_STREAM = 0x013,
+ CCV_IO_PLAIN_STREAM = 0x014,
+ CCV_IO_DEFLATE_STREAM = 0x015,
// read self-describe on-disk data
CCV_IO_ANY_FILE = 0x020,
CCV_IO_BMP_FILE = 0x021,
@@ -279,13 +280,14 @@ enum {
CCV_IO_BINARY_FILE = 0x024,
// read not-self-describe in-memory data (a.k.a. raw data)
// you need to specify rows, cols, or scanline for these data
- CCV_IO_RGB_RAW = 0x031,
- CCV_IO_RGBA_RAW = 0x032,
- CCV_IO_ARGB_RAW = 0x033,
- CCV_IO_BGR_RAW = 0x034,
- CCV_IO_BGRA_RAW = 0x035,
- CCV_IO_ABGR_RAW = 0x036,
- CCV_IO_GRAY_RAW = 0x037,
+ CCV_IO_ANY_RAW = 0x040,
+ CCV_IO_RGB_RAW = 0x041,
+ CCV_IO_RGBA_RAW = 0x042,
+ CCV_IO_ARGB_RAW = 0x043,
+ CCV_IO_BGR_RAW = 0x044,
+ CCV_IO_BGRA_RAW = 0x045,
+ CCV_IO_ABGR_RAW = 0x046,
+ CCV_IO_GRAY_RAW = 0x047,
};
enum {
View
@@ -24,8 +24,9 @@
#endif
#include "io/_ccv_io_bmp.c"
#include "io/_ccv_io_binary.c"
+#include "io/_ccv_io_raw.c"
-int ccv_read_and_close_fd(FILE* fd, ccv_dense_matrix_t** x, int type)
+static int _ccv_read_and_close_fd(FILE* fd, ccv_dense_matrix_t** x, int type)
{
int ctype = (type & 0xF00) ? CCV_8U | ((type & 0xF00) >> 8) : 0;
if ((type & 0XFF) == CCV_IO_ANY_FILE)
@@ -67,6 +68,41 @@ int ccv_read_and_close_fd(FILE* fd, ccv_dense_matrix_t** x, int type)
return CCV_IO_FINAL;
}
+static int _ccv_read_raw(ccv_dense_matrix_t** x, void* data, int type, int rows, int cols, int scanline)
+{
+ assert(rows > 0 && cols > 0 && scanline > 0);
+ 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);
+ (*x)->step = scanline;
+ } else {
+ switch (type & 0xFF)
+ {
+ case CCV_IO_RGB_RAW:
+ _ccv_read_rgb_raw(x, data, type, rows, cols, scanline);
+ break;
+ case CCV_IO_RGBA_RAW:
+ break;
+ case CCV_IO_ARGB_RAW:
+ break;
+ case CCV_IO_BGR_RAW:
+ break;
+ case CCV_IO_BGRA_RAW:
+ break;
+ case CCV_IO_ABGR_RAW:
+ break;
+ case CCV_IO_GRAY_RAW:
+ _ccv_read_gray_raw(x, data, type, rows, cols, scanline);
+ break;
+ }
+ }
+ if (*x != 0)
+ ccv_make_matrix_immutable(*x);
+ return CCV_IO_FINAL;
+}
+
int ccv_read_impl(const char* in, ccv_dense_matrix_t** x, int type, int rows, int cols, int scanline)
{
FILE* fd = 0;
@@ -75,8 +111,17 @@ int ccv_read_impl(const char* in, ccv_dense_matrix_t** x, int type, int rows, in
fd = fopen(in, "rb");
if (!fd)
return CCV_IO_ERROR;
- return ccv_read_and_close_fd(fd, x, type);
- } else {
+ return _ccv_read_and_close_fd(fd, x, type);
+ } else if (type & CCV_IO_ANY_STREAM) {
+ assert(rows > 8);
+ 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;
+ type = (type & ~0x10) | 0x20;
+ return _ccv_read_and_close_fd(fd, x, type);
+ } else if (type & CCV_IO_ANY_RAW) {
+ return _ccv_read_raw(x, (void*)in /* it can be modifiable if it is NO_COPY mode */, type, rows, cols, scanline);
}
return CCV_IO_UNKNOWN;
}
View
@@ -0,0 +1,51 @@
+static void _ccv_read_rgb_raw(ccv_dense_matrix_t** x, const void* data, int type, int rows, int cols, int scanline)
+{
+ int ctype = (type & 0xF00) ? CCV_8U | ((type & 0xF00) >> 8) : CCV_8U | CCV_C3;
+ ccv_dense_matrix_t* dx = *x = ccv_dense_matrix_new(rows, cols, ctype, 0, 0);
+ int i, j;
+ unsigned char* x_ptr = dx->data.u8;
+ unsigned char* a_ptr = (unsigned char*)data;
+ if (type & CCV_IO_GRAY)
+ {
+ for (i = 0; i < rows; i++)
+ {
+ for (j = 0; j < cols; j++)
+ x_ptr[j] = (unsigned char)((a_ptr[j * 3] * 6969 + a_ptr[j * 3 + 1] * 23434 + a_ptr[j * 3 + 2] * 2365) >> 15);
+ a_ptr += scanline;
+ x_ptr += dx->step;
+ }
+ } else {
+ for (i = 0; i < rows; i++)
+ {
+ memcpy(x_ptr, a_ptr, cols * 3);
+ a_ptr += scanline;
+ x_ptr += dx->step;
+ }
+ }
+}
+
+static void _ccv_read_gray_raw(ccv_dense_matrix_t** x, const void* data, int type, int rows, int cols, int scanline)
+{
+ int ctype = (type & 0xF00) ? CCV_8U | ((type & 0xF00) >> 8) : CCV_8U | CCV_C1;
+ ccv_dense_matrix_t* dx = *x = ccv_dense_matrix_new(rows, cols, ctype, 0, 0);
+ int i, j;
+ unsigned char* x_ptr = dx->data.u8;
+ unsigned char* a_ptr = (unsigned char*)data;
+ if (type & CCV_IO_GRAY)
+ {
+ for (i = 0; i < rows; i++)
+ {
+ memcpy(x_ptr, a_ptr, cols);
+ a_ptr += scanline;
+ x_ptr += dx->step;
+ }
+ } else {
+ for (i = 0; i < rows; i++)
+ {
+ for (j = 0; j < cols; j++)
+ x_ptr[j * 3] = x_ptr[j * 3 + 1] = x_ptr[j * 3 + 2] = a_ptr[j];
+ a_ptr += scanline;
+ x_ptr += dx->step;
+ }
+ }
+}

0 comments on commit 656f3c3

Please sign in to comment.