Exemplo n.º 1
0
int getImageFromPPMStream( FILE* src, Image* im){
	long count;
	int c;
	unsigned char* d;

	int rh = readPPMFileHeader(src, im);
	if(rh < 0){  
		PrintError("Error Reading File Header");
		return -1;
	}else if(rh>0){ // No more data
		return 1;
	}

	im->bytesPerLine 	= im->width * im->bitsPerPixel/8;
	im->dataSize		= im->bytesPerLine * im->height;
	
	im->data			= (unsigned char**) mymalloc( im->width * im->height * im->bitsPerPixel/24 * 4 );
	if ( im->data == NULL )
	{
		PrintError("Not enough memory");
		return -1;
	}
	
	count = 	im->width * im->height * im->bitsPerPixel/24 * 3;
	
	myread(src,count,*(im->data));
	
	if( count != im->width * im->height * im->bitsPerPixel/24 * 3 )
	{
		PrintError("Error: Read %d instead of %d bytes",
			count, im->width * im->height * im->bitsPerPixel/24 * 3 );
		return -1;
	}
#ifndef BIGENDIAN
	if(im->bitsPerPixel == 48){
		switchBytes((char*)*(im->data), (long)im->width * im->height * 3);
	}
#endif

	ThreeToFourBPP( im );
	return 0;
}
Exemplo n.º 2
0
int readJPEG(Image * im, fullPath * sfile)
{
    struct jpeg_decompress_struct cinfo;
    struct jpeg_error_mgr jerr;
    FILE *infile;
    char filename[256];
    int scan_lines_to_be_read, scan_lines_read;
    unsigned int i, scanheight;
    unsigned char *data;
    JSAMPARRAY sarray;
    JOCTET *ptr = NULL;
    unsigned int size  = 0; 



#ifdef __Mac__
    unsigned char the_pcUnixFilePath[256];      // added by Kekus Digital
    Str255 the_cString;
    Boolean the_bReturnValue;
    CFStringRef the_FilePath;
    CFURLRef the_Url;           //till here
#endif

    //PrintError("%s", sfile->name);        

    cinfo.err = jpeg_std_error(&jerr);
    jpeg_create_decompress(&cinfo);

    // Prepare the library for reading the ICC profile, see its source code
    // for further information
    jpegICCSetupReadICCProfile(&cinfo);

    if (GetFullPath(sfile, filename))
        return -1;

#ifdef __Mac__
    CopyCStringToPascal(filename, the_cString); //Added by Kekus Digital
    the_FilePath =
        CFStringCreateWithPascalString(kCFAllocatorDefault, the_cString,
                                       kCFStringEncodingUTF8);
    the_Url =
        CFURLCreateWithFileSystemPath(kCFAllocatorDefault, the_FilePath,
                                      kCFURLHFSPathStyle, false);
    the_bReturnValue =
        CFURLGetFileSystemRepresentation(the_Url, true, the_pcUnixFilePath,
                                         256);

    strcpy(filename, the_pcUnixFilePath);       //till here
#endif

    if ((infile = fopen(filename, "rb")) == NULL) {
        PrintError("can't open %s", filename);
        return -1;
    }

    jpeg_stdio_src(&cinfo, infile);

    jpeg_read_header(&cinfo, TRUE);

    jpeg_start_decompress(&cinfo);

    SetImageDefaults(im);
    im->width = cinfo.output_width;
    im->height = cinfo.output_height;
    if (cinfo.output_components != 3)
    {
        PrintError("Image must be rgb");
        fclose(infile);
        return -1;
    }


    im->bitsPerPixel = 24;
    im->bytesPerLine = im->width * 3;
    im->dataSize = im->width * 4 * im->height;
    im->data = (unsigned char **) mymalloc((size_t) im->dataSize);
    if (im->data == NULL)
    {
        PrintError("Not enough memory");
        fclose(infile);
        return -1;
    }

    scanheight = cinfo.rec_outbuf_height;
    sarray = (JSAMPARRAY) malloc(scanheight * sizeof(JSAMPROW));

    scan_lines_to_be_read = im->height;
    data = *(im->data);

    while (scan_lines_to_be_read)
    {
        for (i = 0; i < scanheight; i++)
        {
            sarray[i] = (JSAMPROW) (data + i * im->bytesPerLine);
        }

        scan_lines_read = jpeg_read_scanlines(&cinfo, sarray, scanheight);

        scan_lines_to_be_read -= scan_lines_read;
        data += scan_lines_read * im->bytesPerLine;
    }

    // read ICC profile 
    
    if (jpegICCReadProfile(&cinfo, &ptr, &size)) {
        
        im->metadata.iccProfile.size = size;        
        im->metadata.iccProfile.data = (char*) ptr;
    }

    jpeg_finish_decompress(&cinfo);
    jpeg_destroy_decompress(&cinfo);

    ThreeToFourBPP(im);
    free(sarray);


    fclose(infile);

    return 0;


}