Browse Source

Added better image format detection

removed lame extension checks
v1.x
Tj Holowaychuk 14 years ago
parent
commit
8192e7e817
  1. 40
      src/Image.cc
  2. 4
      src/Image.h

40
src/Image.cc

@ -315,27 +315,35 @@ Image::error(Local<Value> err) {
/* /*
* Load cairo surface from the image src. * Load cairo surface from the image src.
* *
* TODO: better format detection
* TODO: support more formats * TODO: support more formats
* TODO: use node IO or at least thread pool * TODO: use node IO or at least thread pool
*/ */
cairo_status_t cairo_status_t
Image::loadSurface() { Image::loadSurface() {
switch (extension(filename)) { FILE *stream = fopen(filename, "r");
case Image::PNG: if (!stream) return CAIRO_STATUS_READ_ERROR;
return loadPNG(); uint8_t buf[5];
if (1 != fread(&buf, 5, 1, stream)) return CAIRO_STATUS_READ_ERROR;
fseek(stream, 0, SEEK_SET);
// png
if (isPNG(buf)) {
fclose(stream);
return loadPNG();
}
// gif
#ifdef HAVE_GIF #ifdef HAVE_GIF
case Image::GIF: if (isGIF(buf)) return loadGIF(stream);
return loadGIF();
#endif #endif
// jpeg
#ifdef HAVE_JPEG #ifdef HAVE_JPEG
case Image::JPEG: if (isJPEG(buf)) return loadJPEG(stream);
return loadJPEG();
#endif #endif
default:
return CAIRO_STATUS_READ_ERROR; return CAIRO_STATUS_READ_ERROR;
}
} }
/* /*
@ -386,10 +394,7 @@ read_gif_from_memory(GifFileType *gif, GifByteType *buf, int len) {
*/ */
cairo_status_t cairo_status_t
Image::loadGIF() { Image::loadGIF(FILE *stream) {
FILE *stream = fopen(filename, "r");
if (!stream) return CAIRO_STATUS_READ_ERROR;
fseek(stream, 0L, SEEK_END); fseek(stream, 0L, SEEK_END);
int len = ftell(stream); int len = ftell(stream);
fseek(stream, 0L, SEEK_SET); fseek(stream, 0L, SEEK_SET);
@ -612,10 +617,7 @@ Image::loadJPEGFromBuffer(uint8_t *buf, unsigned len) {
*/ */
cairo_status_t cairo_status_t
Image::loadJPEG() { Image::loadJPEG(FILE *stream) {
FILE *stream = fopen(filename, "r");
if (!stream) return CAIRO_STATUS_READ_ERROR;
// JPEG setup // JPEG setup
struct jpeg_decompress_struct info; struct jpeg_decompress_struct info;
struct jpeg_error_mgr err; struct jpeg_error_mgr err;

4
src/Image.h

@ -42,11 +42,11 @@ class Image: public node::ObjectWrap {
cairo_status_t loadPNG(); cairo_status_t loadPNG();
#ifdef HAVE_GIF #ifdef HAVE_GIF
cairo_status_t loadGIFFromBuffer(uint8_t *buf, unsigned len); cairo_status_t loadGIFFromBuffer(uint8_t *buf, unsigned len);
cairo_status_t loadGIF(); cairo_status_t loadGIF(FILE *stream);
#endif #endif
#ifdef HAVE_JPEG #ifdef HAVE_JPEG
cairo_status_t loadJPEGFromBuffer(uint8_t *buf, unsigned len); cairo_status_t loadJPEGFromBuffer(uint8_t *buf, unsigned len);
cairo_status_t loadJPEG(); cairo_status_t loadJPEG(FILE *stream);
#endif #endif
void error(Local<Value> error); void error(Local<Value> error);
void loaded(); void loaded();

Loading…
Cancel
Save