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.
*
* TODO: better format detection
* TODO: support more formats
* TODO: use node IO or at least thread pool
*/
cairo_status_t
Image::loadSurface() {
switch (extension(filename)) {
case Image::PNG:
return loadPNG();
FILE *stream = fopen(filename, "r");
if (!stream) return CAIRO_STATUS_READ_ERROR;
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
case Image::GIF:
return loadGIF();
if (isGIF(buf)) return loadGIF(stream);
#endif
// jpeg
#ifdef HAVE_JPEG
case Image::JPEG:
return loadJPEG();
if (isJPEG(buf)) return loadJPEG(stream);
#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
Image::loadGIF() {
FILE *stream = fopen(filename, "r");
if (!stream) return CAIRO_STATUS_READ_ERROR;
Image::loadGIF(FILE *stream) {
fseek(stream, 0L, SEEK_END);
int len = ftell(stream);
fseek(stream, 0L, SEEK_SET);
@ -612,10 +617,7 @@ Image::loadJPEGFromBuffer(uint8_t *buf, unsigned len) {
*/
cairo_status_t
Image::loadJPEG() {
FILE *stream = fopen(filename, "r");
if (!stream) return CAIRO_STATUS_READ_ERROR;
Image::loadJPEG(FILE *stream) {
// JPEG setup
struct jpeg_decompress_struct info;
struct jpeg_error_mgr err;

4
src/Image.h

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

Loading…
Cancel
Save