Example #1
0
bool ImageLoaderJPG::LoadParams(Image &cImage, File &cFile, bool bBlockSmoothing, bool bFancyUpsampling)
{
    jpeg_decompress_struct sInfo;
    jpeg_error_mgr sError;

    sInfo.err = jpeg_std_error(&sError);
    sInfo.err->error_exit = ExitErrorHandle;

    jpeg_create_decompress(&sInfo);

    // Set the user given parameters
    sInfo.do_block_smoothing  = bBlockSmoothing;
    sInfo.do_fancy_upsampling = bFancyUpsampling;

    jpeg_read_init(&sInfo, &cFile);

    jpeg_read_header(&sInfo, TRUE);
    jpeg_start_decompress(&sInfo);

    // Get the color format
    EColorFormat nColorFormat;
    switch (sInfo.num_components) {
    case 1:
        nColorFormat = ColorGrayscale;
        break;

    case 3:
        nColorFormat = ColorRGB;
        break;

    case 4:
        nColorFormat = ColorRGBA;
        break;

    default:
        // Error: Unsupported color format
        return false;
    }

    // Create image buffer
    ImageBuffer *pImageBuffer = cImage.CreatePart()->CreateMipmap();
    pImageBuffer->CreateImage(DataByte, nColorFormat, Vector3i(sInfo.output_width, sInfo.output_height, 1));

    // Read in the data
    uint8 *pCurrentData = pImageBuffer->GetData();
    while (sInfo.output_scanline < sInfo.output_height) {
        jpeg_read_scanlines(&sInfo, &pCurrentData, 1);
        pCurrentData += pImageBuffer->GetBytesPerRow();
    }

    // Cleanup
    jpeg_finish_decompress(&sInfo);
    jpeg_destroy_decompress(&sInfo);

    // Done
    return true;
}