Skip to content

Commit

Permalink
Change to FreeImage for image loading
Browse files Browse the repository at this point in the history
  • Loading branch information
puffnfresh committed May 28, 2011
1 parent 276235a commit 70877b4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 25 deletions.
8 changes: 2 additions & 6 deletions README.md
Expand Up @@ -12,10 +12,7 @@ Dependencies
* node.js
* SDL
* OpenGL
* DevIL

I'd like to try and get rid of the DevIL dependency when there are more node.js
image libraries.
* FreeImage

Building
--------
Expand All @@ -26,7 +23,7 @@ Testing
-------

node examples/example.js
chromium-browser --enable-webgl examples/example.html
chromium-browser examples/example.html

On Mac OS X, you have to run the following first:

Expand All @@ -37,7 +34,6 @@ Developing

* Add more WebGL functions!
* Make some node.js image libraries
* Replace DevIL with those node.js image libraries
* Find nice ways to remove the `nodejs` variable from example-include.js
* Add more events (e.g. keyboard)
* Make/port some WebGL examples
Expand Down
41 changes: 24 additions & 17 deletions src/image.h
Expand Up @@ -5,7 +5,7 @@

#include <node.h>

#include <IL/il.h>
#include <FreeImage.h>

using namespace v8;
using namespace node;
Expand All @@ -30,7 +30,7 @@ class Image : public EventEmitter {

target->Set(String::NewSymbol("Image"), t->GetFunction());

ilInit();
FreeImage_Initialise(true);
}

void
Expand All @@ -52,36 +52,43 @@ class Image : public EventEmitter {

int
GetBPP () {
ilBindImage(image_id);
return ilGetInteger(IL_IMAGE_BPP);
//return FreeImage_GetBPP(image_bmp);
return 32;
}

int
GetWidth () {
ilBindImage(image_id);
return ilGetInteger(IL_IMAGE_WIDTH);
return FreeImage_GetWidth(image_bmp);
}

int
GetHeight () {
ilBindImage(image_id);
return ilGetInteger(IL_IMAGE_HEIGHT);
return FreeImage_GetHeight(image_bmp);
}

void *
GetData () {
return data;
BYTE *pixels = FreeImage_GetBits(image_bmp);

// FreeImage stores data in BGR
// Convert from BGR to RGB
for(int i = 0; i < FreeImage_GetWidth(image_bmp) * FreeImage_GetHeight(image_bmp); i++)
{
BYTE temp = pixels[i * 4 + 0];
pixels[i * 4 + 0] = pixels[i * 4 + 2];
pixels[i * 4 + 2] = temp;
}

return pixels;
}

void
Load (const char *filename) {
this->filename = (char *)filename;

ilBindImage(image_id);
ilLoadImage(filename);
ilConvertImage(IL_RGB, IL_UNSIGNED_BYTE);

data = ilGetData();
FREE_IMAGE_FORMAT format = FreeImage_GetFileType(filename, 0);
image_bmp = FreeImage_Load(format, filename, 0);
image_bmp = FreeImage_ConvertTo32Bits(image_bmp);
}

protected:
Expand Down Expand Up @@ -157,16 +164,16 @@ class Image : public EventEmitter {
}

Image () : EventEmitter() {
ilGenImages(1, &image_id);
}

~Image () {
ilDeleteImages(1, &image_id);
if (image_bmp) FreeImage_Unload(image_bmp);
FreeImage_DeInitialise();
}

private:

ILuint image_id;
FIBITMAP *image_bmp;
char *filename;
void *data;

Expand Down
4 changes: 2 additions & 2 deletions wscript
Expand Up @@ -16,13 +16,13 @@ def configure(conf):
conf.env["CXXDEFINES"] = ["GL_GLEXT_PROTOTYPES"]
conf.check_cfg(package='gl', mandatory=True, args='--cflags --libs')
conf.check_cfg(package='sdl', mandatory=True, args='--cflags --libs')
conf.check_cfg(package='IL', mandatory=True, args='--cflags --libs')

def build(bld):
webgl = bld.new_task_gen("cxx", "shlib", "node_addon")
webgl.target = "webgl"
webgl.source = "src/webgl.cc"
webgl.uselib = ["GL", "SDL", 'IL']
webgl.lib = ['freeimage']
webgl.uselib = ["GL", "SDL"]

def shutdown():
# HACK to get binding.node out of build directory.
Expand Down

0 comments on commit 70877b4

Please sign in to comment.