void photoalbum::api::map::map_tile_km( const jsonrpc::request& request, jsonrpc::result& result, sqlite::connection& conn ) { const std::string region = request.params().get<std::string>(0); const int eastings = request.params().get<int>(1); const int northings = request.params().get<int>(2); photoalbum::map::tile_data_db data_db; db::get( conn, region, eastings/10, northings/10, data_db ); try { Magick::Image image(Magick::Blob( reinterpret_cast<const void*>(&(data_db.data[0])), data_db.data.size() ) ); Magick::Geometry out_size( image.size().width()/5, image.size().height()/5 ); Magick::Image out_image(out_size, Magick::Color(255,255,255)); int off_x = -(out_size.width() * (eastings%5)); int off_y = -(out_size.height() * (4 - (northings%5))); out_image.composite(image, off_x, off_y); Magick::Blob blob; out_image.write(&blob, "PNG"); png_data(result.data()).data() = base64::encode( reinterpret_cast<const unsigned char*>(blob.data()), blob.length() ); } catch(const std::exception& e) { } }
int unfli ( FILE *input, char *out_name_base, char *out_name_ext, int outtype, int begin_frame, int max_frames, int verbose, int compress_flag ) { unsigned char file_header[FILE_HEAD_SIZE]; unsigned char frame_header[FRAME_HEAD_SIZE]; unsigned char *frame_buffer; int buffer_len; int work_len; int file_len, file_type, frame_len, frame_type; int n, nframes; int last_frame, digits, help; int nchunks; if (fread(file_header, 1, FILE_HEAD_SIZE, input) != FILE_HEAD_SIZE) return(-1); file_len = get_ulong(&file_header[0]); if (verbose) fprintf(stdout,"Size of input file: %d\n", file_len); file_type = get_ushort(&file_header[4]); if (verbose) fprintf(stdout,"Type of input file: 0x%X\n", file_type); if (!((file_type == FLI_FILE_MAGIC) || (file_type == FLC_FILE_MAGIC))) { fprintf(stdout,"Unknown file type!\n"); return(0); } nframes = get_ushort(&file_header[6]); image_width = get_ushort(&file_header[8]); image_height = get_ushort(&file_header[10]); if (verbose) { fprintf(stdout,"Number of frames: %d\n", nframes); fprintf(stdout,"Width: %d\n", image_width); fprintf(stdout,"Height: %d\n", image_height); } buffer_len = image_width * image_height; frame_buffer = malloc(buffer_len); image_buffer = malloc(buffer_len); if ((frame_buffer == NULL) || (image_buffer == NULL)) return(-2); memset(image_buffer, 0, buffer_len); memset(palette, 0, MAXCOLORS * 3); if (max_frames <= 0) max_frames = nframes; nframes++; if ((begin_frame < 1) || (begin_frame > nframes)) { fprintf(stderr,"Begin frame-number %d out of range (1 .... %d)\n", begin_frame, nframes); return(0); } last_frame = begin_frame + max_frames - 1; if (last_frame > nframes) last_frame = nframes; help = nframes; digits=0; while (help > 0) { help = (help - (help % 10))/10; digits++; } if (digits < 3) digits = 3; if (last_frame == begin_frame) fprintf(stdout,"Extracting frame %d\n",begin_frame); else fprintf(stdout,"Extracting frames from %d to %d\n", begin_frame, last_frame); for (n = 1; n <= nframes; n++) { if (n > last_frame) break; if (fread(frame_header, 1, FRAME_HEAD_SIZE, input) != FRAME_HEAD_SIZE) return(-1); frame_len = get_ulong(&frame_header[0]); if (verbose) fprintf(stdout,"Frame %d -- size: %d\n", n, frame_len); frame_type = get_ushort(&frame_header[4]); if (frame_type != FLI_FRAME_MAGIC) { fprintf(stderr,"Invalid frame type: 0x%X\n", frame_type); return(0); } work_len = frame_len - FRAME_HEAD_SIZE; if (work_len > 0) { if (buffer_len < work_len) { free(frame_buffer); buffer_len = work_len; frame_buffer = malloc(buffer_len); if (frame_buffer == NULL) return(-2); } if (fread(frame_buffer, 1, work_len, input) != work_len) return(-1); nchunks = get_ushort(&frame_header[6]); process_frame(frame_buffer, nchunks, verbose); } if (n >= begin_frame) { if (!out_image(image_buffer,palette,image_width,image_height, outtype,out_name_base,out_name_ext,digits,n, compress_flag)) return(0); } } return(1); }
int main(int argc, char** argv){ if(argc < 3){ std::cout << "usage:" << argv[0] << " src.png dst.png" << std::endl; return 1; } int mode = 0; // for cuda if(argc == 4){ // this is silly implementation char* m = argv[3]; mode = m[0] - '0'; } const char* input_file = argv[1]; const char* output_file = argv[2]; std::vector<unsigned char> in_image; unsigned int width, height; const unsigned input_error = lodepng::decode(in_image, width, height, input_file); if(input_error){ std::cout << "decoder error " << input_error << ": " << lodepng_error_text(input_error) << std::endl; return 1; } unsigned char* input_image = new unsigned char[in_image.size()]; unsigned char* output_image = new unsigned char[in_image.size()]; std::copy(in_image.begin(), in_image.end(), input_image); // first CUDA call takes a whlie. // For benchmark, I do not take account of first call. const int N = 5; long sum_of_n_minus_one = 0; for(int i=0;i<N;i++){ struct timeval st; struct timeval et; gettimeofday(&st, NULL); if(mode == 0){ rgb_invert_in_cuda(output_image, input_image, width, height); }else if(mode == 1){ rgb_invert_in_cpu(output_image, input_image, width, height); }else if(mode == 2){ rgb_invert_in_cuda_uchar_array(output_image, input_image, width, height); }else{ std::cout << "ERROR: unknown mode: " << mode << std::endl; return 1; } gettimeofday(&et, NULL); long us = time_diff_us(st, et); if(i != 0){ sum_of_n_minus_one += us; } } printf("%lu\n",sum_of_n_minus_one/(N-1)); std::vector<unsigned char> out_image(output_image, output_image+in_image.size()); unsigned output_error = lodepng::encode(output_file, out_image, width, height); if(output_error){ std::cout << "encoder error " << output_error << ": "<< lodepng_error_text(output_error) << std::endl; return 1; } return 0; }
// [[myRcpp::export]] RcppExport SEXP itkConvolveImage( SEXP r_in_image1 , SEXP r_in_image2 ) { if( r_in_image1 == NULL || r_in_image2 == NULL ) { Rcpp::Rcout << " Invalid Arguments: convolveImage.cpp " << std::endl ; Rcpp::wrap( 1 ) ; } Rcpp::S4 in_image1( r_in_image1 ) ; Rcpp::S4 in_image2( r_in_image2 ) ; std::string in_pixeltype = Rcpp::as< std::string >( in_image1.slot( "pixeltype" ) ) ; unsigned int dimension = Rcpp::as< unsigned int >( in_image1.slot( "dimension" ) ) ; std::string in_pixeltype2 = Rcpp::as< std::string >( in_image2.slot( "pixeltype" ) ) ; unsigned int dimension2 = Rcpp::as< unsigned int >( in_image2.slot( "dimension" ) ) ; if ( ( dimension != dimension2 ) || ( in_pixeltype.compare(in_pixeltype2) != 0 ) ) { Rcpp::Rcout << " Images must have equivalent dimensionality & pixel type" << std::endl ; Rcpp::wrap( 1 ); } // make new out image, result of convolution Rcpp::S4 out_image( std::string( "antsImage" ) ) ; out_image.slot( "pixeltype" ) = in_pixeltype ; out_image.slot( "dimension" ) = dimension ; if ( dimension == 2 ) { typedef itk::Image< float , 2 > ImageType; typedef ImageType::Pointer ImagePointerType; Rcpp::XPtr< ImagePointerType > antsimage_xptr1( static_cast< SEXP >( in_image1.slot( "pointer" ) ) ) ; Rcpp::XPtr< ImagePointerType > antsimage_xptr2( static_cast< SEXP >( in_image2.slot( "pointer" ) ) ) ; ImagePointerType* out_image_ptr_ptr = new ImagePointerType( convolveImageHelper<ImageType>( *antsimage_xptr1,*antsimage_xptr2 ) ); Rcpp::XPtr< ImagePointerType > out_image_xptr( out_image_ptr_ptr , true ); out_image.slot( "pointer" ) = out_image_xptr; } else if ( dimension == 3 ) { typedef itk::Image< float , 3 > ImageType; typedef ImageType::Pointer ImagePointerType; Rcpp::XPtr< ImagePointerType > antsimage_xptr1( static_cast< SEXP >( in_image1.slot( "pointer" ) ) ) ; Rcpp::XPtr< ImagePointerType > antsimage_xptr2( static_cast< SEXP >( in_image2.slot( "pointer" ) ) ) ; ImagePointerType* out_image_ptr_ptr = new ImagePointerType( convolveImageHelper<ImageType>( *antsimage_xptr1,*antsimage_xptr2 ) ); Rcpp::XPtr< ImagePointerType > out_image_xptr( out_image_ptr_ptr , true ); out_image.slot( "pointer" ) = out_image_xptr; } else if ( dimension == 4 ) { typedef itk::Image< float , 4 > ImageType; typedef ImageType::Pointer ImagePointerType; Rcpp::XPtr< ImagePointerType > antsimage_xptr1( static_cast< SEXP >( in_image1.slot( "pointer" ) ) ) ; Rcpp::XPtr< ImagePointerType > antsimage_xptr2( static_cast< SEXP >( in_image2.slot( "pointer" ) ) ) ; ImagePointerType* out_image_ptr_ptr = new ImagePointerType( convolveImageHelper<ImageType>( *antsimage_xptr1,*antsimage_xptr2 ) ); Rcpp::XPtr< ImagePointerType > out_image_xptr( out_image_ptr_ptr , true ); out_image.slot( "pointer" ) = out_image_xptr; } else Rcpp::Rcout << " Dimension " << dimension << " is not supported " << std::endl; return out_image; }