Skip to content

Commit

Permalink
Merge 92ccbcb into 850c2ec
Browse files Browse the repository at this point in the history
  • Loading branch information
mvictoras committed Jan 12, 2015
2 parents 850c2ec + 92ccbcb commit ef1d1c6
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 10 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -420,11 +420,17 @@ _Requires libvips 7.42.0+_

An advanced setting to disable adaptive row filtering for the lossless PNG output format.

#### tileSize(tileSize)
Setting the tile_size when DZI output format is selected. Default is 256.

#### overlap(overlap)
Setting the overlap when DZI output format is selected. Default is 0.

### Output methods

#### toFile(filename, [callback])

`filename` is a String containing the filename to write the image data to. The format is inferred from the extension, with JPEG, PNG, WebP and TIFF supported.
`filename` is a String containing the filename to write the image data to. The format is inferred from the extension, with JPEG, PNG, WebP, TIFF and DZI supported.

`callback`, if present, is called with two arguments `(err, info)` where:

Expand Down
36 changes: 35 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ var Sharp = function(input) {
compressionLevel: 6,
withoutAdaptiveFiltering: false,
streamOut: false,
withMetadata: false
withMetadata: false,
tileSize: 256,
tileOverlap: 0
};
if (typeof input === 'string') {
// input=file
Expand Down Expand Up @@ -357,6 +359,32 @@ Sharp.prototype.withMetadata = function(withMetadata) {
return this;
};

/*
dzi tile size for DZI output
max is 8192 since 7.36.5 (1024 before)
*/
Sharp.prototype.tileSize = function(tileSize) {
if (!Number.isNaN(tileSize) && tileSize >= 1 && tileSize <= 8192) {
this.options.tileSize = tileSize;
} else {
throw new Error('Invalid tileSize (1 to 8192) ' + tileSize);
}
return this;
};

/*
dzi overlap for DZI output
*/
Sharp.prototype.tileOverlap = function(tileOverlap) {
if (!Number.isNaN(tileOverlap) && tileOverlap >=0 && tileOverlap <= 8192) {
this.options.tileOverlap = tileOverlap;
} else {
throw new Error('Invalid tileOverlap (0 to 8192) ' + tileOverlap);
}

return this;
};

Sharp.prototype.resize = function(width, height) {
if (!width) {
this.options.width = -1;
Expand Down Expand Up @@ -425,6 +453,12 @@ Sharp.prototype.webp = function() {
return this;
};

Sharp.prototype.dzi = function() {
this.options.output = '__dzi';
return this;
};


/*
Used by a Writable Stream to notify that it is ready for data
*/
Expand Down
13 changes: 10 additions & 3 deletions src/common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ namespace sharp {
bool IsTiff(std::string const &str) {
return EndsWith(str, ".tif") || EndsWith(str, ".tiff") || EndsWith(str, ".TIF") || EndsWith(str, ".TIFF");
}

bool IsDzi(std::string const &str) {
return EndsWith(str, ".dzi") || EndsWith(str, ".DZI");
}
// Buffer content checkers
unsigned char const MARKER_JPEG[] = {0xff, 0xd8};
unsigned char const MARKER_PNG[] = {0x89, 0x50};
Expand Down Expand Up @@ -90,11 +92,14 @@ namespace sharp {
imageType = ImageType::PNG;
} else if (vips_foreign_is_a("webpload", file)) {
imageType = ImageType::WEBP;
} else if (vips_foreign_is_a("tiffload", file)) {
} else if (vips_foreign_is_a("openslideload", file)) {
imageType = ImageType::OPENSLIDE;
} else if (vips_foreign_is_a("tiffload", file)) {
imageType = ImageType::TIFF;
} else if(vips_foreign_is_a("magickload", file)) {
imageType = ImageType::MAGICK;
}

return imageType;
}

Expand All @@ -109,7 +114,9 @@ namespace sharp {
vips_pngload(file, &image, "access", access, NULL);
} else if (imageType == ImageType::WEBP) {
vips_webpload(file, &image, "access", access, NULL);
} else if (imageType == ImageType::TIFF) {
} else if (imageType == ImageType::OPENSLIDE) {
vips_openslideload(file, &image, "access", access, NULL);
}else if (imageType == ImageType::TIFF) {
vips_tiffload(file, &image, "access", access, NULL);
} else if (imageType == ImageType::MAGICK) {
vips_magickload(file, &image, "access", access, NULL);
Expand Down
5 changes: 4 additions & 1 deletion src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ namespace sharp {
PNG,
WEBP,
TIFF,
MAGICK
MAGICK,
OPENSLIDE,
DZI
};

// How many tasks are in the queue?
Expand All @@ -23,6 +25,7 @@ namespace sharp {
bool IsPng(std::string const &str);
bool IsWebp(std::string const &str);
bool IsTiff(std::string const &str);
bool IsDzi(std::string const &str);

/*
Determine image format of a buffer.
Expand Down
6 changes: 5 additions & 1 deletion src/metadata.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,12 @@ class MetadataWorker : public NanAsyncWorker {
case ImageType::JPEG: baton->format = "jpeg"; break;
case ImageType::PNG: baton->format = "png"; break;
case ImageType::WEBP: baton->format = "webp"; break;
case ImageType::TIFF: baton->format = "tiff"; break;
#ifdef HAVE_OPENSLIDE_3_4
case ImageType::OPENSLIDE: baton->format = "openslide"; break;
#endif
case ImageType::TIFF: baton->format = "tiff"; break;
case ImageType::MAGICK: baton->format = "magick"; break;
case ImageType::DZI: baton->format = "dzi"; break;
case ImageType::UNKNOWN: break;
}
// VipsImage attributes
Expand Down
37 changes: 34 additions & 3 deletions src/resize.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ struct ResizeBaton {
bool withoutAdaptiveFiltering;
std::string err;
bool withMetadata;
#ifdef HAVE_OPENSLIDE_3_4
int tileSize;
int tileOverlap;
#endif

ResizeBaton():
bufferInLength(0),
Expand All @@ -92,7 +96,12 @@ struct ResizeBaton {
quality(80),
compressionLevel(6),
withoutAdaptiveFiltering(false),
withMetadata(false) {
withMetadata(false)
#ifdef HAVE_OPENSLIDE_3_4
,tileSize(256),
tileOverlap(0)
#endif
{
background[0] = 0.0;
background[1] = 0.0;
background[2] = 0.0;
Expand Down Expand Up @@ -640,7 +649,14 @@ class ResizeWorker : public NanAsyncWorker {
bool outputPng = IsPng(baton->output);
bool outputWebp = IsWebp(baton->output);
bool outputTiff = IsTiff(baton->output);
bool matchInput = !(outputJpeg || outputPng || outputWebp || outputTiff);
#ifdef HAVE_OPENSLIDE_3_4
bool outputDzi = IsDzi(baton->output);
#endif
bool matchInput = !(outputJpeg || outputPng || outputWebp || outputTiff
#ifdef HAVE_OPENSLIDE_3_4
|| outputDzi
#endif
);
if (outputJpeg || (matchInput && inputImageType == ImageType::JPEG)) {
// Write JPEG to file
if (vips_jpegsave(image, baton->output.c_str(), "strip", !baton->withMetadata,
Expand Down Expand Up @@ -679,7 +695,18 @@ class ResizeWorker : public NanAsyncWorker {
return Error(baton, hook);
}
baton->outputFormat = "tiff";
} else {
}
#ifdef HAVE_OPENSLIDE_3_4
else if (outputDzi || matchInput) {
// Write DZI to file
std::string filename_no_extension = baton->output.substr(0, baton->output.length() - 4);
if (vips_dzsave(image, filename_no_extension.c_str(), "strip", !baton->withMetadata,
"tile_size", baton->tileSize, "overlap", baton->tileOverlap, NULL)) {
return Error(baton, hook);
}
}
#endif
else {
(baton->err).append("Unsupported output " + baton->output);
return Error(baton, hook);
}
Expand Down Expand Up @@ -898,6 +925,10 @@ NAN_METHOD(resize) {
baton->withoutAdaptiveFiltering = options->Get(NanNew<String>("withoutAdaptiveFiltering"))->BooleanValue();
baton->withMetadata = options->Get(NanNew<String>("withMetadata"))->BooleanValue();
// Output filename or __format for Buffer
#ifdef HAVE_OPENSLIDE_3_4
baton->tileSize = options->Get(NanNew<String>("tileSize"))->Int32Value();
baton->tileOverlap = options->Get(NanNew<String>("tileOverlap"))->Int32Value();
#endif
baton->output = *String::Utf8Value(options->Get(NanNew<String>("output"))->ToString());

// Join queue for worker thread
Expand Down

0 comments on commit ef1d1c6

Please sign in to comment.