Exemplo n.º 1
0
 void write_image(
     const Image&                image,
     const string&               filepath)
 {
     GenericImageFileWriter writer;
     writer.write(filepath.c_str(), image);
 }
Exemplo n.º 2
0
bool Frame::write_image(
    const char*             file_path,
    const Image&            image,
    const ImageAttributes&  image_attributes) const
{
    assert(file_path);

    Image final_image(image);
    transform_to_output_color_space(final_image);

    Stopwatch<DefaultWallclockTimer> stopwatch;
    stopwatch.start();

    try
    {
        try
        {
            GenericImageFileWriter writer;
            writer.write(file_path, final_image, image_attributes);
        }
        catch (const ExceptionUnsupportedFileFormat&)
        {
            const string extension = lower_case(filesystem::path(file_path).extension());

            RENDERER_LOG_ERROR(
                "file format '%s' not supported, writing the image in OpenEXR format "
                "(but keeping the filename unmodified).",
                extension.c_str());

            EXRImageFileWriter writer;
            writer.write(file_path, final_image, image_attributes);
        }
    }
    catch (const ExceptionIOError&)
    {
        RENDERER_LOG_ERROR(
            "failed to write image file %s: i/o error.",
            file_path);

        return false;
    }
    catch (const Exception& e)
    {
        RENDERER_LOG_ERROR(
            "failed to write image file %s: %s.",
            file_path,
            e.what());

        return false;
    }

    stopwatch.measure();

    RENDERER_LOG_INFO(
        "wrote image file %s in %s.",
        file_path,
        pretty_time(stopwatch.get_seconds()).c_str());

    return true;
}
Exemplo n.º 3
0
    void render_curves_to_image(
        const BezierCurveType   curves[],
        const size_t            curve_count,
        const char*             filename,
        const bool              textured)
    {
        typedef typename BezierCurveType::ValueType ValueType;
        typedef typename BezierCurveType::VectorType VectorType;
        typedef typename BezierCurveType::MatrixType MatrixType;
        typedef Ray<ValueType, 3> RayType;
        typedef RayInfo<ValueType, 3> RayInfoType;
        typedef BezierCurveIntersector<BezierCurveType> BezierCurveIntersectorType;

        const size_t ImageWidth = 500;
        const size_t ImageHeight = 500;

        const ValueType RcpImageWidth = ValueType(1.0) / ImageWidth;
        const ValueType RcpImageHeight = ValueType(1.0) / ImageHeight;

        Image image(ImageWidth, ImageHeight, ImageWidth, ImageHeight, 3, PixelFormatFloat);

        for (size_t y = 0; y < ImageHeight; ++y)
        {
            for (size_t x = 0; x < ImageWidth; ++x)
            {
                Color3f color(0.0f);

                // Compute the coordinates of the center of the pixel.
                const ValueType pix_x = (ValueType(2.0) * x + ValueType(1.0)) * RcpImageWidth - ValueType(1.0);
                const ValueType pix_y = ValueType(1.0) - (ValueType(2.0) * y + ValueType(1.0)) * RcpImageHeight;

                // Build a ray. We assume the curve is on the x-y plane with z = 0.
                const RayType ray(
                    VectorType(pix_x, pix_y, ValueType(-3.0)),
                    VectorType(ValueType(0.0), ValueType(0.0), ValueType(1.0)));
                const RayInfoType ray_info(ray);

                for (size_t c = 0; c < curve_count; ++c)
                {
                    const BezierCurveType& curve = curves[c];

                    // Draw the curve.
                    MatrixType curve_transform;
                    make_curve_projection_transform(curve_transform, ray);
                    if (textured)
                    {
                        ValueType u, v, t = numeric_limits<ValueType>::max();
                        if (BezierCurveIntersectorType::intersect(curve, ray, curve_transform, u, v, t))
                        {
                            // Checkboard pattern.
                            const int b = (truncate<int>(4.0 * u) ^ truncate<int>(32.0 * v)) & 1;
                            color = b ? Color3f(0.8f) : Color3f(0.2f);
                        }
                    }
                    else
                    {
                        if (BezierCurveIntersectorType::intersect(curve, ray, curve_transform))
                        {
                            color[0] = 0.2f;
                            color[2] = 0.7f;
                        }
                    }

                    // Draw control points.
                    if (!textured)
                    {
                        const size_t control_point_count = curve.get_control_point_count();
                        for (size_t i = 0; i < control_point_count; ++i)
                        {
                            const VectorType& cp = curve.get_control_point(i);
                            const ValueType dx = pix_x - cp.x;
                            const ValueType dy = pix_y - cp.y;
                            if (square(dx) + square(dy) < square(ValueType(0.02)))
                            {
                                color = Color3f(1.0f, 1.0f, 0.0f);
                                break;
                            }
                        }
                    }
                }

                image.set_pixel(x, y, color);
            }
        }

        GenericImageFileWriter writer;
        writer.write(filename, image);
    }