int icv_write(icv_image_t *bif, const char *filename, ICV_IMAGE_FORMAT format) { /* FIXME: should not be introducing fixed size buffers */ char buf[BUFSIZ] = {0}; if (format == ICV_IMAGE_AUTO) { format = (ICV_IMAGE_FORMAT)icv_guess_file_format(filename, buf); } ICV_IMAGE_VAL_INT(bif); switch (format) { /* case ICV_IMAGE_BMP: return bmp_write(bif, filename); */ case ICV_IMAGE_PPM: return ppm_write(bif, filename); case ICV_IMAGE_PNG: return png_write(bif, filename); case ICV_IMAGE_PIX: return pix_write(bif, filename); case ICV_IMAGE_BW: return bw_write(bif, filename); case ICV_IMAGE_DPIX : return dpix_write(bif, filename); default: bu_log("Unrecognized format. Outputting in PIX format.\n"); } return pix_write(bif, filename); }
int write_image(const std::string& filename, const Image* image) { switch(image->type) { case IMAGE_TYPE::PNG: return png_write(filename, image->width, image->height, image->buffer); case IMAGE_TYPE::EXR: return (-1); default: return (10); } }
int main( int argc, char **argv ) { FILE *fp1, fp2; int out; unsigned char *dat1 = NULL; int wid1 = 0; int hei1 = 0; unsigned char *dat2 = NULL; int wid2 = 0; int hei2 = 0; if (argc > 3) { dat1 = LoadTex( argv[1], wid1, hei1 ); dat2 = LoadTex( argv[2], wid2, hei2 ); out = 3; } else { dat1 = LoadTex( argv[1], wid1, hei1 ); out = 2; } unsigned char *data = dat1; if (!dat1) return 0; if (argc > 3 && dat2) { assert( wid1 == wid2 ); assert( hei1 == hei2 ); data = (unsigned char*) malloc( sizeof (unsigned char)*wid1*hei1*4 ); for (int i = 0; i < wid1*hei1; i++) { data[i*4] = dat1[i*3]; data[i*4+1] = dat1[i*3+1]; data[i*4+2] = dat1[i*3+2]; data[i*4+3] = dat2[i*3]; } } png_write( argv[out], data, wid1, hei1, dat2 != NULL, 8 ); if (argc > 3 && dat2) free( dat1 ); if (dat2) free( dat2 ); return 0; }
bool png_image::save( std::ostream & stream, uint32_t flags ) { if (png_image_is_empty( this )) { return 0; } #ifdef PNG_APPLE_MODE_SUPPORTED png_set_apple_mode( flags ); #endif png_structp writePtr = png_create_write_struct( PNG_LIBPNG_VER_STRING, NULL, NULL, NULL ); if (!writePtr) { pngio_error( "Couldn't initialize PNG write struct." ); return 0; } png_set_write_fn( writePtr, (png_voidp) & stream, png_write_stream_data, png_flush_stream_data ); return png_write( writePtr, this, flags ); }
uint8_t png_image_save( png_image * image, FILE * file, uint32_t flags ) { if (png_image_is_empty( image )) { return 0; } #ifdef PNG_APPLE_MODE_SUPPORTED png_set_apple_mode( flags & PNG_IMAGE_OPTIMIZE_FOR_IOS ); #endif png_structp writePtr = png_create_write_struct( PNG_LIBPNG_VER_STRING, NULL, NULL, NULL ); if (!writePtr) { pngio_error( "Couldn't initialize PNG write struct." ); return 0; } png_set_write_fn( writePtr, (png_voidp) file, png_write_file_data, png_flush_file_data ); return png_write( writePtr, image, flags ); }
// method, which performs the block-matching for a given channel int block_matching (png_img* img, png_img* tmp, unsigned int const b_size, unsigned int const b_step, unsigned int const sigma, unsigned int const h_search, unsigned int const v_search, double const th_2d, double const tau_match, unsigned int const channel, unsigned int block_marking, list_t* list) { int i, j, k, l; block_t ref_block; block_t cmp_block; double d; // block distance group_t group = 0; // group, which holds a set of similar blocks int count = 0; // variable to add increasing numbers to the file names // allocate block memory if (new_block_struct(b_size, &ref_block) != 0) { return 1; } if (new_block_struct(b_size, &cmp_block) != 0) { return 1; } // compare blocks according to the sliding-window manner for (j=0; j<img->height; j=j+b_step) { for (i=0; i<img->width; i=i+b_step) { // obtain refernce block if (!exceeds_image_dimensions(img->width, img->height, b_size, i, j)) { if (get_block (img, channel, &ref_block, i-(b_size/2), j-(b_size/2)) != 0) { return 1; } if (append_block (&group, &ref_block, 0.0) != 0) { return 1; } if (block_marking) { png_copy_values (tmp, img); mark_search_window (tmp, &ref_block, h_search, v_search); mark_ref_block (tmp, &ref_block); } for (l=0; l<img->height; l=l+b_step) { for (k=0; k<img->width; k=k+b_step) { // obtain block to compare to reference block if (!(exceeds_image_dimensions(img->width, img->height, b_size, k, l)) && !(exceeds_search_window(img->width, img->height, b_size, h_search, v_search, i, j, k, l)) && !((i==k) && (j==l))) { // must be different from reference block if (get_block (img, channel, &cmp_block, k-(b_size/2), l-(b_size/2)) != 0) { return 1; } // compare blocks for similarity d = get_block_distance (&ref_block, &cmp_block, sigma, th_2d); // decide whether block similarity is sufficient if (d < tau_match*255) { if (append_block (&group, &cmp_block, d) != 0) { return 1; } if (block_marking) { mark_cmp_block (tmp, &cmp_block); } } } } } // add group of similar blocks to list if (append_group (list, &group) != 0) { return 1; } // write output image with marked group in it if (block_marking) { yuv2rgb(tmp); if (png_write(tmp, "img/rgb/grp/", "group", ++count) != 0) { return 1; } } group = 0; } } } return 0; }
int bm3d (char* const infile, // name of input file char* const kind, // kind of shrinkage (ht, wnr, avg) int const block_size, // size of internal processed blocks int const block_step, // step size between blocks int const sigma, // standard deviation of noise int const max_blocks, // maximum number of block in one 3D array int const h_search, // horizontal width of search window int const v_search, // vertical width of search window double const th_2d, // threshold for the 2D transformation double const tau_match, // match value for block-matching double const th_3d, // threshold for the 3D transformtaion int const block_marking) { // indicates the action of block marking png_img img; // noisy input image png_img org; // temporary image for marking the blocks FILE* log = 0; // log-file for all kinds of messages char logfile[30]; // name of the log-file including path char path[30]; // universally used path-name char prefix[20]; // universally used prefix-name char pure_name[30]; int h_search_true; // true value of horizontal search window size after validation int v_search_true; // true value of vertical search window size after validation list_t y_list = 0; // list of groups of the y-channel list_t u_list = 0; // list of groups of the u-channel list_t v_list = 0; // list of groups of the v-channel clock_t start, end; // time variables for counting durations double time; // ---------------------------------------------------------------------- // OPEN LOG-FILE FOR WRITING // ---------------------------------------------------------------------- // obtain filename without path and extension if (exclude_extension(infile, pure_name) != 0) { return 1; } sprintf (logfile, "log/log_%s_%s[%d].txt", pure_name, kind, sigma); log = fopen (logfile, "a"); if (log == NULL) { generate_error ("Unable to open log-file for writing ..."); return 1; } // ---------------------------------------------------------------------- // INPUT READING AND VALIDATION // ---------------------------------------------------------------------- // read input image if (png_read(&img, infile) != 0) { return 1; } // read temporary image if (png_read(&org, infile) != 0) { return 1; } // control color type if (img.color != PNG_COLOR_TYPE_RGB) { generate_error ("Wrong color type..."); return 1; } // control number of channels if (img.channels != 3) { generate_error ("Wrong number of channels..."); return 1; } // ---------------------------------------------------------------------- // PARAMETER VALIDATION // ---------------------------------------------------------------------- // verify kind of shrinkage if (strcmp(kind, "none") && strcmp(kind, "avg") && strcmp(kind, "ht") && strcmp(kind, "wnr")) { generate_error ("Unknown kind of shrinkage..."); return 1; } // verify block size if ((block_size!=7) && (block_size!=9) && (block_size!=11) && (block_size!=13)) { generate_error ("Wrong value for block size...\nValid values: 7, 9, 11, 13"); return 1; } // verify block step if ((block_step<5) || (block_step>15)) { generate_error ("Block step is out of valid Range...\nValid range: 5..15"); return 1; } // control search window dimensions h_search_true = ((h_search > img.width) || (h_search <= 0)) ? img.width : h_search; v_search_true = ((v_search > img.height) || (v_search <= 0)) ? img.height : v_search; // ---------------------------------------------------------------------- // PRINTING OF STATUS INFORMATION // ---------------------------------------------------------------------- fprintf (log, "-------------------------------------------------------------------------\n"); fprintf (log, "-------------------------------------------------------------------------\n"); fprintf (log, "[INFO] ... image dimensions: %dx%d\n", img.width, img.height); fprintf (log, "[INFO] ... kind of shrinkage: %s\n", kind); fprintf (log, "[INFO] ... block size: %d\n", block_size); fprintf (log, "[INFO] ... block step: %d\n", block_step); fprintf (log, "[INFO] ... sigma: %d\n", sigma); fprintf (log, "[INFO] ... maximum number of blocks: %d\n", max_blocks); fprintf (log, "[INFO] ... horizontal search window size: %d\n", h_search_true); fprintf (log, "[INFO] ... vertical search window size: %d\n", v_search_true); fprintf (log, "[INFO] ... threshold 2D: %f\n", th_2d); fprintf (log, "[INFO] ... tau-match 2D: %f\n", tau_match); fprintf (log, "[INFO] ... threshold 3D: %f\n", th_3d); fprintf (log, "[INFO] ... block marking: %s\n\n", block_marking ? "yes" : "no"); // ---------------------------------------------------------------------- // COLORSPACE CONVERSION & WRITEBACK // ---------------------------------------------------------------------- printf ("[INFO] ... launch of color conversion...\n"); rgb2yuv (&img); // write output image if (png_write(&img, "img/yuv/", "noisy_yuv", 0) != 0) { return 1; } printf ("[INFO] ... end of color conversion...\n\n"); fprintf (log, "[INFO] ... converted colorspace of input image to YUV...\n\n"); // ---------------------------------------------------------------------- // IMAGE-TO-ARRAY CONVERSION // ---------------------------------------------------------------------- printf ("[INFO] ... launch of printing image as three separate value arrays...\n"); printf ("[INFO] ... ... luminance channel...\n"); img2array (&img, 0, "img/", "y_channel_before"); printf ("[INFO] ... ... chrominance channel 1...\n"); img2array (&img, 1, "img/", "u_channel_before"); printf ("[INFO] ... ... chrominance channel 2...\n"); img2array (&img, 2, "img/", "v_channel_before"); printf ("[INFO] ... end of printing arrays...\n\n"); fprintf (log, "[INFO] ... printed every intput channel as array of values...\n\n"); // ---------------------------------------------------------------------- // BLOCK-MATCHING // ---------------------------------------------------------------------- printf ("[INFO] ... launch of block-matching...\n"); printf ("[INFO] ... ... luminance channel...\n"); start = clock(); if (block_matching(&img, &org, block_size, block_step, sigma, h_search_true, v_search_true, th_2d, tau_match, 0, block_marking, &y_list) != 0) { return 1; } printf ("[INFO] ... end of block-matching...\n\n"); end = clock(); time = (end - start) / (double)CLOCKS_PER_SEC; fprintf (log, "[INFO] ... block-matching accomplished...\n"); fprintf (log, "[INFO] ... ... elapsed time: %f\n", time); fprintf (log, "[INFO] ... ... number of groups in list: %d\n\n", list_length(&y_list)); // print recognized groups to file sprintf (path, "grp/org/%s/y/", kind); if (print_list(y_list, path, "group") != 0) { return 1; } // trim groups to maximal number of blocks printf ("[INFO] ... trimming groups to maximum size...\n\n"); if (trim_list(&y_list, max_blocks) != 0) { return 1; } fprintf (log, "[INFO] ... trimmed groups to maximum size of blocks...\n\n"); // obtain the pixel values from the u- and v-channel of the image printf ("[INFO] ... extracting blocks from chrominance channels...\n"); printf ("[INFO] ... ... chrominance channel 1...\n"); if (get_chrom(&img, &y_list, &u_list, 1)) { return 1; } printf ("[INFO] ... ... chrominance channel 2...\n\n"); if (get_chrom(&img, &y_list, &v_list, 2)) { return 1; } fprintf (log, "[INFO] ... extracted values from chrominance channels...\n\n"); // print trimmed groups to file sprintf (path, "grp/trm/%s/y/", kind); if (print_list(y_list, path, "group") != 0) { return 1; } sprintf (path, "grp/trm/%s/u/", kind); if (print_list(u_list, path, "group") != 0) { return 1; } sprintf (path, "grp/trm/%s/v/", kind); if (print_list(v_list, path, "group") != 0) { return 1; } // ---------------------------------------------------------------------- // IMAGE-DENOISING // ---------------------------------------------------------------------- printf ("[INFO] ... launch of shrinkage...\n"); start = clock(); printf ("[INFO] ... ... luminance channel...\n"); if (shrinkage(kind, &y_list, sigma, th_3d, 0) != 0) { return 1; } // printf ("[INFO] ... ... chrominance channel 1...\n"); // if (shrinkage(kind, &u_list, sigma, th_3d, 1) != 0) { // return 1; // } // printf ("[INFO] ... ... chrominance channel 2...\n"); // if (shrinkage(kind, &v_list, sigma, th_3d, 1) != 0) { // return 1; // } printf ("[INFO] ... end of shrinkage...\n\n"); end = clock(); time = (end - start) / (double)CLOCKS_PER_SEC; fprintf (log, "[INFO] ... accomplished shrinkage...\n"); fprintf (log, "[INFO] ... ... elapsed time: %f\n\n", time); sprintf (path, "grp/est/%s/y/", kind); if (print_list(y_list, path, "group") != 0) { return 1; } sprintf (path, "grp/est/%s/u/", kind); if (print_list(u_list, path, "group") != 0) { return 1; } sprintf (path, "grp/est/%s/v/", kind); if (print_list(v_list, path, "group") != 0) { return 1; } // ---------------------------------------------------------------------- // AGGREGATION // ---------------------------------------------------------------------- printf ("[INFO] ... launch of aggregation...\n"); start = clock(); printf ("[INFO] ... ... luminance channel...\n"); if (aggregate(kind, &img, &y_list, 0) != 0) { return 1; } // printf ("[INFO] ... chrominance channel 1...\n"); // if (aggregate(kind, &img, &u_list, 1) != 0) { // return 1; // } // printf ("[INFO] ... chrominance channel 2...\n"); // if (aggregate(kind, &img, &v_list, 2) != 0) { // return 1; // } printf ("[INFO] ... end of aggregation...\n\n"); end = clock(); time = (end - start) / (double)CLOCKS_PER_SEC; fprintf (log, "[INFO] ... accomplished aggregation...\n"); fprintf (log, "[INFO] ... ... elapsed time: %f\n\n", time); // ---------------------------------------------------------------------- // IMAGE-TO-ARRAY CONVERSION // ---------------------------------------------------------------------- printf ("[INFO] ... launch of printing image as three separate value arrays...\n"); printf ("[INFO] ... ... luminance channel...\n"); img2array (&img, 0, "img/", "y_channel_after"); printf ("[INFO] ... ... chrominance channel 1...\n"); img2array (&img, 1, "img/", "u_channel_after"); printf ("[INFO] ... ... chrominance channel 2...\n"); img2array (&img, 2, "img/", "v_channel_after"); printf ("[INFO] ... end of printing arrays...\n\n"); // ---------------------------------------------------------------------- // COLORSPACE CONVERSION & WRITEBACK // ---------------------------------------------------------------------- printf ("[INFO] ... launch of color conversion...\n"); yuv2rgb (&img); sprintf (prefix, "denoised_rgb_%s_%s", pure_name, kind); // write output image if (png_write(&img, "img/rgb/", prefix, sigma) != 0) { return 1; } printf ("[INFO] ... end of color conversion...\n\n"); fprintf (log, "[INFO] ... converted colorspace of output image to RGB...\n\n"); fprintf (log, "[INFO] ... PSNR after denoising: %fdB\n", get_snr(&org, &img)); fprintf (log, "-------------------------------------------------------------------------\n"); fprintf (log, "-------------------------------------------------------------------------\n\n\n"); // ---------------------------------------------------------------------- // FREEING DYNAMICALY ALLOCATED MEMORY // ---------------------------------------------------------------------- free_list (&y_list); free_list (&u_list); free_list (&v_list); png_free_mem (&img); png_free_mem (&org); fclose (log); return 0; }
static struct data *_get( const void *buf, size_t len ) { struct data *object_interface = & _interface; FILE *fp = NULL; long sizeof_png = 0; #ifdef HAVE_VIDEO const int W = VIDEO_FORMATS[0].width; const int H = VIDEO_FORMATS[0].height; #else const int W = DEFAULT_VIDEO_WIDTH; const int H = DEFAULT_VIDEO_HEIGHT; #endif #ifdef HAVE_TIMESTAMP static const char *TIME_FORMAT = "%y-%m-%d_%I:%M:%S%p"; // "2013-11-11 6:06:24PM" is 21 characters char tbuf[32]; const time_t ctime = time(NULL); struct tm ltime; localtime_r( &ctime, <ime ); if( strftime( tbuf, 32, TIME_FORMAT, <ime ) == 0 ) warnx( "strftime failed" ); #endif printf( "%s( %p, %ld )\n", __func__, buf, len ); #ifdef HAVE_CRYPTO crypto_init_iv( buf, len ); // currently entire Get payload is IV. #endif #ifdef HAVE_VIDEO if( _vci->snap( _vci, &_sizeof_vid_buffer, (uint8_t**)&_vid_buffer ) ) { warnx( "failed snapshot" ); return NULL; } yuyv2rgb( (const uint16_t *)_vid_buffer, W, H, _obj_buffer ); #else // Write "snow" into the raster line buffer. for(int i = 0; i < _sizeof_obj_buffer; i++ ) { _obj_buffer[i] = (uint8_t)( rand() % 256 ); } #endif #ifdef HAVE_TIMESTAMP _overlay_time_rgb( tbuf, _obj_buffer + ((H-charcell_height-2)*W + 2)*_samples_per_dcs_pixel, // assuming 8x17 W*_samples_per_dcs_pixel /* stride */); #endif fp = tmpfile(); if( NULL == fp ) { warn( "failed creating tmpfile" ); return NULL; } png_write( fp, _obj_buffer, W, H, "", _samples_per_dcs_pixel ); sizeof_png = ftell( fp ); _sizeof_obj // may be > sizeof_png... #ifdef HAVE_CRYPTO = crypto_sizeof_ciphertext( sizeof_png ); #else = sizeof_png;