PostScript での透明部分の操作 | C++

PSドキュメントに透明度を追加する

PostScriptは、ベクターグラフィックオブジェクトの描画において透明度をサポートしていません。ただし、半透明(半透明)画像は、完全に透明なピクセルと完全に不透明なピクセルのセットとしてレンダリングできます。このような画像はマスクと呼ばれます。

Aspose.Page for C++ライブラリは、PSドキュメントに透明画像を追加するメソッドを提供しています。ベクターグラフィック(図形やテキスト)の描画には、**「擬似透明」**を使用できます。

**「擬似透明」**とは、アルファ値が255未満の色を淡くする処理です。これは、アルファ値が1の赤、緑、青の成分を特定の割合で混合することで実現されます。

「疑似透明」 とは、もちろん、上の透明レイヤーの下にある下の色のレイヤーが見えるわけではありませんが、下のレイヤーが白であれば透明であるような錯覚を生み出します。

PS ドキュメントに透明画像を追加する

前述のように、透明画像は マスク として PS ドキュメントに追加できます。Aspose.Page for C++ ライブラリには、この目的のために AddTransparentImage() メソッドが用意されています。 このメソッドは、画像が完全に不透明か、完全に透明か、あるいは半透明かを認識します。完全に不透明な場合は AddImage() メソッドで不透明画像として追加され、完全に透明な場合はドキュメントに追加されません。半透明画像の場合は、PostScript 画像 マスク として追加されます。

以下の例では、AddImage()AddTransparentImage() を使用して PS ドキュメントに透明画像を追加する場合の違いを示します。白い半透明画像を表示するには、ページの背景色を白以外に設定しています。

この例では、Aspose.Page for C++ ライブラリを使用して新しい PsDocument に任意の画像を追加するために、以下の手順を実行します。

  1. 生成される PS ファイルの出力ストリームを作成します。
  2. デフォルトのオプションを使用して PsSaveOptions オブジェクトを作成します。必要に応じて背景色を変更します。
  3. 既に作成済みの出力ストリームと保存オプションを使用して、1 ページの PsDocument を作成します。
  4. 新しいグラフィックス状態を作成します。
  5. 画像ファイルから ビットマップ を作成します。
  6. 画像に必要な変換を作成します。
  7. 画像が不透明であることが確実な場合は、完全に不透明な画像として PsDocument に追加します (AddImage() メソッドを使用)。不透明であることが確実でない場合は、透明な画像として追加します (AddTransparentImage() メソッドを使用)。
  8. 現在のグラフィック状態から上位レベル 1 に移行します。
  9. ページを閉じます。
  10. ドキュメントを保存します。
 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    }

.NET または Java の PS ドキュメントでの透明性の操作を参照してください。

このコードを実行した結果は次のようになります

Add Transparent Image

透明なベクターグラフィックオブジェクトの追加

Aspose.Page for C++ ライブラリは、透明な図形とテキストに「疑似透明」と呼ばれる淡色化アルゴリズムを使用することを先ほど説明しました。 以下の例では、同じ色で塗りつぶされた 2 つの図形の違いを示しています。ただし、最初の図形はアルファ要素なし、2 番目の図形はアルファ要素ありです。

 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    }

.NET または Java の PS ドキュメントでの透明性の操作を参照してください。

このコードを実行した結果は次のように表示されます。

Show Pseudo-Transparency

サンプルとデータ ファイルは GitHub からダウンロードできます。

Have any questions about Aspose.Page?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.