/* Can this PPM file be read with a mmap? */ static int isppmmmap( const char *filename ) { IMAGE *im; FILE *fp; int bits; int ascii; int msb_first; if( !(fp = im__file_open_read( filename, NULL, FALSE )) ) return( -1 ); if( !(im = im_open( "temp", "p" )) ) { fclose( fp ); return( 0 ); } if( read_header( fp, im, &bits, &ascii, &msb_first ) ) { im_close( im ); fclose( fp ); return( 0 ); } im_close( im ); fclose( fp ); return( !ascii && bits >= 8 ); }
/** * im_ppm2vips: * @filename: file to load * @out: image to write to * * Read a PPM/PBM/PGM/PFM file into a VIPS image. * It can read 1, 8, 16 and 32 bit images, colour or monochrome, * stored in binary or in ASCII. One bit images become 8 bit VIPS images, * with 0 and 255 for 0 and 1. * * PFM images have the scale factor attached as "pfm-scale". * * See also: #VipsFormat, im_vips2ppm(), im_meta_get_double() * * Returns: 0 on success, -1 on error. */ int im_ppm2vips( const char *filename, IMAGE *out ) { FILE *fp; /* Note that we open in binary mode. If this is a binary PPM, we need * to be able to mmap it. */ if( !(fp = im__file_open_read( filename, NULL, FALSE )) ) return( -1 ); if( parse_ppm( fp, filename, out ) ) { fclose( fp ); return( -1 ); } fclose( fp ); return( 0 ); }
static int ppm2vips_header( const char *filename, IMAGE *out ) { FILE *fp; int bits; int ascii; int msb_first; if( !(fp = im__file_open_read( filename, NULL, FALSE )) ) return( -1 ); if( read_header( fp, out, &bits, &ascii, &msb_first ) ) { fclose( fp ); return( -1 ); } fclose( fp ); return( 0 ); }
static Read * read_new( const char *name, IMAGE *out ) { Read *read; if( !(read = IM_NEW( NULL, Read )) ) return( NULL ); read->name = im_strdup( NULL, name ); read->out = out; read->fp = NULL; read->pPng = NULL; read->pInfo = NULL; read->row_pointer = NULL; read->data = NULL; if( !(read->fp = im__file_open_read( name, NULL )) ) { read_destroy( read ); return( NULL ); } if( !(read->pPng = png_create_read_struct( PNG_LIBPNG_VER_STRING, NULL, user_error_function, user_warning_function )) ) { read_destroy( read ); return( NULL ); } /* Catch PNG errors from png_create_info_struct(). */ if( setjmp( read->pPng->jmpbuf ) ) { read_destroy( read ); return( NULL ); } if( !(read->pInfo = png_create_info_struct( read->pPng )) ) { read_destroy( read ); return( NULL ); } return( read ); }
/** * im_read_dmask: * @filename: read matrix from this file * * Reads a matrix from a file. * * Matrix files have a simple format that's supposed to be easy to create with * a text editor or a spreadsheet. * * The first line has four numbers for width, height, scale and * offset (scale and offset may be omitted, in which case they default to 1.0 * and 0.0). Scale must be non-zero. Width and height must be positive * integers. The numbers are separated by any mixture of spaces, commas, * tabs and quotation marks ("). The scale and offset fields may be * floating-point, and must use '.' * as a decimal separator. * * Subsequent lines each hold one line of matrix data, with numbers again * separated by any mixture of spaces, commas, * tabs and quotation marks ("). The numbers may be floating-point, and must * use '.' * as a decimal separator. * * Extra characters at the ends of lines or at the end of the file are * ignored. * * See also: im_read_imask(), im_gauss_dmask(). * * Returns: the loaded mask on success, or NULL on error. */ DOUBLEMASK * im_read_dmask( const char *filename ) { FILE *fp; double sc, off; int xs, ys; DOUBLEMASK *out; int x, y, i; char buf[MAX_LINE]; if( !(fp = im__file_open_read( filename, NULL, TRUE )) ) return( NULL ); if( read_header( fp, &xs, &ys, &sc, &off ) ) { fclose( fp ); return( NULL ); } if( !(out = im_create_dmask( filename, xs, ys )) ) { fclose( fp ); return( NULL ); } out->scale = sc; out->offset = off; for( i = 0, y = 0; y < ys; y++ ) { char *p; if( get_line( fp, buf ) ) { im_free_dmask( out ); fclose( fp ); return( NULL ); } for( p = buf, x = 0; p && x < xs; x++, i++, p = im_break_token( p, " \t,\";" ) ) out->coeff[i] = g_ascii_strtod( p, NULL ); } fclose( fp ); return( out ); }
/* Read a JPEG file into a VIPS image. */ static int jpeg2vips( const char *name, IMAGE *out, gboolean header_only ) { char filename[FILENAME_MAX]; char mode[FILENAME_MAX]; char *p, *q; int shrink; struct jpeg_decompress_struct cinfo; ErrorManager eman; FILE *fp; int result; gboolean invert_pels; gboolean fail_on_warn; /* By default, we ignore any warnings. We want to get as much of * the user's data as we can. */ fail_on_warn = FALSE; /* Parse the filename. */ im_filename_split( name, filename, mode ); p = &mode[0]; shrink = 1; if( (q = im_getnextoption( &p )) ) { shrink = atoi( q ); if( shrink != 1 && shrink != 2 && shrink != 4 && shrink != 8 ) { im_error( "im_jpeg2vips", _( "bad shrink factor %d" ), shrink ); return( -1 ); } } if( (q = im_getnextoption( &p )) ) { if( im_isprefix( "fail", q ) ) fail_on_warn = TRUE; } /* Make jpeg dcompression object. */ cinfo.err = jpeg_std_error( &eman.pub ); eman.pub.error_exit = new_error_exit; eman.pub.output_message = new_output_message; eman.fp = NULL; if( setjmp( eman.jmp ) ) { /* Here for longjmp() from new_error_exit(). */ jpeg_destroy_decompress( &cinfo ); return( -1 ); } jpeg_create_decompress( &cinfo ); /* Make input. */ if( !(fp = im__file_open_read( filename, NULL, FALSE )) ) return( -1 ); eman.fp = fp; jpeg_stdio_src( &cinfo, fp ); /* Need to read in APP1 (EXIF metadata) and APP2 (ICC profile). */ jpeg_save_markers( &cinfo, JPEG_APP0 + 1, 0xffff ); jpeg_save_markers( &cinfo, JPEG_APP0 + 2, 0xffff ); /* Convert! */ result = read_jpeg_header( &cinfo, out, &invert_pels, shrink ); if( !header_only && !result ) result = read_jpeg_image( &cinfo, out, invert_pels ); /* Close and tidy. */ fclose( fp ); eman.fp = NULL; jpeg_destroy_decompress( &cinfo ); if( eman.pub.num_warnings != 0 ) { if( fail_on_warn ) { im_error( "im_jpeg2vips", "%s", im_error_buffer() ); result = -1; } else { im_warn( "im_jpeg2vips", _( "read gave %ld warnings" ), eman.pub.num_warnings ); im_warn( "im_jpeg2vips", "%s", im_error_buffer() ); } } return( result ); }