lovell / sharp

High performance Node.js image processing, the fastest module to resize JPEG, PNG, WebP, AVIF and TIFF images. Uses the libvips library.
https://sharp.pixelplumbing.com
Apache License 2.0
29.27k stars 1.3k forks source link

Support for Windows #19

Closed DullReferenceException closed 9 years ago

DullReferenceException commented 10 years ago

I'd like to be able to use sharp on the Windows platform. Because libvips is available for Windows, I assume that lack of support is mostly in figuring out the build script.

Has this been investigated already? If so, what approaches have already been tried and roadblocks encountered? Knowing what's been tried so far, if anything, will help me or other potential developers come to a working solution quicker.

jcupitt commented 9 years ago

I had a quick look at the normalise function, it looks nice.

You could make it a bit more concise with vips_object_local_array(), I expect you know. For example (untested):

        VipsImage **t = (VipsImage **) vips_object_local_array(hook, 7);

        // normalize the luminance band in LAB space:
        if (vips_colourspace(image, &t[0], VIPS_INTERPRETATION_LAB, NULL) ||
                vips_extract_band(t[0], &t[1], 0, "n", 1, NULL) ||
                vips_extract_band(t[0], &t[2], 1, "n", 2, NULL) ||
                vips_stats(t[1], &t[3], NULL)) {
                return Error();
        }

        double min = *VIPS_MATRIX(stats, 0, 0);
        double max = *VIPS_MATRIX(stats, 1, 0);

        if (min == max) {
                // Range of zero: create black image
                if (vips_black(&t[4], image->Xsize, image->Ysize, 
                        "bands", 1, NULL )) {
                        return Error();
                }
        } else {
                double f = 100.0 / (max - min);
                double a = -(min * f);

                if (vips_linear1(t[1], &t[5], f, a, NULL) ||
                        vips_bandjoin2(t[5], t[2], &t[6], NULL) ||
                        vips_colourspace(t[6], &t[4], image->Type, NULL)) {
                        return Error();
                }
         }

        VipsImage *normalized = t[4];

You could also consider taking a histogram of L* and setting the range to 1% and 99%, ie. letting the top 1% and the bottom 1% over- and under-expose. This would make it less sensitive to noise and probably produce a more pleasing image in more cases.

vips_percent() does almost this, you'd swap your vips_stats() for this code:

https://github.com/jcupitt/libvips/blob/master/libvips/histogram/percent.c#L82

lovell commented 9 years ago

The judgement branch should be working on Windows again and I've added a Windows CI job via AppVeyor e.g. https://ci.appveyor.com/project/lovell/sharp/build/19

@jcupitt The use of vips_object_local and vips_object_local_array will go away with #152. Your suggested improvements for normalisation are in #200, thank you.

jcupitt commented 9 years ago

Ah great ... yes, #152 should be a nice improvement to readability.

lovell commented 9 years ago

Support for Windows now in git master - will be published to npm shortly.

lovell commented 9 years ago

Support for Windows now available in v0.10.0 via npm https://www.npmjs.com/package/sharp

Thanks for everyone's help with this year-long task!

Please create a new issue for any problems/improvements you discover.

blowsie commented 9 years ago

Great news! thanks everyone and @lovell :+1:

blowsie commented 9 years ago

Sadly, Following the installation instructions on the readme, I get the following error

Windows: 8.1 Node : 0.12.2 (x86) Npm: 2.7.4 Node-gyp: 1.0.3 Vips: 8.0.2

D:\Projects\myproject>npm install sharp
|
> sharp@0.10.0 install D:\Projects\myproject\node_modules\sharp
> node-gyp rebuild

