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 // 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) { case CCV_IO_RGB_RAW: _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); break; } } if (*x != 0) ccv_make_matrix_immutable(*x); return CCV_IO_FINAL; }
int ccv_read(const char* in, ccv_dense_matrix_t** x, int type) { FILE* fd = 0; int ctype = (type & 0xF00) ? CCV_8U | ((type & 0xF00) >> 8) : 0; if (type & CCV_IO_ANY_FILE) { fd = fopen(in, "rb"); if (!fd) return CCV_IO_ERROR; } if ((type & 0XFF) == CCV_IO_ANY_FILE) { unsigned char sig[8]; (void) fread(sig, 1, 8, fd); if (memcmp(sig, "\x89\x50\x4e\x47\xd\xa\x1a\xa", 8) == 0) type = CCV_IO_PNG_FILE; else if (memcmp(sig, "\xff\xd8\xff", 3) == 0) type = CCV_IO_JPEG_FILE; else if (memcmp(sig, "BM", 2) == 0) type = CCV_IO_BMP_FILE; else if (memcmp(sig, "CCVBINDM", 8) == 0) type = CCV_IO_BINARY_FILE; fseek(fd, 0, SEEK_SET); } switch (type & 0XFF) { #ifdef HAVE_LIBJPEG case CCV_IO_JPEG_FILE: _ccv_read_jpeg_fd(fd, x, ctype); break; #endif #ifdef HAVE_LIBPNG case CCV_IO_PNG_FILE: _ccv_read_png_fd(fd, x, ctype); break; #endif case CCV_IO_BMP_FILE: _ccv_read_bmp_fd(fd, x, ctype); break; case CCV_IO_BINARY_FILE: _ccv_read_binary_fd(fd, x, ctype); } if (*x != 0) ccv_make_matrix_immutable(*x); if (type & CCV_IO_ANY_FILE) fclose(fd); return CCV_IO_FINAL; }
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) { unsigned char sig[8]; (void) fread(sig, 1, 8, fd); if (memcmp(sig, "\x89\x50\x4e\x47\xd\xa\x1a\xa", 8) == 0) type = CCV_IO_PNG_FILE; else if (memcmp(sig, "\xff\xd8\xff", 3) == 0) type = CCV_IO_JPEG_FILE; else if (memcmp(sig, "BM", 2) == 0) type = CCV_IO_BMP_FILE; else if (memcmp(sig, "CCVBINDM", 8) == 0) type = CCV_IO_BINARY_FILE; else { printf("Unknown file signature in '%s'\n", sig); exit(1); // XXX make exit/return gracefull later }; fseek(fd, 0, SEEK_SET); } switch (type & 0XFF) { #ifdef HAVE_LIBJPEG case CCV_IO_JPEG_FILE: _ccv_read_jpeg_fd(fd, x, ctype); break; #endif #ifdef HAVE_LIBPNG case CCV_IO_PNG_FILE: _ccv_read_png_fd(fd, x, ctype); break; #endif case CCV_IO_BMP_FILE: _ccv_read_bmp_fd(fd, x, ctype); break; case CCV_IO_BINARY_FILE: _ccv_read_binary_fd(fd, x, ctype); } if (*x != 0) ccv_make_matrix_immutable(*x); if (type & CCV_IO_ANY_FILE) fclose(fd); return CCV_IO_FINAL; }