Skip to content

Commit 5ee744f

Browse files
committed
clean up
1 parent 32a45c4 commit 5ee744f

File tree

7 files changed

+156
-138
lines changed

7 files changed

+156
-138
lines changed

.clang-tidy

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ Checks: >
1414
-readability-uppercase-literal-suffix,
1515
-readability-simplify-boolean-expr,
1616
-readability-math-missing-parentheses,
17+
-readability-braces-around-statements,
18+
-readability-isolate-declaration,
1719
clang-analyzer-*,
1820
-clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling,
1921
performance-*,

tools/mtmd/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ if (MTMD_WITH_FFMPEG)
3939
endif()
4040
endif()
4141

42+
option(MTMD_MAX_VIDEO_FRAMES_SMALL "Set a small number of frames for fast test locally" OFF)
43+
if(MTMD_MAX_VIDEO_FRAMES_SMALL)
44+
target_compile_definitions(mtmd PRIVATE MTMD_MAX_VIDEO_FRAMES_SMALL)
45+
endif()
46+
4247
if (BUILD_SHARED_LIBS)
4348
set_target_properties (mtmd PROPERTIES POSITION_INDEPENDENT_CODE ON)
4449
target_compile_definitions(mtmd PRIVATE LLAMA_BUILD)

tools/mtmd/mtmd-cli.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,8 @@ struct mtmd_cli_context {
153153
);
154154
}
155155

