Skip to content

Commit

Permalink
dzsave: fix a couple of minor bugs (libvips#3726)
Browse files Browse the repository at this point in the history
- Avoid writing files with `[options]`.
- Honor `--imagename` option.
- Fix `--skip_blanks` option.
- Fix ZIP write with absolute paths.
  • Loading branch information
kleisauke authored Oct 22, 2023
1 parent 0c1e687 commit df8b8e2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 20 deletions.
28 changes: 10 additions & 18 deletions libvips/foreign/dzsave.c
Original file line number Diff line number Diff line change
Expand Up @@ -1147,7 +1147,6 @@ tile_name(Level *level, int x, int y)
{
VipsForeignSaveDz *dz = level->dz;
VipsForeignSave *save = (VipsForeignSave *) dz;
const char *suffix = iszip(dz->container) ? dz->file_suffix : dz->suffix;

char *out;
char *dirname;
Expand All @@ -1159,7 +1158,7 @@ tile_name(Level *level, int x, int y)
switch (dz->layout) {
case VIPS_FOREIGN_DZ_LAYOUT_DZ:
vips_snprintf(subdir, VIPS_PATH_MAX, "%d", level->n);
vips_snprintf(name, VIPS_PATH_MAX, "%d_%d%s", x, y, suffix);
vips_snprintf(name, VIPS_PATH_MAX, "%d_%d%s", x, y, dz->file_suffix);

break;

Expand All @@ -1182,7 +1181,7 @@ tile_name(Level *level, int x, int y)

vips_snprintf(subdir, VIPS_PATH_MAX, "TileGroup%d", n / 256);
vips_snprintf(name, VIPS_PATH_MAX,
"%d-%d-%d%s", level->n, x, y, suffix);
"%d-%d-%d%s", level->n, x, y, dz->file_suffix);

/* Used at the end in ImageProperties.xml
*/
Expand All @@ -1194,7 +1193,7 @@ tile_name(Level *level, int x, int y)
vips_snprintf(subdir, VIPS_PATH_MAX,
"%d" G_DIR_SEPARATOR_S "%d", level->n, y);
vips_snprintf(name, VIPS_PATH_MAX,
"%d%s", x, suffix);
"%d%s", x, dz->file_suffix);

break;

Expand Down Expand Up @@ -1245,7 +1244,7 @@ tile_name(Level *level, int x, int y)
rotation);
}

vips_snprintf(name, VIPS_PATH_MAX, "default%s", suffix);
vips_snprintf(name, VIPS_PATH_MAX, "default%s", dz->file_suffix);
}

break;
Expand Down Expand Up @@ -1281,7 +1280,7 @@ static gboolean
region_tile_equal(VipsRegion *region, VipsRect *rect,
int threshold, VipsPel *restrict ink)
{
int bytes = VIPS_REGION_SIZEOF_LINE(region);
int bytes = VIPS_REGION_SIZEOF_PEL(region);

int x, y, b;

Expand Down Expand Up @@ -2132,7 +2131,7 @@ vips_foreign_save_dz_build(VipsObject *object)
? dz->filename
: vips_connection_filename(VIPS_CONNECTION(dz->target));

if (!vips_object_argument_isset(object, "imagename") ||
if (!vips_object_argument_isset(object, "imagename") &&
!vips_object_argument_isset(object, "basename")) {
if (filename) {
dz->imagename = g_path_get_basename(filename);
Expand Down Expand Up @@ -2203,22 +2202,15 @@ vips_foreign_save_dz_build(VipsObject *object)
return -1;
}

char *path;

// SZI needs an enclosing folder named after the image, according to
// the spec
if (dz->container == VIPS_FOREIGN_DZ_CONTAINER_SZI)
path = g_build_filename(dz->dirname, dz->imagename, NULL);
else
path = g_strdup(dz->dirname);
char *path = dz->container == VIPS_FOREIGN_DZ_CONTAINER_SZI
? dz->imagename
: "";

if (!(dz->archive = vips__archive_new_to_target(dz->target,
path, dz->compression))) {
g_free(path);
path, dz->compression)))
return -1;
}

g_free(path);
}
else {
if (!(dz->archive = vips__archive_new_to_dir(dz->dirname)))
Expand Down
3 changes: 1 addition & 2 deletions test/test-suite/test_foreign.py
Original file line number Diff line number Diff line change
Expand Up @@ -1310,8 +1310,7 @@ def test_dzsave(self):
with open(filename, 'rb') as f:
buf1 = f.read()
buf2 = self.colour.dzsave_buffer(basename=root)
# won't be identical since tiles can be in a different order
assert abs(len(buf1) - len(buf2)) < 5000
assert len(buf1) == len(buf2)

# we can't test the bytes are exactly equal -- the timestamp in
# vips-properties.xml will be different
Expand Down

0 comments on commit df8b8e2

Please sign in to comment.