Skip to content

Commit

Permalink
added immultiread support
Browse files Browse the repository at this point in the history
  • Loading branch information
diegohce authored and deadprogram committed Aug 14, 2024
1 parent 674c565 commit 0ba962c
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 0 deletions.
Binary file added images/multipage.tif
Binary file not shown.
33 changes: 33 additions & 0 deletions imgcodecs.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <stdlib.h>
#include "imgcodecs.h"

// Image
Expand All @@ -6,6 +7,38 @@ Mat Image_IMRead(const char* filename, int flags) {
return new cv::Mat(img);
}

Mats Image_IMReadMulti(const char* filename, int flags) {
std::vector<cv::Mat> dst;
Mats m = Mats();

bool b = cv::imreadmulti(filename, dst, flags);
if (b) {
m.mats = new Mat[dst.size()];
for (size_t i = 0; i < dst.size(); ++i) {
m.mats[i] = new cv::Mat(dst[i]);
}
m.length = (int)dst.size();
}

return m;
}

Mats Image_IMReadMulti_WithParams(const char* filename, int start, int count, int flags) {
std::vector<cv::Mat> dst;
auto m = Mats();

auto b = cv::imreadmulti(filename, dst, start, count, flags);
if (b) {
m.mats = new Mat[dst.size()];
for (size_t i = 0; i < dst.size(); ++i) {
m.mats[i] = new cv::Mat(dst[i]);
}
m.length = (int)dst.size();
}

return m;
}


bool Image_IMWrite(const char* filename, Mat img) {
return cv::imwrite(filename, *img);
Expand Down
52 changes: 52 additions & 0 deletions imgcodecs.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,58 @@ func IMRead(name string, flags IMReadFlag) Mat {
return newMat(C.Image_IMRead(cName, C.int(flags)))
}

// IMReadMulti reads multi-page image from a file into a []Mat.
// The flags param is one of the IMReadFlag flags.
// If the image cannot be read (because of missing file, improper permissions,
// unsupported or invalid format), the function returns an empty []Mat.
//
// For further details, please see:
// https://docs.opencv.org/4.x/d4/da8/group__imgcodecs.html#gaaeb5e219c706fd6aa1ec6cf17b172080
func IMReadMulti(name string, flags IMReadFlag) []Mat {
var mats []Mat
multiRead := C.struct_Mats{}

cName := C.CString(name)
defer C.free(unsafe.Pointer(cName))

multiRead = C.Image_IMReadMulti(cName, C.int(flags))
defer C.Mats_Close(multiRead)

if multiRead.length > C.int(0) {
mats = make([]Mat, multiRead.length)
for i := 0; i < int(multiRead.length); i++ {
mats[i].p = C.Mats_get(multiRead, C.int(i))
}
}
return mats
}

// IMReadMulti reads multi-page image from a file into a []Mat.
// The flags param is one of the IMReadFlag flags.
// If the image cannot be read (because of missing file, improper permissions,
// unsupported or invalid format), the function returns an empty []Mat.
//
// For further details, please see:
// https://docs.opencv.org/4.x/d4/da8/group__imgcodecs.html#ga55e88dc40b65807cfbe2c62d27f7fdf9
func IMReadMulti_WithParams(name string, start int, count int, flags IMReadFlag) []Mat {
var mats []Mat
multiRead := C.struct_Mats{}

cName := C.CString(name)
defer C.free(unsafe.Pointer(cName))

multiRead = C.Image_IMReadMulti_WithParams(cName, C.int(start), C.int(count), C.int(flags))
defer C.Mats_Close(multiRead)

if multiRead.length > C.int(0) {
mats = make([]Mat, multiRead.length)
for i := 0; i < int(multiRead.length); i++ {
mats[i].p = C.Mats_get(multiRead, C.int(i))
}
}
return mats
}

// IMWrite writes a Mat to an image file.
//
// For further details, please see:
Expand Down
2 changes: 2 additions & 0 deletions imgcodecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ extern "C" {
#include "core.h"

Mat Image_IMRead(const char* filename, int flags);
Mats Image_IMReadMulti(const char* filename, int flags);
Mats Image_IMReadMulti_WithParams(const char* filename, int start, int count, int flags);
bool Image_IMWrite(const char* filename, Mat img);
bool Image_IMWrite_WithParams(const char* filename, Mat img, IntVector params);
void Image_IMEncode(const char* fileExt, Mat img, void* vector);
Expand Down
24 changes: 24 additions & 0 deletions imgcodecs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,27 @@ func TestIMDecodeWebp(t *testing.T) {
dec.Close()

}

func TestIMReadMulti(t *testing.T) {

mats := IMReadMulti("images/multipage.tif", IMReadAnyColor)

for i, page := range mats {
if page.Empty() {
t.Errorf("page %d empty", i)
}
}

}

func TestIMReadMulti_WithParams(t *testing.T) {

mats := IMReadMulti_WithParams("images/multipage.tif", 2, 3, IMReadAnyColor)

for i, page := range mats {
if page.Empty() {
t.Errorf("page %d empty", i)
}
}

}

0 comments on commit 0ba962c

Please sign in to comment.