156-
bool load_media(const std::string & fname) {
157-
mtmd::bitmap bmp(mtmd_helper_bitmap_init_from_file(ctx_vision.get(), fname.c_str()));
156+
bool load_media(const std::string & path) {
157+
mtmd::bitmap bmp(mtmd_helper_bitmap_init_from_file(ctx_vision.get(), path.c_str()));
158158
if (!bmp.ptr) {
159159
return false;
160160
}

tools/mtmd/mtmd-helper.cpp

Lines changed: 75 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
# define NOMINMAX
66
#endif
77
#include <windows.h>
8+
#else
9+
#include <dirent.h>
10+
#include <sys/stat.h>
11+
#include <sys/types.h>
812
#endif
913

1014
#include "mtmd.h"
@@ -442,16 +446,16 @@ mtmd_bitmap * mtmd_helper_bitmap_init_from_buf(mtmd_context * ctx, const unsigne
442446
return result;
443447
}
444448

445-
mtmd_bitmap * mtmd_helper_bitmap_init_from_file(mtmd_context * ctx, const char * fname) {
449+
mtmd_bitmap * mtmd_helper_bitmap_init_from_file(mtmd_context * ctx, const char * path) {
446450
// although we could read the file into memory and call mtmd_helper_bitmap_init_from_buf,
447451
// but for video files, it's better to let ffmpeg read from file
448-
if(mtmd_video::is_video_file(fname)){
449-
return mtmd_video::init_video_bitmap(ctx, fname);
452+
if(mtmd_video::is_video_file(path) || mtmd_helper::is_dir(path)){
453+
return mtmd_video::init_video_bitmap(ctx, path);
450454
}
451455

452-
FILE * f = fopen(fname, "rb");
456+
FILE * f = fopen(path, "rb");
453457
if (!f) {
454-
LOG_ERR("Unable to open file %s: %s\n", fname, strerror(errno));
458+
LOG_ERR("Unable to open path %s: %s\n", path, strerror(errno));
455459
return nullptr;
456460
}
457461

@@ -463,11 +467,76 @@ mtmd_bitmap * mtmd_helper_bitmap_init_from_file(mtmd_context * ctx, const char *
463467
size_t n_read = fread(buf, 1, file_size, f);
464468
fclose(f);
465469
if (n_read != (size_t)file_size) {
466-
LOG_ERR("Failed to read entire file %s", fname);
470+
LOG_ERR("Failed to read entire path %s", path);
467471
return nullptr;
468472
}
469473

470474
auto * res = mtmd_helper_bitmap_init_from_buf(ctx, buf, file_size);
471475
delete [] buf;
472476
return res;
473477
}
478+
479+
namespace mtmd_helper{
480+
481+
bool has_image_ext(const std::string & name) {
482+
auto lower = name;
483+
std::transform(lower.begin(), lower.end(), lower.begin(), [](unsigned char c){ return (char)std::tolower(c); });
484+
return lower.rfind(".jpg") != std::string::npos ||
485+
lower.rfind(".jpeg") != std::string::npos ||
486+
lower.rfind(".png") != std::string::npos ||
487+
lower.rfind(".bmp") != std::string::npos ||
488+
lower.rfind(".gif") != std::string::npos ||
489+
lower.rfind(".webp") != std::string::npos;
490+
}
491+
492+
bool is_dir(const std::string & path) {
493+
#if defined(_WIN32)
494+
DWORD attrs = GetFileAttributesA(path.c_str());
495+
return (attrs != INVALID_FILE_ATTRIBUTES) && (attrs & FILE_ATTRIBUTE_DIRECTORY);
496+
#else
497+
struct stat st;
498+
if (stat(path.c_str(), &st) != 0) return false;
499+
return S_ISDIR(st.st_mode);
500+
#endif
501+
}
502+
503+
void list_files(const std::string & dir, std::vector<std::string> & out, bool recursive) {
504+
#if defined(_WIN32)
505+
std::string pattern = dir;
506+
if (!pattern.empty() && pattern.back() != '/' && pattern.back() != '\\') pattern += "\\";
507+
pattern += "*";
508+
WIN32_FIND_DATAA ffd;
509+
HANDLE hFind = FindFirstFileA(pattern.c_str(), &ffd);
510+
if (hFind == INVALID_HANDLE_VALUE) return;
511+
do {
512+
std::string name = ffd.cFileName;
513+
if (name == "." || name == "..") continue;
514+
std::string path = dir;
515+
if (!path.empty() && path.back() != '/' && path.back() != '\\') path += "\\";
516+
path += name;
517+
if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
518+
if (recursive) list_files(path, out, recursive);
519+
} else {
520+
out.push_back(path);
521+
}
522+
} while (FindNextFileA(hFind, &ffd) != 0);
523+
FindClose(hFind);
524+
#else
525+
DIR * dp = opendir(dir.c_str());
526+
if (!dp) return;
527+
struct dirent * de;
528+
while ((de = readdir(dp)) != nullptr) {
529+
std::string name = de->d_name;
530+
if (name == "." || name == "..") continue;
531+
std::string path = dir + "/" + name;
532+
if (is_dir(path)) {
533+
if (recursive) list_files(path, out, recursive);
534+
} else {
535+
out.push_back(path);
536+
}
537+
}
538+
closedir(dp);
539+
#endif
540+
}
541+
542+
}

tools/mtmd/mtmd-helper.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ extern "C" {
2424
// it calls mtmd_helper_bitmap_init_from_buf() internally
2525
// returns nullptr on failure
2626
// this function is thread-safe
27-
MTMD_API mtmd_bitmap * mtmd_helper_bitmap_init_from_file(mtmd_context * ctx, const char * fname);
27+
MTMD_API mtmd_bitmap * mtmd_helper_bitmap_init_from_file(mtmd_context * ctx, const char * path);
2828

2929
// helper function to construct a mtmd_bitmap from a buffer containing a file
3030
// supported formats:
@@ -88,4 +88,10 @@ MTMD_API int32_t mtmd_helper_decode_image_chunk(mtmd_context * ctx,
8888
// C++ wrappers
8989
//
9090

91+
namespace mtmd_helper{
92+
bool has_image_ext(const std::string & name);
93+
bool is_dir(const std::string & path);
94+
void list_files(const std::string & dir, std::vector<std::string> & out, bool recursive);
95+
}
96+
9197
#endif

0 commit comments

Comments
 (0)