Working with Transparency in PostScript | C++
Add transparency in PS document
PostScript doesn’t support transparency in painting vector graphics objects. However, translucent (partially transparent) images can be rendered as a set of fully transparent and fully opaque pixels. Such images are called masks.
Aspose.Page for C++ library offers a method that adds the transparent image to the PS document. As for painting vector graphics: shapes or text, we offer “pseudo-transparency”.
“Pseudo-transparency” is a process of paling colors that have an Alpha component of less than 255. It is reached by the specific blending of Red, Green, and Blue components with Alpha one.
“Pseudo-transparency”, of course, doesn’t allow us to see the lower colored layer from under the upper transparent layer, but makes an illusion of transparency if the bottom layer is white.
Add transparent image in PS document
As we wrote earlier transparent images can be added to the PS document as a mask and Aspose.Page for C++ library offers for this purpose a method AddTransparentImage(). This method recognizes whether the image is fully opaque or fully transparent or translucent. If it is fully opaque it is added as the opaque image in AddImage() method, if it is fully transparent it is not added to the document at all, if it is the translucent image it is added as a PostScript image mask.
In the example below we demonstrate the difference between adding a transparent image in a PS document with AddImage() and AddTransparentImage(). In order to see the white translucent image we set the page background color to non-white.
In order to add any image to a new PsDocument with Aspose.Page for C++ library in this example we do the following steps:
- Create an output stream for the resulting PS file.
- Create PsSaveOptions object with default options. Change the background color if it is required.
- Create a 1-paged PsDocument with an already created output stream and save options.
- Create a new graphics state.
- Create Bitmap from image file.
- Create the necessary transformation for the image.
- Add the image to PsDocument as a fully opaque image (using AddImage() method) if we are sure that the image is opaque or add one as a transparent image (using AddTransparentImage() method) if we are not sure that the image is opaque.
- Exit from the current graphics state to upper level one.
- Close the page.
- Save the document.
1 // The path to the documents directory.
2 System::String dataDir = RunExamples::GetDataDir_WorkingWithTransparency();
3
4 //Create output stream for PostScript document
5 {
6 System::SharedPtr<System::IO::Stream> outPsStream = System::MakeObject<System::IO::FileStream>(dataDir + u"AddTransparentImage_outPS.ps", System::IO::FileMode::Create);
7 // Clearing resources under 'using' statement
8 System::Details::DisposeGuard<1> __dispose_guard_2({ outPsStream});
9 // ------------------------------------------
10
11 try
12 {
13 //Create save options with A4 size
14 System::SharedPtr<PsSaveOptions> options = System::MakeObject<PsSaveOptions>();
15 //Set page's background color to see white image on it's own transparent background
16 options->set_BackgroundColor(System::Drawing::Color::FromArgb(211, 8, 48));
17
18 // Create new 1-paged PS Document
19 System::SharedPtr<PsDocument> document = System::MakeObject<PsDocument>(outPsStream, options, false);
20
21
22 document->WriteGraphicsSave();
23 document->Translate(20.0f, 100.0f);
24
25 //Create bitmap from translucent image file
26 {
27 System::SharedPtr<System::Drawing::Bitmap> image = System::MakeObject<System::Drawing::Bitmap>(dataDir + u"mask1.png");
28 // Clearing resources under 'using' statement
29 System::Details::DisposeGuard<1> __dispose_guard_0({ image});
30 // ------------------------------------------
31
32 try
33 {
34 //Add this image to document as usual opaque RGB image
35 document->DrawImage(image, System::MakeObject<System::Drawing::Drawing2D::Matrix>(1.0f, 0.0f, 0.0f, 1.0f, 100.0f, 0.0f), System::Drawing::Color::Empty);
36 }
37 catch(...)
38 {
39 __dispose_guard_0.SetCurrentException(std::current_exception());
40 }
41 }
42
43 //Again create bitmap from the same image file
44 {
45 System::SharedPtr<System::Drawing::Bitmap> image = System::MakeObject<System::Drawing::Bitmap>(dataDir + u"mask1.png");
46 // Clearing resources under 'using' statement
47 System::Details::DisposeGuard<1> __dispose_guard_1({ image});
48 // ------------------------------------------
49
50 try
51 {
52 //Add this image to document as transparent image image
53 document->DrawTransparentImage(image, System::MakeObject<System::Drawing::Drawing2D::Matrix>(1.0f, 0.0f, 0.0f, 1.0f, 350.0f, 0.0f), 255);
54 }
55 catch(...)
56 {
57 __dispose_guard_1.SetCurrentException(std::current_exception());
58 }
59 }
60
61 document->WriteGraphicsRestore();
62
63 //Close current page
64 document->ClosePage();
65
66 //Save the document
67 document->Save();
68 }
69 catch(...)
70 {
71 __dispose_guard_2.SetCurrentException(std::current_exception());
72 }
73 }
The result of running this code is next
Adding transparent vector graphics object
Earlier we wrote that Aspose.Page for C++ library uses a paling algorithm for transparent shapes and text, which we called “pseudo-transparency”. In the example below we demonstrate a difference between two shapes painted with the same color, but in the first shape without the Alpha component and in the second case with the Alpha component.
1 // The path to the documents directory.
2 System::String dataDir = RunExamples::GetDataDir_WorkingWithTransparency();
3
4 //Create output stream for PostScript document
5 {
6 System::SharedPtr<System::IO::Stream> outPsStream = System::MakeObject<System::IO::FileStream>(dataDir + u"ShowPseudoTransparency_outPS.ps", System::IO::FileMode::Create);
7 // Clearing resources under 'using' statement
8 System::Details::DisposeGuard<1> __dispose_guard_0({ outPsStream});
9 // ------------------------------------------
10
11 try
12 {
13 //Create save options with A4 size
14 System::SharedPtr<PsSaveOptions> options = System::MakeObject<PsSaveOptions>();
15
16 // Create new 1-paged PS Document
17 System::SharedPtr<PsDocument> document = System::MakeObject<PsDocument>(outPsStream, options, false);
18
19 float offsetX = 50.0f;
20 float offsetY = 100.0f;
21 float width = 200.0f;
22 float height = 100.0f;
23
24 ///////////////////////////////// Create rectangle with opaque gradient fill /////////////////////////////////////////////////////////
25 System::SharedPtr<System::Drawing::Drawing2D::GraphicsPath> path = System::MakeObject<System::Drawing::Drawing2D::GraphicsPath>();
26 path->AddRectangle(System::Drawing::RectangleF(offsetX, offsetY, width, height));
27
28 System::SharedPtr<System::Drawing::Drawing2D::LinearGradientBrush> opaqueBrush = System::MakeObject<System::Drawing::Drawing2D::LinearGradientBrush>(System::Drawing::RectangleF(0.0f, 0.0f, 200.0f, 100.0f), System::Drawing::Color::FromArgb(0, 0, 0), System::Drawing::Color::FromArgb(40, 128, 70), 0.f);
29 System::SharedPtr<System::Drawing::Drawing2D::Matrix> brushTransform = System::MakeObject<System::Drawing::Drawing2D::Matrix>(width, 0.0f, 0.0f, height, offsetX, offsetY);
30 opaqueBrush->set_Transform(brushTransform);
31 System::SharedPtr<GradientBrush> gradientBrush = System::MakeObject<GradientBrush>(opaqueBrush);
32 gradientBrush->set_WrapMode(System::Drawing::Drawing2D::WrapMode::Clamp);
33
34 document->SetPaint(gradientBrush);
35 document->Fill(path);
36 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
37
38 offsetX = 350.0f;
39
40 ///////////////////////////////// Create rectangle with translucent gradient fill ///////////////////////////////////////////////////
41 //Create graphics path from the first rectangle
42 path = System::MakeObject<System::Drawing::Drawing2D::GraphicsPath>();
43 path->AddRectangle(System::Drawing::RectangleF(offsetX, offsetY, width, height));
44
45 //Create linear gradient brush colors which transparency are not 255, but 150 and 50. So it are translucent.
46 System::SharedPtr<System::Drawing::Drawing2D::LinearGradientBrush> translucentBrush = System::MakeObject<System::Drawing::Drawing2D::LinearGradientBrush>(System::Drawing::RectangleF(0.0f, 0.0f, width, height), System::Drawing::Color::FromArgb(150, 0, 0, 0), System::Drawing::Color::FromArgb(50, 40, 128, 70), 0.f);
47 //Create a transform for brush.
48 brushTransform = System::MakeObject<System::Drawing::Drawing2D::Matrix>(width, 0.0f, 0.0f, height, offsetX, offsetY);
49 //Set transform
50 translucentBrush->set_Transform(brushTransform);
51 //Create GradientBrush object containing the linear gradient brush
52 gradientBrush = System::MakeObject<GradientBrush>(translucentBrush);
53 gradientBrush->set_WrapMode(System::Drawing::Drawing2D::WrapMode::Clamp);
54 //Set paint
55 document->SetPaint(gradientBrush);
56 //Fill the rectangle
57 document->Fill(path);
58 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
59
60 //Close current page
61 document->ClosePage();
62
63 //Save the document
64 document->Save();
65 }
66 catch(...)
67 {
68 __dispose_guard_0.SetCurrentException(std::current_exception());
69 }
70 }
The result of running this code is appeared as
You can download examples and data files from GitHub.