diff --git a/index.js b/index.js index 025b8ddf5..3c8fe5266 100755 --- a/index.js +++ b/index.js @@ -73,22 +73,7 @@ var Sharp = function(input) { this.options.fileIn = input; } else if (typeof input === 'object' && input instanceof Buffer) { // input=buffer - if ( - (input.length > 3) && - // JPEG - (input[0] === 0xFF && input[1] === 0xD8) || - // PNG - (input[0] === 0x89 && input[1] === 0x50) || - // WebP - (input[0] === 0x52 && input[1] === 0x49) || - // TIFF - (input[0] === 0x4D && input[1] === 0x4D && input[2] === 0x00 && (input[3] === 0x2A || input[3] === 0x2B)) || - (input[0] === 0x49 && input[1] === 0x49 && (input[2] === 0x2A || input[2] === 0x2B) && input[3] === 0x00) - ) { - this.options.bufferIn = input; - } else { - throw new Error('Buffer contains an unsupported image format. JPEG, PNG, WebP and TIFF are currently supported.'); - } + this.options.bufferIn = input; } else { // input=stream this.options.streamIn = true; diff --git a/src/common.cc b/src/common.cc index a8c670d31..2e69253ee 100755 --- a/src/common.cc +++ b/src/common.cc @@ -29,36 +29,40 @@ namespace sharp { return EndsWith(str, ".tif") || EndsWith(str, ".tiff") || EndsWith(str, ".TIF") || EndsWith(str, ".TIFF"); } - // Buffer content checkers - unsigned char const MARKER_JPEG[] = {0xff, 0xd8}; - unsigned char const MARKER_PNG[] = {0x89, 0x50}; - unsigned char const MARKER_WEBP[] = {0x52, 0x49}; - - static bool buffer_is_tiff(char *buffer, size_t len) { - return ( - len >= 4 && ( - (buffer[0] == 'M' && buffer[1] == 'M' && buffer[2] == '\0' && (buffer[3] == '*' || buffer[3] == '+')) || - (buffer[0] == 'I' && buffer[1] == 'I' && (buffer[2] == '*' || buffer[2] == '+') && buffer[3] == '\0') - ) - ); - } - /* Determine image format of a buffer. */ ImageType DetermineImageType(void *buffer, size_t const length) { ImageType imageType = ImageType::UNKNOWN; - if (length >= 4) { - if (memcmp(MARKER_JPEG, buffer, 2) == 0) { +#if (VIPS_MAJOR_VERSION >= 8) + if (vips_foreign_is_a_buffer("jpegload_buffer", buffer, length)) { + imageType = ImageType::JPEG; + } else if (vips_foreign_is_a_buffer("pngload_buffer", buffer, length)) { + imageType = ImageType::PNG; + } else if (vips_foreign_is_a_buffer("webpload_buffer", buffer, length)) { + imageType = ImageType::WEBP; + } else if (vips_foreign_is_a_buffer("tiffload_buffer", buffer, length)) { + imageType = ImageType::TIFF; + } else if(vips_foreign_is_a_buffer("magickload_buffer", buffer, length)) { + imageType = ImageType::MAGICK; + } +#else + const char* loader = vips_foreign_find_load_buffer(buffer, length); + + if (loader != NULL) { + if (!strcmp(loader, "VipsForeignLoadJpegBuffer")) { imageType = ImageType::JPEG; - } else if (memcmp(MARKER_PNG, buffer, 2) == 0) { + } else if (!strcmp(loader, "VipsForeignLoadPngBuffer")) { imageType = ImageType::PNG; - } else if (memcmp(MARKER_WEBP, buffer, 2) == 0) { + } else if (!strcmp(loader, "VipsForeignLoadWebpBuffer")) { imageType = ImageType::WEBP; - } else if (buffer_is_tiff(static_cast(buffer), length)) { + } else if (!strcmp(loader, "VipsForeignLoadTiffBuffer")) { imageType = ImageType::TIFF; + } else if (!strcmp(loader, "VipsForeignLoadMagickBuffer")) { + imageType = ImageType::MAGICK; } } +#endif return imageType; } diff --git a/test/unit/io.js b/test/unit/io.js index 1501b6f31..74f1130d4 100755 --- a/test/unit/io.js +++ b/test/unit/io.js @@ -202,27 +202,23 @@ describe('Input/output', function() { }); it('Fail when input is empty Buffer', function(done) { - var failed = true; - try { - sharp(new Buffer(0)); - failed = false; - } catch (err) { + sharp(new Buffer(0)).toBuffer().then(function () { + assert(false); + done(); + }).catch(function (err) { assert(err instanceof Error); - } - assert(failed); - done(); + done(); + }); }); it('Fail when input is invalid Buffer', function(done) { - var failed = true; - try { - sharp(new Buffer([0x1, 0x2, 0x3, 0x4])); - failed = false; - } catch (err) { + sharp(new Buffer([0x1, 0x2, 0x3, 0x4])).toBuffer().then(function () { + assert(false); + done(); + }).catch(function (err) { assert(err instanceof Error); - } - assert(failed); - done(); + done(); + }); }); it('Promises/A+', function(done) {