Handle<Value> ImageView::height(const Arguments& args) { HandleScope scope; ImageView* im = ObjectWrap::Unwrap<ImageView>(args.This()); return scope.Close(Integer::New(im->get()->height())); }
Handle<Value> ImageView::getPixel(const Arguments& args) { HandleScope scope; unsigned x(0); unsigned y(0); if (args.Length() >= 2) { if (!args[0]->IsNumber()) return ThrowException(Exception::TypeError( String::New("first arg, 'x' must be an integer"))); if (!args[1]->IsNumber()) return ThrowException(Exception::TypeError( String::New("second arg, 'y' must be an integer"))); x = args[0]->IntegerValue(); y = args[1]->IntegerValue(); } else { return ThrowException(Exception::TypeError( String::New("must supply x,y to query pixel color"))); } ImageView* im = ObjectWrap::Unwrap<ImageView>(args.This()); image_view_ptr view = im->get(); if (x < view->width() && y < view->height()) { mapnik::image_view<mapnik::image_data_32>::pixel_type const * row = view->getRow(y); mapnik::image_view<mapnik::image_data_32>::pixel_type const pixel = row[x]; unsigned r = pixel & 0xff; unsigned g = (pixel >> 8) & 0xff; unsigned b = (pixel >> 16) & 0xff; unsigned a = (pixel >> 24) & 0xff; return Color::New(mapnik::color(r,g,b,a)); }
Handle<Value> ImageView::save(const Arguments& args) { HandleScope scope; if (!args.Length() >= 1 || !args[0]->IsString()){ return ThrowException(Exception::TypeError( String::New("filename required"))); } std::string filename = TOSTR(args[0]); std::string format(""); if (args.Length() >= 2) { if (!args[1]->IsString()) return ThrowException(Exception::TypeError( String::New("both 'filename' and 'format' arguments must be strings"))); format = mapnik::guess_type(TOSTR(args[1])); if (format == "<unknown>") { std::ostringstream s(""); s << "unknown output extension for: " << filename << "\n"; return ThrowException(Exception::Error( String::New(s.str().c_str()))); } } ImageView* im = ObjectWrap::Unwrap<ImageView>(args.This()); try { save_to_file(*im->get(),filename); } catch (const std::exception & ex) { return ThrowException(Exception::Error( String::New(ex.what()))); } catch (...) { return ThrowException(Exception::TypeError( String::New("unknown exception happened while saving an image, please submit a bug report"))); } return Undefined(); }
Handle<Value> ImageView::isSolid(const Arguments& args) { HandleScope scope; ImageView* im = ObjectWrap::Unwrap<ImageView>(args.This()); image_view_ptr view = im->get(); if (view->width() > 0 && view->height() > 0) { mapnik::image_view<mapnik::image_data_32>::pixel_type const* first_row = view->getRow(0); mapnik::image_view<mapnik::image_data_32>::pixel_type const first_pixel = first_row[0]; for (unsigned y = 0; y < view->height(); ++y) { mapnik::image_view<mapnik::image_data_32>::pixel_type const * row = view->getRow(y); for (unsigned x = 0; x < view->width(); ++x) { if (first_pixel != row[x]) { return scope.Close(Boolean::New(false)); } } } } return scope.Close(Boolean::New(true)); }