Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add htj2k option #9

Merged
merged 3 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- add shrink-on-load using the "page" param to kakaduload
- add "options" arg to kakadusave
- add resolution load and save
- add "htj2k" arg to enable high-throughput j2k compression
- add `ORGgen_tlm` support
- add "rate" arg to save

Expand Down
2 changes: 1 addition & 1 deletion src/kakaduload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -923,7 +923,7 @@ vips_foreign_load_kakadu_file_build(VipsObject *object)
}

const char *vips__kakadu_suffs[] = {
".j2k", ".jp2", ".jpt", ".j2c", ".jpc", NULL
".jph", ".j2k", ".jp2", ".jpt", ".j2c", ".jpc", NULL
};

static int
Expand Down
50 changes: 40 additions & 10 deletions src/kakadusave.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ typedef struct _VipsForeignSaveKakadu {
*/
gboolean lossless;

/* Enable high-throughput jp2k.
*/
gboolean htj2k;

/* Quality factor.
*/
int Q;
Expand Down Expand Up @@ -238,6 +242,11 @@ vips_foreign_save_kakadu_write_block(VipsRegion *region, VipsRect *area,
return 0;
}

const char *vips__jph_suffix[] = {
".jph",
NULL
};

static int
vips_foreign_save_kakadu_build(VipsObject *object)
{
Expand All @@ -262,17 +271,28 @@ vips_foreign_save_kakadu_build(VipsObject *object)
return -1;
}

// see if we have something like a jph filename and enable high-throughput
// compression
const char *filename =
vips_connection_filename(VIPS_CONNECTION(kakadu->target));
if (filename &&
vips_filename_suffix_match(filename, vips__jph_suffix))
kakadu->htj2k = true;

kakadu->stripe_heights = VIPS_ARRAY(NULL, image->Bands, int);

siz_params siz;

siz.set(Scomponents, 0, 0, image->Bands);
siz.set(Sdims, 0, 0, image->Ysize);
siz.set(Sdims, 0, 1, image->Xsize);
siz.set(Sprecision, 0, 0,
(int) (vips_format_sizeof(image->BandFmt) << 3));
siz.set(Sprecision, 0, 0, (int) (vips_format_sizeof(image->BandFmt) << 3));
siz.set(Ssigned, 0, 0, false);

// enable high throughput jp2 compression
if (kakadu->htj2k)
siz.set(Scap, 0, 0, Scap_P15);

// finalize to complete other fields ... has to be a reference
kdu_params *siz_ref = &siz;
siz_ref->finalize();
Expand Down Expand Up @@ -433,21 +453,28 @@ vips_foreign_save_kakadu_class_init(VipsForeignSaveKakaduClass *klass)
VIPS_TYPE_FOREIGN_SUBSAMPLE,
VIPS_FOREIGN_SUBSAMPLE_OFF);

VIPS_ARG_INT(klass, "Q", 14,
_("Q"),
_("Q factor"),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET(VipsForeignSaveKakadu, Q),
1, 100, 48);

VIPS_ARG_STRING(klass, "options", 25,
_("Options"),
_("Set of Kakadu option specifications"),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET(VipsForeignSaveKakadu, options),
NULL);

VIPS_ARG_BOXED(klass, "rate", 111,
VIPS_ARG_BOOL(klass, "htj2k", 15,
_("High-throughput"),
_("Enable high-throughput jp2k compression"),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET(VipsForeignSaveKakadu, htj2k),
FALSE);

VIPS_ARG_INT(klass, "Q", 14,
_("Q"),
_("Q factor"),
VIPS_ARGUMENT_OPTIONAL_INPUT,
G_STRUCT_OFFSET(VipsForeignSaveKakadu, Q),
1, 100, 48);

VIPS_ARG_BOXED(klass, "rate", 111,
_("Bitrate"),
_("Bitrate per layer"),
VIPS_ARGUMENT_OPTIONAL_INPUT,
Expand Down Expand Up @@ -681,6 +708,9 @@ vips_foreign_save_kakadu_target_init(VipsForeignSaveKakaduTarget *target)
* mode uses YCC rather than RGB colourspace, and many jpeg2000 decoders do
* not support this.
*
* Set @htj2k to enable high-throughput jpeg2000 compression. This option is
* enabled automatically if a filename ending in `.jph` is detected.
*
* This operation always writes a pyramid.
*
* See also: vips_image_write_to_file(), vips_kakaduload().
Expand Down
11 changes: 10 additions & 1 deletion test/test_kakadusave.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ def test_kakadusave_resolution(self):
assert abs(image.xres - 11.8) < 0.1
assert abs(image.yres - 11.8) < 0.1

@skip_if_no("kakaduload")
@skip_if_no("kakadusave")
def test_kakadusave_htj2k(self):
data1 = self.ppm.kakadusave_buffer()
data2 = self.ppm.kakadusave_buffer(htj2k=True)

# ie. the option has had an effect
assert len(data1) != len(data2)

@skip_if_no("kakaduload")
@skip_if_no("kakadusave")
def test_kakadusave_tlm(self):
# tlm needs working rewrite in target
Expand All @@ -80,4 +90,3 @@ def test_kakadusave_rate(self):

image10 = pyvips.Image.kakaduload_buffer(data10)
self.image_matches_file(image10, PPM_FILE, 10)