Exemple #1
0
// Tests reading and writing of a single long variant from and to binary files
TEST_F(TestVariant, FileIOBinaryIOMultipleTypes) {
    BinaryFileStream stream;

    // Values we'll be testing for
    long value1 = 123;
    std::string value2 = "test";
    auto value3 = nullptr;

    // Initialize variants
    Variant variant1(value1);
    Variant variant2(value2);
    Variant variant3(value3);

    // Open stream for writing
    stream.open(filename, std::ios::out);

    // Write variants
    stream << variant1;
    stream << variant2;
    stream << variant3;

    // Close stream
    stream.close();

    // Open stream for reading
    stream.open(filename, std::ios::in);

    // Initialize a final variant for storage of data read from the stream
    Variant variant("a value that must not conflict with the tests");

    stream >> variant;
    EXPECT_EQ(variant, value1);

    stream >> variant;
    EXPECT_EQ(variant, value2);

    stream >> variant;
    EXPECT_EQ(variant, value3);

    // Lets make sure this was all the data. We need to read again to set the
    // EOF bit
    char tc;
    EXPECT_FALSE(stream.get(tc));

    // Ensure EOF
    EXPECT_TRUE(stream.eof());

    // Close stream
    stream.close();
}
Exemple #2
0
DTerr ImporterImageJPG::import(const FilePath &pathname, const std::string &args, DTuint &width, DTuint &height, std::shared_ptr<DTubyte> &data, DT3GLTextelFormat &format)
{
    
	//
	// The following is based on the libpng manual. http://www.libpng.org/pub/png/libpng-1.2.5-manual.html#section-3.1
	//

	//
	// Check the file header
	//

	// open the file
	BinaryFileStream file;
	DTerr err;
	if ((err = FileManager::open(file, pathname, true)) != DT3_ERR_NONE)
		return err;

    // This struct contains the JPEG decompression parameters and pointers to
    // working space (which is allocated as needed by the JPEG library).
    jpeg_decompress_struct cinfo;
    ImportSource           csrc;

    // We use our private extension JPEG error handler.
    // Note that this struct must live as long as the main JPEG parameter
    // struct, to avoid dangling-pointer problems.
    ImportErr jerr;

    cinfo.err = jpeg_std_error(&jerr.pub);
    jerr.pub.error_exit = ImporterImageJPG::error_handler;
    
    // Establish the setjmp return context for my_error_exit to use.
    if (setjmp(jerr.setjmp_buffer)) {
        // If we get here, the JPEG code has signaled an error.
        // We need to clean up the JPEG object, close the input file, and return.
        jpeg_destroy_decompress(&cinfo);
        file.close();
        return DT3_ERR_FILE_WRONG_TYPE;
    }

    //  Now we can initialize the JPEG decompression object.
    jpeg_create_decompress(&cinfo);
    cinfo.src = (jpeg_source_mgr*) &csrc;

    //  Step 2: specify data source (eg, a file)
    jpeg_stream_src(&cinfo, file);

    //  Step 3: read file parameters with jpeg_read_header()
    jpeg_read_header(&cinfo, TRUE);

    //  Step 4: set parameters for decompression

    //  In this example, we don't need to change any of the defaults set by
    //  jpeg_read_header(), so we do nothing here.

    //  Step 5: Start decompressor
    jpeg_start_decompress(&cinfo);
    
    ASSERT(cinfo.out_color_components == 3 && cinfo.data_precision == 8);
    
    // Build a buffer for reading in the image
    width = cinfo.output_width;
    height = cinfo.output_height;
    format = DT3GL_FORMAT_RGB;
    
    // Change data format to 16 bit
    data = std::shared_ptr<DTubyte>(new DTubyte[width * height * 3]);
	DTubyte *buffer = data.get();
    
    DTint row_stride = cinfo.output_width * cinfo.output_components;
    
    // Make a one-row-high sample array that will go away when done with image
    JSAMPARRAY scanline_buffer = (*cinfo.mem->alloc_sarray) ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
    
    // Step 6: while (scan lines remain to be read)
    // Here we use the library's state variable cinfo.output_scanline as the
    // loop counter, so that we don't have to keep track ourselves.

    while (cinfo.output_scanline < cinfo.output_height) {
        //jpeg_read_scanlines expects an array of pointers to scanlines.
        // Here the array is only one element long, but you could ask for
        // more than one scanline at a time if that's more convenient.

        jpeg_read_scanlines(&cinfo, scanline_buffer, 1);
        
        // Copy Row into buffer
        DTubyte *src_data = (DTubyte*) scanline_buffer[0];
        DTubyte *dst_data = (DTubyte*) &buffer[width * (height - cinfo.output_scanline) * 3];
        
        // Copy row
        ::memcpy(dst_data, src_data, row_stride);
    }

    // Step 7: Finish decompression
    jpeg_finish_decompress(&cinfo);

    // Step 8: Release JPEG decompression object
    // This is an important step since it will release a good deal of memory.
    jpeg_destroy_decompress(&cinfo);
    
//#endif

	return DT3_ERR_NONE;
}