D:\Projects\myproject\node_modules\sharp>if not defined npm_config_node_gyp (node "C:\Program Files (x86)\nodejs\node_modules\npm\bin\node-gyp-bin\\..\..\node
_modules\node-gyp\bin\node-gyp.js" rebuild )  else (rebuild)
Building the projects in this solution one at a time. To enable parallel build, please add the "/m" switch.
  common.cc
  utilities.cc
  metadata.cc
  resize.cc
  sharp.cc
..\src\metadata.cc(2): fatal error C1083: Cannot open include file: 'vips/vips.h': No such file or directory [D:\Projects\myproject\node_modules\sharp\build\
sharp.vcxproj]
..\src\sharp.cc(2): fatal error C1083: Cannot open include file: 'vips/vips.h': No such file or directory [D:\Projects\myproject\node_modules\sharp\build\sha
rp.vcxproj]
..\src\utilities.cc(2): fatal error C1083: Cannot open include file: 'vips/vips.h': No such file or directory [D:\Projects\myproject\node_modules\sharp\build
\sharp.vcxproj]
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xlocale(337): warning C4530: C++ exception handler used, but unwind semantics are not enabled. Specify 
/EHsc (..\src\common.cc) [D:\Projects\myproject\node_modules\sharp\build\sharp.vcxproj]
..\src\resize.cc(6): fatal error C1083: Cannot open include file: 'vips/vips.h': No such file or directory [D:\Projects\myproject\node_modules\sharp\build\sh
arp.vcxproj]
..\src\common.cc(3): fatal error C1083: Cannot open include file: 'vips/vips.h': No such file or directory [D:\Projects\myproject\node_modules\sharp\build\sh
arp.vcxproj]
gyp ERR! build error
gyp ERR! stack Error: `C:\Program Files (x86)\MSBuild\12.0\bin\msbuild.exe` failed with exit code: 1
gyp ERR! stack     at ChildProcess.onExit (C:\Program Files (x86)\nodejs\node_modules\npm\node_modules\node-gyp\lib\build.js:269:23)
gyp ERR! stack     at ChildProcess.emit (events.js:110:17)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (child_process.js:1074:12)
gyp ERR! System Windows_NT 6.3.9600
gyp ERR! command "node" "C:\\Program Files (x86)\\nodejs\\node_modules\\npm\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild"
gyp ERR! cwd D:\Projects\myproject\node_modules\sharp
gyp ERR! node -v v0.12.2
gyp ERR! node-gyp -v v1.0.3
gyp ERR! not ok
npm ERR! Windows_NT 6.3.9600
npm ERR! argv "C:\\Program Files (x86)\\nodejs\\\\node.exe" "C:\\Program Files (x86)\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "install" "sharp"
lovell commented 9 years ago

@blowsie Is VIPS_HOME set? Does the $(VIPS_HOME)/include directory exist?

blowsie commented 9 years ago

Thanks @lovell, I did have them set, but you prompted me to restart, which did the trick. Maybe I should know by now to restart after setting variables, or maybe it should be added to the readme. :)

lovell commented 9 years ago

The look work-in-progress branch now supports the installation of sharp on Windows without having to manually download/unpack vips-dev and without having to set/modify any environment variables.

To test it, run npm install lovell/sharp#look - please report any problems at #42.

blowsie commented 9 years ago

@lovell ooo, nice work!

Thanks

lovell commented 9 years ago

As well as ease-of-installation, it looks like support for 64-bit node.exe on Windows will also be arriving in the next release v0.12.0.

What problems, if any, would anyone who has contributed here have with sharp dropping support for 32-bit Windows from that version onwards?

blowsie commented 8 years ago

On v0.12.0 , I'm have proxy issues with the request to dl.bintray.com I can only assume it is not npm that is making the request, because npm is proxy configurable and is working fine for everything else for me.


Update:

In bindings.js there is a request to the libvibs library @ dl.bintray.com.

I think this request should be proxy aware. How about using shelljs to obtain the npm proxy config via npm config get proxy and then configure the request

lovell commented 8 years ago

@blowsie The request module is used for this purpose. It supports some fairly common proxy-related environment variables - see https://github.com/request/request#controlling-proxy-behaviour-using-environment-variables

Are you setting npm's https-proxy setting? If so, this might be available at install time for us to manually pass along to the request module, but I'd need to check.

blowsie commented 8 years ago

Yep My .npmrc file looks something like this.

proxy=http://proxy.com:43128/
https-proxy=http://proxy.com:43128/

I will try an use the environment vairables now, thanks. :+1:

blowsie commented 8 years ago

Setting the environment variables, and restarting the machine did not work for me :(. It did manage to break a whole plethora of other (Office 365) applications though.

Perhaps using the npm config for this is not such a bad idea.

lovell commented 8 years ago

"It did manage to break a whole plethora of other (Office 365) applications though."

http://thenextweb.com/microsoft/2015/12/03/office-365-is-down-for-many-users-in-europe/ suggests we might not be able to take the credit for that.

"Perhaps using the npm config for this is not such a bad idea."

Will take a look, thanks for confirming.

blowsie commented 8 years ago

Update: using the environment variables did infact work, the reason the installation still did not work was because the file at dl.bintray.com was actually redirecting to cloudfront.net.

So using the environment variables does actually fix the problem, but may effect other applications, like "Skype for Business" in my case

lovell commented 8 years ago

@blowsie Commit 16e0d54 should allow for the use of the https-proxy setting of .npmrc, if any. You can test via npm install lovell/sharp.

blowsie commented 8 years ago

@lovell Works a charm thank you very much. I look forward to the next release.

lovell commented 8 years ago

@blowsie Great, thanks for confirming.

lovell commented 8 years ago

@blowsie v0.12.1 now available via npm