static THTensor * libsox_(read_audio_file)(const char *file_name) { // Create sox objects and read into int32_t buffer sox_format_t *fd; fd = sox_open_read(file_name, NULL, NULL, NULL); if (fd == NULL) abort_("[read_audio_file] Failure to read file"); int nchannels = fd->signal.channels; long buffer_size = fd->signal.length; int32_t *buffer = (int32_t *)malloc(sizeof(int32_t) * buffer_size); size_t samples_read = sox_read(fd, buffer, buffer_size); if (samples_read == 0) abort_("[read_audio_file] Empty file or read failed in sox_read"); // alloc tensor THTensor *tensor = THTensor_(newWithSize2d)(nchannels, samples_read / nchannels ); tensor = THTensor_(newContiguous)(tensor); real *tensor_data = THTensor_(data)(tensor); // convert audio to dest tensor int x,k; for (k=0; k<nchannels; k++) { for (x=0; x<samples_read/nchannels; x++) { *tensor_data++ = (real)buffer[x*nchannels+k]; } } // free buffer and sox structures sox_close(fd); free(buffer); THTensor_(free)(tensor); // return tensor return tensor; }
void write_png_file(boost::shared_array<uint8_t> img, mem_encode* state) { /* static int test = 1; char filename[100]; sprintf(filename, "test%d.png", test); ++test; FILE* f = fopen(filename, "wb+"); */ png_structp png_ptr; png_infop info_ptr; int number_of_passes; png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); png_bytep * row_pointers = (png_bytep*)png_malloc(png_ptr, 480 * sizeof(png_bytep)); for(int h = 0; h < 480; ++h) { (row_pointers)[h] = (png_bytep)((char*)img.get()+(h*640*2)); } /* initialize stuff */ if (!png_ptr) abort_("[write_png_file] png_create_write_struct failed"); /* if my_png_flush() is not needed, change the arg to NULL */ png_set_write_fn(png_ptr, state, my_png_write_data, NULL); info_ptr = png_create_info_struct(png_ptr); if (!info_ptr) abort_("[write_png_file] png_create_info_struct failed"); /* write header */ if (setjmp(png_jmpbuf(png_ptr))) abort_("[write_png_file] Error during writing header"); png_set_IHDR(png_ptr, info_ptr, 640, 480, 16, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); png_write_info(png_ptr, info_ptr); /* write bytes */ if (setjmp(png_jmpbuf(png_ptr))) abort_("[write_png_file] Error during writing bytes"); png_write_image(png_ptr, row_pointers); // /* end write */ if (setjmp(png_jmpbuf(png_ptr))) abort_("[write_png_file] Error during end of write"); png_write_end(png_ptr, NULL); /* cleanup heap allocation */ png_free (png_ptr, row_pointers); png_destroy_write_struct (&png_ptr, &info_ptr); //fclose(f); }
Image * pngfile_to_bytes(char* file_name) { printf("reading file %s\n",file_name); png_structp png_ptr; png_infop info_ptr, end_ptr; png_uint_32 width, height; int bit_depth; int color_type; int interlace_type; int i; FILE *fp = fopen(file_name, "rb"); if (!fp) { fprintf(stderr, "can't open %s\n", file_name); return 0; } /* initialize stuff */ png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); if (!png_ptr) abort_("[read_png_file] png_create_read_struct failed"); info_ptr = png_create_info_struct(png_ptr); if (!info_ptr) abort_("[read_png_file] png_create_info_struct failed"); end_ptr = png_create_info_struct(png_ptr); if (!end_ptr) abort_("[read_png_file] png_create_info_struct failed"); png_init_io(png_ptr, fp); png_set_sig_bytes(png_ptr, 0); png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL); png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, NULL, NULL); Image* img = (Image*)malloc(sizeof(Image)); img->w = width; img->h = height; img->hasAlpha = (color_type == PNG_COLOR_TYPE_RGBA); int bytes_per_pixel = 3; if(img->hasAlpha) { bytes_per_pixel = 4; } unsigned int row_bytes = png_get_rowbytes(png_ptr, info_ptr); img->data = (char*) malloc(row_bytes * height); png_bytepp row_pointers = png_get_rows(png_ptr, info_ptr); for (i = 0; i < height; i++) { memcpy(img->data + row_bytes*i, row_pointers[i], row_bytes); } png_destroy_read_struct(&png_ptr, &info_ptr, NULL); fclose(fp); return img; }
/* libpng calls this (at read_png_data's request) * to copy data from the in-RAM PNG into our bitmap */ void read_cb (png_structp png_ptr, png_bytep outBytes, png_size_t byteCountToRead) { struct bufdata * bd = png_get_io_ptr(png_ptr); if (bd == NULL) abort_("[read_png_file/read_cb] invalid memory passed to png reader"); if (bd->pos + byteCountToRead >= bd->len) abort_("[read_png_file/read_cb] attempting to read beyond end of buffer"); memcpy(outBytes, bd->buf+bd->pos, byteCountToRead); bd->pos += byteCountToRead; }
bool png_validate_file_and_query_size(size_t *img_width, size_t *img_height, const char *file_name) { bool ret = true; size_t width = -1; size_t height = -1; png_byte color_type; png_byte bit_depth; png_structp png_ptr; png_infop info_ptr; unsigned char header[8]; // 8 is the maximum size that can be checked FILE *fp = fopen(file_name, "rb"); if (!fp) abort_("[read_png_file] File %s could not be opened for reading", file_name); fread(header, 1, 8, fp); if (png_sig_cmp(header, 0, 8)) abort_("[read_png_file] File %s is not recognized as a PNG file", file_name); /* initialize stuff */ png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); if (!png_ptr) abort_("[read_png_file] png_create_read_struct failed"); info_ptr = png_create_info_struct(png_ptr); if (!info_ptr) abort_("[read_png_file] png_create_info_struct failed"); if (setjmp(png_jmpbuf(png_ptr))) abort_("[read_png_file] Error during init_io"); png_init_io(png_ptr, fp); png_set_sig_bytes(png_ptr, 8); png_read_info(png_ptr, info_ptr); width = png_get_image_width(png_ptr, info_ptr); height = png_get_image_height(png_ptr, info_ptr); color_type = png_get_color_type(png_ptr, info_ptr); bit_depth = png_get_bit_depth(png_ptr, info_ptr); if (color_type != PNG_COLOR_TYPE_RGB || bit_depth != 8) ret = false; png_destroy_read_struct(&png_ptr, &info_ptr, NULL); fclose(fp); *img_width = width; *img_height = height; return ret; }
int main(int argc, char **argv) { if (argc != 3) abort_("Usage: program_name <file_in> <file_out>"); // Read file + setup logic png_info_t* png_info = malloc(sizeof(png_info_t)); FILE *fp = read_png_file_stats(argv[1], png_info); row_pointers = (png_bytep*) malloc(sizeof(png_bytep) * png_info->height); for (int y=0; y < png_info->height; y++) row_pointers[y] = (png_byte*) malloc(png_get_rowbytes(png_info->png_ptr,png_info->info_ptr)); row_pointers_post_bh = (png_bytep*) malloc(sizeof(png_bytep) * png_info->height); for (int y=0; y < png_info->height; y++) row_pointers_post_bh[y] = (png_byte*) malloc(png_get_rowbytes(png_info->png_ptr,png_info->info_ptr)); row_pointers_post_bv = (png_bytep*) malloc(sizeof(png_bytep) * png_info->height); for (int y=0; y < png_info->height; y++) row_pointers_post_bv[y] = (png_byte*) malloc(png_get_rowbytes(png_info->png_ptr,png_info->info_ptr)); read_png_file(&row_pointers, png_info, fp); // Distributed preparation logic // Distribute chunks of png int global_height = png_info->height; int global_width = png_info->width; int local_min_height; int local_max_height; int local_min_width; int local_max_width; if (world_rank < world_size) { local_min_height = global_height / world_size * (world_rank); local_max_height = (global_height / world_size * (world_rank + 1)); } else { // last rank - takes extra local_min_height = global_height / world_size * (world_rank); local_max_height = global_height; } local_min_width = 0; local_max_width = global_width; distributed_info_t *distributed_info = malloc(sizeof(distributed_info_t)); distributed_info->local_min_height = local_min_height; distributed_info->local_max_height = local_max_height; distributed_info->local_min_width = local_min_width; distributed_info->local_max_width = local_max_width; // Define Need and Have regions (N and H) // Blur logic blur(png_info, distributed_info); // region goes from local_min_height to local_max_height - 1 // Write file + cleanup logic write_png_file(argv[2], &row_pointers_post_bv, png_info); free(distributed_info); for (int y = 0; y < png_info->height; y++) { free(row_pointers[y]); free(row_pointers_post_bh[y]); free(row_pointers_post_bv[y]); } free(row_pointers); free(row_pointers_post_bh); free(row_pointers_post_bv); free(png_info); return 0; }
GrfArray* grf_image_read_jpg(const char* filename){ struct jpeg_decompress_struct cinfo; struct my_error_mgr jerr; FILE* infile; uint8_t** buffer; // 2D Pointers to rows int row_stride; if((infile = fopen(filename, "rb")) == NULL){ abort_("[read_png_file] File %s could not be opened for reading", filename); } cinfo.err = jpeg_std_error(&jerr.pub); jerr.pub.error_exit = my_error_exit; if(setjmp(jerr.setjmp_buffer)){ jpeg_destroy_decompress(&cinfo); fclose(infile); return NULL; } jpeg_create_decompress(&cinfo); jpeg_stdio_src(&cinfo, infile); jpeg_read_header(&cinfo, TRUE); jpeg_start_decompress(&cinfo); uint32_t size[3] = {cinfo.output_height, cinfo.output_width, cinfo.output_components}; GrfArray* array = grf_array_new_with_size_type(3, size, GRF_UINT8); row_stride = size[1]*size[2]; buffer = (uint8_t**)malloc(sizeof(uint8_t*) * size[0]); uint32_t i; for(i = 0; i < size[0]; i++) buffer[i] = array->data_uint8 + row_stride * i; i = 0; while(cinfo.output_scanline < cinfo.output_height){ jpeg_read_scanlines(&cinfo, &buffer[i++], 1); } jpeg_finish_decompress(&cinfo); fclose(infile); free(buffer); return array; }
struct png_info compute_write_info(struct png_info read, int width, int height) { struct png_info write; /* If either width or height is -1, user is requesting us to preserve the aspect ratio: Set write.width, write.height so that: 1. read.width/read.height approx= write.width/write.height 2. write.width <= width 3. write.height <= height 4. Image is large as possible */ if (width == -1 && height > 0) { write.height = height; write.width = ROUND_DIV(write.height * read.width, read.height); } else if (width > 0 && height == -1) { write.width = width; write.height = ROUND_DIV(write.width * read.height, read.width); } else if (width <= 0 || height <= 0) { abort_("Invalid width/height"); } else { write.width = width; write.height = height; } if (write.width == 0) { write.width = 1; } if (write.height == 0) { write.height = 1; } write.bit_depth = 8; write.color_type = read.color_type & ~PNG_COLOR_MASK_PALETTE; return write; }
/** * Get any arguments from the command line. This should be cleaned up, because * it feels way hackier than it should. */ static void process_args(int argc, char **argv, char **src_path, int *n_points, int *n_polygons, double *percentage) { int c; int digit_optind = 0; static struct option long_options[] = { { "src", required_argument, 0, 0 }, { "sides", required_argument, 0, 0 }, { "npoly", required_argument, 0, 0 }, { "perc", required_argument, 0, 0 }, { NULL, 0, NULL, 0 } }; int option_index = 0; while ((c = getopt_long(argc, argv, "", long_options, &option_index)) != -1) { int this_option_optind = optind ? optind : 1; switch (c) { case 0: if (option_index == 0) { *src_path = optarg; } else if (option_index == 1) { *n_points = atoi(optarg); if (*n_points <= 2) abort_("Must have at least 3 points.\n"); else if (*n_points > MAX_POLYGON_POINTS) abort_("Must have <= %d points.\n", MAX_POLYGON_POINTS); } else if (option_index == 2) { *n_polygons = atoi(optarg); if (*n_polygons <= 0) abort_("Must have at least 1 polygon.\n"); } else if (option_index == 3) { *percentage = atof(optarg); } break; default: printf("Usage: %s\n", argv[0]); printf("\t--src <path to PNG src image>\n"); printf("\t--sides <# of polygon sides>\n"); printf("\t--npoly <# of polygons to generate>\n"); printf("\t--perc <Target accuracy percentage <= 100.0f>\n"); exit(0); break; } } }
void write_png_file(char* file_name) { /* create file */ FILE *fp = fopen(file_name, "wb"); if (!fp) abort_("[write_png_file] File %s could not be opened for writing", file_name); /* initialize stuff */ png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); if (!png_ptr) abort_("[write_png_file] png_create_write_struct failed"); info_ptr = png_create_info_struct(png_ptr); if (!info_ptr) abort_("[write_png_file] png_create_info_struct failed"); if (setjmp(png_jmpbuf(png_ptr))) abort_("[write_png_file] Error during init_io"); png_init_io(png_ptr, fp); /* write header */ if (setjmp(png_jmpbuf(png_ptr))) abort_("[write_png_file] Error during writing header"); png_set_IHDR(png_ptr, info_ptr, width, height, 8, 6, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); png_write_info(png_ptr, info_ptr); /* write bytes */ if (setjmp(png_jmpbuf(png_ptr))) abort_("[write_png_file] Error during writing bytes"); png_write_image(png_ptr, row_pointers); /* end write */ if (setjmp(png_jmpbuf(png_ptr))) abort_("[write_png_file] Error during end of write"); png_write_end(png_ptr, NULL); /* cleanup heap allocation */ for (y=0; y<height; y++) free(row_pointers[y]); free(row_pointers); fclose(fp); }
int main(int argc, char **argv) { if (argc != 4) abort_("Usage: program_name <file_in> <file_out> <shift>"); read_png_file(argv[1]); process_file(argv[3]); write_png_file(argv[2]); }
void png_read_color_image(unsigned char *img_data, size_t width, size_t height, const char *file_name) { int i; png_structp png_ptr; png_infop info_ptr; png_bytep * row_pointers; unsigned char header[8]; // 8 is the maximum size that can be checked FILE *fp = fopen(file_name, "rb"); if (!fp) abort_("[read_png_color] File %s could not be opened for reading", file_name); if (!img_data) abort_("[read_png_color] NULL input buffer"); fread(header, 1, 8, fp); if (png_sig_cmp(header, 0, 8)) abort_("[] File %s is not recognized as a PNG file", file_name); /* initialize stuff */ png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); if (!png_ptr) abort_("[%s] png_create_read_struct failed"); info_ptr = png_create_info_struct(png_ptr); if (!info_ptr) abort_("[read_png_file] png_create_info_struct failed"); if (setjmp(png_jmpbuf(png_ptr))) abort_("[read_png_file] Error during init_io"); png_init_io(png_ptr, fp); png_set_sig_bytes(png_ptr, 8); png_read_info(png_ptr, info_ptr); png_read_update_info(png_ptr, info_ptr); /* read file */ if (setjmp(png_jmpbuf(png_ptr))) abort_("[read_png_file] Error during read_image"); row_pointers = (png_bytep*) malloc(sizeof(png_bytep) * height); for (i = 0; i < height; i++) row_pointers[i] = (png_byte*) malloc(png_get_rowbytes(png_ptr,info_ptr)); png_read_image(png_ptr, row_pointers); for (i = 0; i < height; i++) { memcpy(img_data + (i * width * 3), row_pointers[i], width * 3); free(row_pointers[i]); } free(row_pointers); png_destroy_read_struct(&png_ptr, &info_ptr, NULL); fclose(fp); }
/** * Generate a random integer in a given range. Note that this is intended to be * inclusive (low & high are valid values). */ static int randrange(int low, int high) { double r; if (low > high) abort_("Lower boundary must be <= high boundary.\n"); r = ((double)rand() / (double)((double)RAND_MAX + 1.0)) * (high - low + 1) + low; return (int)r; }
int main(int argc, char **argv) { if (argc != 3) abort_("Usage: program_name <file_in> <file_out>"); // three of the above functions called here .... read_png_file(argv[1]); process_file(); write_png_file(argv[2]); return 0; }
int main(int argc, char **argv) { FILE *fin, *fout; if (argc < 3) { printf("Usage: vmuicon <icon.raw> <icon.c>\n"); exit(0); } fin = fopen(argv[1], "rb"); if (!fin) abort_("File %s could not be opened for reading", argv[1]); png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); if (!png_ptr) abort_("Failed to create PNG read struct\n"); info_ptr = png_create_info_struct(png_ptr); if (!info_ptr) abort_("Failed to create PNG info struct\n"); if (setjmp(png_jmpbuf(png_ptr))) { png_destroy_read_struct(&png_ptr, &info_ptr, NULL); fclose(fin); abort_("Error reading PNG\n"); } png_init_io(png_ptr, fin); png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_INVERT_MONO | PNG_TRANSFORM_PACKSWAP, NULL); if ((png_get_image_width(png_ptr, info_ptr) != IMAGE_WIDTH) || (png_get_image_height(png_ptr, info_ptr) != IMAGE_HEIGHT)) abort_("Image is not %ix%i pixels\n", IMAGE_WIDTH, IMAGE_HEIGHT); if ((png_get_color_type(png_ptr, info_ptr) != PNG_COLOR_TYPE_GRAY) || (png_get_bit_depth(png_ptr, info_ptr) != 1)) abort_("Image is not a grayscale image with bit depth 1\n"); rows = png_get_rows(png_ptr, info_ptr); fout = fopen(argv[2], "w+"); if (!fout) abort_("File %s could not be opened for writing", argv[2]); write_data(fout); png_destroy_read_struct(&png_ptr, &info_ptr, NULL); fclose(fin); fclose(fout); return 0; }
int getArgs(int argc, char **argv, Flags *flags) { int opt; /*while( -1 != (opt = getopt(argc, argv, "l:xs:p:d::hr:") ) )*/ while( -1 != (opt = getopt(argc, argv, "l:xs:p:d::hb:") ) ) { switch(opt) { case 'l': flags->ffileName = 1; flags->fileName = optarg; break; case 'x': flags->fexecute = 1; break; case 's': flags->fsize = 1; flags->size = atoi(optarg); // should check the type of optarg break; case 'p': flags->fpad = 1; flags->pad = atoi(optarg); break; /*case 'r': flags->fret = 1; flags->ret = optarg; break;*/ case 'd': flags->fdisplay = 1; if( 0 != optarg ) { flags->display = optarg; } else { flags->display = displayDefault; } break; case 'h': help(); break; case 'b': flags->fbadchar = 1; flags->badchar = optarg; break; case '?': abort_("[!] Syntax error\n"); break; } } checkArgs(*flags); return EXIT_SUCCESS; }
int main(int argc, char **argv) { if( ! testRast() ) { abort_("Test Failed"); } if (argc != 3) { abort_("Usage: program_name <file_out> <vector>"); } //Set Screen and Subsample int w ; int h ; int ss ; //Initialize the polygon soup vector< u_Poly< long , ushort > > polys ; //Read in the list of polygons read_in_vector( polys , argv[2] ,&w , &h , &ss ); int size_s = (int)polys.size(); //Report Number of polygons printf( "%i\n" , size_s ); //Initialize a Depth Buffer zbuff z(w,h,ss); //Rasterize the Scene rastBBox_vec_fix( polys , z ); //Write the Zbuffer to a png z.write_ppm( argv[1] ); return 0; }
/** * Calculate the weighted average of two integers. The weight argument is * applied to the second argument. * * Example: * weightedAverage(2, 10, 0.25) * = (1 - 0.25) x (2) + (0.25 x 10) * = (0.75 x 2) + (0.25 x 10) * = 1.5 + 2.5 * = 4 */ static unsigned char weightedAverage(int a, int b, float weight) { int result; if (unlikely(weight > 1.0f || weight < 0.0f)) abort_("Weight must be between 0 and 1 (%f)\n", weight); if (unlikely(a == -1 && b == -1)) abort_("%s: At least one of the arguments must be >= 0.\n", __func__); if (unlikely(a == -1)) return b; if (unlikely(b == -1)) return a; result = (unsigned char)( ((1.0-weight) * (float)a) + (weight * (float)b) ); return result; }
int checkArgs(Flags flags) { /*if( 1 == flags.fret ) { if( 1 != flags.ffileName ) // no ShellCode loaded { abort_("[!] You must define a shellcode file before resizing it\n"); } if( 8 != strlen(flags.ret) ) { abort_("[!] Return address must be exactly 4 bytes (eight characters)"); } // check the char set for ret: if( EXIT_FAILURE == isHexa(flags.ret) ) { abort_("[!] Return address must be hexadecimal format\n"); } }*/ if( 1 == flags.fbadchar ) { if( 0 != ( strlen(flags.badchar) % 2 ) ) { abort_("[!] Bad chars must be encoded in two digits modes\n"); } if( EXIT_FAILURE == isHexa(flags.badchar) ) { abort_("[!] Bad chars must be encoded in hexa format\n"); } } if( 1 == flags.fsize ) { if( 1 != flags.ffileName ) // no ShellCode loaded { abort_("[!] You must define a shellcode file before resizing it\n"); } } if( 1 == flags.fpad ) { if( 1 != flags.fsize ) { abort_("[!] You must define shellcode size before padding it.\n"); } } if( 1 == flags.fdisplay ) { if( 1 != flags.ffileName ) // no ShellCode loaded { abort_("[!] You must define a shellcode file before displaying it\n"); } } if( 1 == flags.fexecute ) { if( 1 != flags.ffileName ) // no ShellCode loaded { abort_("[!] You must define a shellcode file before executing it\n"); } } return EXIT_SUCCESS; }
void process_file(char* shift) { int shift_ = atoi(shift); if (info_ptr->color_type != PNG_COLOR_TYPE_RGBA) abort_("[process_file] color_type of input file must be PNG_COLOR_TYPE_RGBA (is %d)", info_ptr->color_type); width -= shift_; for (y=0; y<height; y++) { row_pointers[y] += 4 * shift_; } }
int main(int argc, char **argv) { if (argc != 3) abort_("Usage: program_name <file1> <file2>"); png_bytep * row_pointers; png_bytep * row_pointers2; row_pointers = read_png_file(argv[1]); row_pointers2 = read_png_file(argv[2]); process_file(row_pointers, row_pointers2); return 0; }
void read_png_file(char* file_name) { unsigned char header[8]; // 8 is the maximum size that can be checked /* open file and test for it being a png */ FILE *fp = fopen(file_name, "rb"); if (!fp) abort_("[read_png_file] File %s could not be opened for reading", file_name); fread(header, 1, 8, fp); if (png_sig_cmp(header, 0, 8)) abort_("[read_png_file] File %s is not recognized as a PNG file", file_name); /* initialize stuff */ png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); if (!png_ptr) abort_("[read_png_file] png_create_read_struct failed"); info_ptr = png_create_info_struct(png_ptr); if (!info_ptr) abort_("[read_png_file] png_create_info_struct failed"); if (setjmp(png_jmpbuf(png_ptr))) abort_("[read_png_file] Error during init_io"); png_init_io(png_ptr, fp); png_set_sig_bytes(png_ptr, 8); png_read_info(png_ptr, info_ptr); width = png_get_image_width(png_ptr, info_ptr); height = png_get_image_height(png_ptr, info_ptr); color_type = png_get_color_type(png_ptr, info_ptr); bit_depth = png_get_bit_depth(png_ptr, info_ptr); number_of_passes = png_set_interlace_handling(png_ptr); png_read_update_info(png_ptr, info_ptr); /* read file */ if (setjmp(png_jmpbuf(png_ptr))) abort_("[read_png_file] Error during read_image"); row_pointers = (png_bytep*) malloc(sizeof(png_bytep) * height); if (bit_depth == 16) rowbytes = width*8; else rowbytes = width*4; for (y=0; y<height; y++) row_pointers[y] = (png_byte*) malloc(rowbytes); png_read_image(png_ptr, row_pointers); fclose(fp); }
/* Given PNG-formatted data at bd, read the data into a buffer that we allocate * and return (row_pointers, here). * * Note: caller must free the returned value. */ png_bytep* read_png_file(png_structp png_ptr, png_infop * info_ptr, struct bufdata * bd) { int y; int height; png_byte bit_depth; png_bytep * row_pointers; if (png_sig_cmp(bd->buf, 0, 8)) abort_("[read_png_file] Input is not recognized as a PNG file"); *info_ptr = png_create_info_struct(png_ptr); if (!*info_ptr) abort_("[read_png_file] png_create_info_struct failed"); if (setjmp(png_jmpbuf(png_ptr))) abort_("[read_png_file] Error during init_io"); bd->pos = 0; png_set_read_fn(png_ptr, bd, read_cb); png_read_info(png_ptr, *info_ptr); height = png_get_image_height(png_ptr, *info_ptr); bit_depth = png_get_bit_depth(png_ptr, *info_ptr); if (bit_depth != 8) abort_("[read_png_file] bit depth 16 PNG files unsupported"); if (setjmp(png_jmpbuf(png_ptr))) abort_("[read_png_file] Error during read_image"); row_pointers = (png_bytep*) malloc(sizeof(png_bytep) * BUF_HEIGHT); for (y=0; y<height; y++) row_pointers[y] = (png_byte*) malloc(png_get_rowbytes(png_ptr, *info_ptr)); png_read_image(png_ptr, row_pointers); return row_pointers; }
/* libpng calls this (at read_png_data's request) * to copy data from the in-RAM PNG into our bitmap */ void read_cb (png_structp png_ptr, png_bytep outBytes, png_size_t byteCountToRead) { struct bufdata * bd = png_get_io_ptr(png_ptr); //pthread_mutex_lock(&lock_read_cb); if (bd == NULL) abort_("[read_png_file/read_cb] invalid memory passed to png reader"); if (bd->pos + byteCountToRead >= bd->len) { #ifdef _DEBUG_1_ printf("\nError (read_cb): bd->pos: %d, byteCountToRead: %d, bd->len: %d\n", bd->pos, byteCountToRead, bd->len); fflush(stdout); #endif abort_("[read_png_file/read_cb] attempting to read beyond end of buffer"); } #ifdef _DEBUG_1_ //printf("\read_cb: bd->pos: %d, byteCountToRead: %d, bd->len: %d\n", bd->pos, byteCountToRead, bd->len); //fflush(stdout); #endif memcpy(outBytes, bd->buf+bd->pos, byteCountToRead); bd->pos += byteCountToRead; //pthread_mutex_unlock(&lock_read_cb); }
void process_file(void) { if (png_get_color_type(png_ptr, info_ptr) == PNG_COLOR_TYPE_RGB) abort_("[process_file] input file is PNG_COLOR_TYPE_RGB but must be PNG_COLOR_TYPE_RGBA " "(lacks the alpha channel)"); if (png_get_color_type(png_ptr, info_ptr) != PNG_COLOR_TYPE_RGBA) abort_("[process_file] color_type of input file must be PNG_COLOR_TYPE_RGBA (%d) (is %d)", PNG_COLOR_TYPE_RGBA, png_get_color_type(png_ptr, info_ptr)); for (y=0; y<height; y++) { png_byte* row = row_pointers[y]; for (x=0; x<width; x++) { png_byte* ptr = &(row[x*4]); printf("Pixel at position [ %d - %d ] has RGBA values: %d - %d - %d - %d\n", x, y, ptr[0], ptr[1], ptr[2], ptr[3]); /* set red value to 0 and green value to the blue one */ ptr[0] = 0; ptr[1] = ptr[2]; } } }
/* write output_row_pointers back to PNG file as specified by file_name. */ void write_png_file(char* file_name, png_bytep * output_row_pointers) { png_structp png_ptr; png_infop info_ptr; /* create file */ FILE *fp = fopen(file_name, "wb"); if (!fp) abort_("[write_png_file] File %s could not be opened for writing", file_name); /* initialize stuff */ png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); if (!png_ptr) abort_("[write_png_file] png_create_write_struct failed"); info_ptr = png_create_info_struct(png_ptr); if (!info_ptr) abort_("[write_png_file] png_create_info_struct failed"); if (setjmp(png_jmpbuf(png_ptr))) abort_("[write_png_file] Error during init_io"); png_init_io(png_ptr, fp); /* write header */ if (setjmp(png_jmpbuf(png_ptr))) abort_("[write_png_file] Error during writing header"); png_set_IHDR(png_ptr, info_ptr, WIDTH, HEIGHT, 8, 6, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); png_write_info(png_ptr, info_ptr); /* write bytes */ if (setjmp(png_jmpbuf(png_ptr))) abort_("[write_png_file] Error during writing bytes"); png_write_image(png_ptr, output_row_pointers); if (setjmp(png_jmpbuf(png_ptr))) abort_("[write_png_file] Error during end of write"); png_write_end(png_ptr, NULL); //void png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr); png_destroy_write_struct(&png_ptr, &info_ptr); fclose(fp); }
void writePngFile(char* file_name) { /* create file */ FILE *fp = fopen(file_name, "wb"); if (!fp) abort_("[write_png_file] File %s could not be opened for writing", file_name); /* initialize stuff */ pngPtr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); if (!pngPtr) abort_("[write_png_file] png_create_write_struct failed"); infoPtr = png_create_info_struct(pngPtr); if (!infoPtr) abort_("[write_png_file] png_create_info_struct failed"); if (setjmp(png_jmpbuf(pngPtr))) abort_("[write_png_file] Error during init_io"); png_init_io(pngPtr, fp); /* write header */ if (setjmp(png_jmpbuf(pngPtr))) abort_("[write_png_file] Error during writing header"); png_set_IHDR(pngPtr, infoPtr, imageWidth, imageLength, bitsPerSample, config, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); png_write_info(pngPtr, infoPtr); /* write bytes */ if (setjmp(png_jmpbuf(pngPtr))) abort_("[write_png_file] Error during writing bytes"); png_write_image(pngPtr, rowPointers); /* end write */ if (setjmp(png_jmpbuf(pngPtr))) abort_("[write_png_file] Error during end of write"); png_write_end(pngPtr, NULL); fclose(fp); }
int main(int argc, char **argv) { if (argc != 3) abort_("Usage: program_name <file_in> <file_out>"); read_png_file(argv[1]); process_file(argv[2]); write_png_file("output.png"); /* int y; for (y=0; y<height; y++) free(row_pointers[y]); free(row_pointers); */ return 0; }
void RRCSSensor::Run() { auto thread_lambda = [this]() -> void { std::cout << "Starting " << name_ << std::endl; time_ = std::chrono::high_resolution_clock::now(); time_ += std::chrono::milliseconds(10000); bool queue_status = true; RRCSSensorMeasurement d; while(!abort_() && queue_status) { std::this_thread::sleep_until(time_); ReadSensor(d, time_); time_ += rate_; queue_status = update_(d); } }; thread_ = std::thread(thread_lambda); }
GrfArray* grf_image_read_png(const char* filename){ png_structp png_ptr; png_infop info_ptr; unsigned char header[8]; FILE* infile = fopen(filename, "rb"); if(!infile) abort_("[read_png_file] File %s could not be opened for reading", filename); fread(header, 1, 8, infile); if(png_sig_cmp(header, 0, 8)) abort_("[read_png_file] File %s is not recognized as a PNG file", filename); png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); if(!png_ptr) abort_("[read_png_file] png_create_read_struct failed"); info_ptr = png_create_info_struct(png_ptr); if(!info_ptr) abort_("[read_png_file] png_create_info_struct failed"); if(setjmp(png_jmpbuf(png_ptr))) abort_("[read_png_file] Error during init_io"); png_init_io(png_ptr, infile); png_set_sig_bytes(png_ptr, 8); png_read_info(png_ptr, info_ptr); uint32_t* size = malloc(sizeof(uint32_t) * 3); size[0] = png_get_image_height(png_ptr, info_ptr); size[1] = png_get_image_width(png_ptr, info_ptr); size[2] = png_get_channels(png_ptr, info_ptr); uint8_t bit_depth = png_get_bit_depth(png_ptr, info_ptr); GrfDataType type; switch (bit_depth) { case 16: type = GRF_UINT16; break; default: type = GRF_UINT8; break; } GrfArray* array = grf_array_new_with_size_type(3, size, type); png_read_update_info(png_ptr, info_ptr); // Read file if(setjmp(png_jmpbuf(png_ptr))) abort_("[read_png_file] Error during read_image"); uint8_t**buffer = (uint8_t**)malloc(sizeof(uint8_t*) * size[0]); size_t row_stride = png_get_rowbytes(png_ptr, info_ptr); uint32_t i; for(i = 0; i < size[0]; i++) buffer[i] = array->data_uint8 + row_stride * i; png_read_image(png_ptr, buffer); fclose(infile); free(buffer); return array; }