From a4ed9c5edd9c57f47de8643cb6b9ead673d29757 Mon Sep 17 00:00:00 2001 From: Tj Holowaychuk Date: Fri, 13 Jan 2012 08:56:04 -0800 Subject: [PATCH] tweak styling --- src/Canvas.cc | 4 +-- src/JPEGStream.h | 74 ++++++++++++++++++++++++------------------------ 2 files changed, 38 insertions(+), 40 deletions(-) diff --git a/src/Canvas.cc b/src/Canvas.cc index ab86673..4c403f3 100644 --- a/src/Canvas.cc +++ b/src/Canvas.cc @@ -318,9 +318,7 @@ Canvas::StreamJPEGSync(const Arguments &args) { TryCatch try_catch; write_to_jpeg_stream(canvas->surface(), args[0]->NumberValue(), args[1]->NumberValue(), &closure); - if (try_catch.HasCaught()) { - return try_catch.ReThrow(); - } + if (try_catch.HasCaught()) return try_catch.ReThrow(); return Undefined(); } diff --git a/src/JPEGStream.h b/src/JPEGStream.h index 0085bd8..48ed5f0 100644 --- a/src/JPEGStream.h +++ b/src/JPEGStream.h @@ -29,13 +29,13 @@ init_closure_destination(j_compress_ptr cinfo){ boolean empty_closure_output_buffer(j_compress_ptr cinfo){ - closure_destination_mgr * dest = (closure_destination_mgr *) cinfo->dest; + closure_destination_mgr *dest = (closure_destination_mgr *) cinfo->dest; Local buf = Buffer::New(dest->bufsize); memcpy(Buffer::Data(buf->handle_), dest->buffer, dest->bufsize); Local argv[3] = { - Local::New(Null()), - Local::New(buf->handle_), - Integer::New(dest->bufsize) + Local::New(Null()) + , Local::New(buf->handle_) + , Integer::New(dest->bufsize) }; dest->closure->fn->Call(Context::GetCurrent()->Global(), 3, argv); cinfo->dest->next_output_byte = dest->buffer; @@ -45,23 +45,27 @@ empty_closure_output_buffer(j_compress_ptr cinfo){ void term_closure_destination(j_compress_ptr cinfo){ - closure_destination_mgr * dest = (closure_destination_mgr *) cinfo->dest; + closure_destination_mgr *dest = (closure_destination_mgr *) cinfo->dest; /* emit remaining data */ size_t remaining = dest->bufsize - cinfo->dest->free_in_buffer; Local buf = Buffer::New(remaining); memcpy(Buffer::Data(buf->handle_), dest->buffer, remaining); + Local data_argv[3] = { - Local::New(Null()), - Local::New(buf->handle_), - Integer::New(remaining) + Local::New(Null()) + , Local::New(buf->handle_) + , Integer::New(remaining) }; + dest->closure->fn->Call(Context::GetCurrent()->Global(), 3, data_argv); - /* emit 'end' */ + + // emit "end" Local end_argv[3] = { - Local::New(Null()), - Local::New(Null()), - Integer::New(0) + Local::New(Null()) + , Local::New(Null()) + , Integer::New(0) }; + dest->closure->fn->Call(Context::GetCurrent()->Global(), 3, end_argv); } @@ -72,10 +76,10 @@ jpeg_closure_dest(j_compress_ptr cinfo, closure_t * closure, int bufsize){ /* The destination object is made permanent so that multiple JPEG images * can be written to the same buffer without re-executing jpeg_mem_dest. */ - if (cinfo->dest == NULL) { /* first time for this JPEG object? */ + if (cinfo->dest == NULL) { /* first time for this JPEG object? */ cinfo->dest = (struct jpeg_destination_mgr *) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, - sizeof(closure_destination_mgr)); + sizeof(closure_destination_mgr)); } dest = (closure_destination_mgr *) cinfo->dest; @@ -93,33 +97,29 @@ jpeg_closure_dest(j_compress_ptr cinfo, closure_t * closure, int bufsize){ } void -write_to_jpeg_stream(cairo_surface_t *surface, int bufsize, int quality, closure_t * closure){ - /* - libjs code adapted from the Cairo R graphics project: - http://www.rforge.net/Cairo/ - */ +write_to_jpeg_stream(cairo_surface_t *surface, int bufsize, int quality, closure_t *closure){ int w = cairo_image_surface_get_width(surface); int h = cairo_image_surface_get_height(surface); - struct jpeg_compress_struct cinfo; - struct jpeg_error_mgr jerr; - - JSAMPROW slr; - cinfo.err = jpeg_std_error(&jerr); - jpeg_create_compress(&cinfo); - cinfo.in_color_space = JCS_RGB; - cinfo.input_components = 3; - cinfo.image_width = w; - cinfo.image_height = h; - jpeg_set_defaults(&cinfo); - jpeg_set_quality(&cinfo, quality, (quality<25)?0:1); + struct jpeg_compress_struct cinfo; + struct jpeg_error_mgr jerr; + + JSAMPROW slr; + cinfo.err = jpeg_std_error(&jerr); + jpeg_create_compress(&cinfo); + cinfo.in_color_space = JCS_RGB; + cinfo.input_components = 3; + cinfo.image_width = w; + cinfo.image_height = h; + jpeg_set_defaults(&cinfo); + jpeg_set_quality(&cinfo, quality, (quality<25)?0:1); jpeg_closure_dest(&cinfo, closure, bufsize); - jpeg_start_compress(&cinfo, TRUE); + jpeg_start_compress(&cinfo, TRUE); unsigned char *dst; - unsigned int *src = (unsigned int*)cairo_image_surface_get_data(surface); + unsigned int *src = (unsigned int *) cairo_image_surface_get_data(surface); int sl = 0; - dst = (unsigned char*) malloc(w*3); - while (sl