About this Blog

This Blog has English posts and Japanese posts. About Mac, iOS, Objective-C, and so on.

2012年12月8日土曜日

xcf.txtの日本語訳 9. COMPOSITING AND LAYER MODES

This post is a personal translation of Henning Makholm's xcf.txt to Japanese. : Index here
アプリを作る際に作成した、xcf.txtの日本語訳です。目次及び使用上の注意はこちら

9. COMPOSITING AND LAYER MODES
===============================

[この章を理解するにあたって、以下のURLを参考にしました。この場を借りて御礼申し上げます。
osakana.factory - ブレンドモード詳説
レイヤーモードについて: 晴れときどきGIMP ]

[xcf.txtに拠る計算式は以下のとおりですが、アプリ内では他の資料も参考に計算式を少し変更しています。そちらについても近々まとめて公開する予定です。]

This section describes the "flattening" process that GIMP applies
when a multi-layer image is displayed in the editor or exported to
other image file formats. It is present for reference only; an XCF
consumer may well elect to do something different with pixel data from
the layers than flattening them.

この章では、複数のレイヤーを持つ画像をGIMPがどのように表示するかまた、レイヤー非対応の画像フォーマットにエクスポートする時に、どのような処理をしているかを説明します。(レイヤーの結合)[? It is present for reference only]

Most XCF consumers will need to react to the layer mode property of
each layer; such a reaction must be informed by knowledge of how the
different layer modes affect the flattening process. In some
applications it might be acceptable for an XCF consumer to refuse
processing images with layer modes other than "Normal", but such an
application will probably not be considered fully XCF capable by its
users.

ほとんどのXCFファイル利用者は、各レイヤーのモードプロパティに対応する必要があるでしょう。その時には、レイヤーのモードがレイヤー結合にどのような違いをもたらすのかを しっかりと知っている必要があります。アプリケーションによっては、Normal以外のモードのレイヤーを拒否するという手段もアリかも知れませんが、そのようなソフトはXCFファイルを扱えるアプリ、とはみなされないでしょう。

In this section we consider primary color (or grayscale) intensities
and alpha values for pixels to be real numbers ranging from 0.0 to
1.0. This makes many of the formulas easier; the reader is asked to
keep in mind that a (linear) conversion from the integral 0..255 scale
of the actual XCF scale is implied whenever data from the XCF file is
mentioned.

この章では、数式表記の簡略化のために、各ピクセルのプライマリーなカラー強度(またはグレースケール)とアルファ値を0.0から1.0の実数であるとして考えます。、実際のXCFファイルにおいては0-255の整数によって表現されているということを頭の片隅に置いておいてください。

Any practical implementation of the computations specified below may
suffer rounding errors; this specification do not detail how these are
to be handled. GIMP itself rounds values to an integral number of
255ths at many points in the computation. This specification does not
specify exactly which these points are, and authors of XCF renderers
that aim to reproduce the effects of GIMP's flattening down to the
least significant bits are referred to studying its source code.

以下に説明する計算を実装する時は、必ず丸め誤差の問題がつきまといます。が、その問題にどう対処するかに関しては、このドキュメントでは扱いません。GIMPでは、計算のいたるところで丸めを行なっています。GIMPのレイヤー結合を再現したい開発者は、GIMPのソースコードをあたることとし、この仕様書では、詳細は述べません。[このへん適当]

In the description below, the variable letter "a" is used for alpha
values. The variable letter "r", "g", "b" are used for primary
intensities, "y" is used for grayscale intensities, and "i" is used
for colormap indexed. The letter "c" is used for the complete
color information for a pixel; depending on the color mode of the
image that is either an (r,g,b) triple, a y, or a c.

以下の説明では,"a"がアルファ値、r,g,bがRGBのそれぞれの値、yがグレースケールの強度、iがカラーマップのインデックス、cがピクセルについての全データ、としています。使用するのは、カラーモードによって、r,g,bであったり、a,yであったり、a,cであったりします。

The flattening process works independently for each pixel in the
canvas area. The description of some layer modes in the GIMP manual
may give the impression that they involve filters that let pixels
influence neighbor pixels, but that is not true.

レイヤー結合の処理は、canvas上の各ピクセルごとに独立で行うことができます。GIMPマニュアルにある幾つかのレイヤーモードでは、近傍のピクセルの情報が必要であるような印象を受けるかも知れませんが、それは正しくありません。

This description does not attempt to preserve the color information
for completely transparent pixels in a layer. If an application uses
this color information, it should document explicitly how it behaves
when transparent pixels from several different layers cover the same
point of the canvas.

ここの説明では、完全に透明なピクセルの色情報を保存しません。もしもアプリケーションがその情報を使用するなら、異なるレイヤーの透明なピクセルが重なった場合にどのような振る舞いをするのかをドキュメントに明示すべきです。

Flattening overview
-------------------

概観

This is how to compute the flattened result for a single pixel
position (in theory, that is - efficient implementations will of
course follow this procedure or an equivalent one for many pixels in
parallel):

各ピクセルにおいて、レイヤー結合された結果をどのように計算するかを示します。実際にはこれらの処理を並列で行うように実装するのが、普通でしょう。

1. Initialize a "working pixel" (a1,c1) to be completely transparent
   (that is, a1=0.0 and the value of c1 is immaterial).
   
2. Do the following for each visible layer in the image, starting with
   the one that comes LAST in the master layer list:

   3. Ignore the layer if it is the floating selection, or if it
      does not overlap the pixel position in question.

   4. Let (a2,c2) be the pixel data for the layer at the pixel
      position in question. If the layer does not have an alpha
      channel, then set a1 to 1.0.

   5. If the layer is the one that the floating selection is attached
      to and the floating selection overlaps the pixel position in
      question, then do the following:

      6. Let (a3,c3) be the pixel data for the floating selection
         layer at the pixel position in question.

      7. If there is a selection channel, then let x be its value
         at the pixel position in question, and set a3 to a3*x.

      8. Let m3 be the layer mode of the floating selection.
         
      9. Set (a2,c2) to COMPOSITE(a2,c2, a3,c3,m3).
         The COMPOSITE function is defined below.

   10. If the layer has a layer mask and it is enabled, then let x be
       the value of the layer mask at the pixel position in question,
       and set a2 to a2*x.

   11. Let m2 be the layer mode of the layer.

   12. If the layer is the bottommost visible layer (i.e., if it is
       the last visible layer in the master layer list) and m2 is not
       "Normal" or "Dissolve", then set m2 to "Normal".

   13. Set (a1,c1) to COMPOSITE(a1,c1, a2,c2,m2).
       The COMPOSITE function is defined below.

14. If the flattened image is to be shown against a background of
    color c0, then actually visible pixel is
    COMPOSITE(1.0,c0, a1,c1,Normal).

    Note that unless all layers have mode Normal, it would give the
    wrong result to start by initializing (a1,c1) to (1.0,c0).

  • 1. 完全に透明な、working pixel(a1,c1)を確保します。
  • 2. 以下の処理3-13を各レイヤー(表示されているもの)について行います。
    • 3. レイヤーがフローティング選択領域である場合や、処理中のピクセルをカバーしていない場合、無視します。
    • 4. (a2,c2)を処理中のピクセルに対応したレイヤー上のピクセルとします。もしもレイヤーがアルファチャンネルを持っていない場合は、a1=1.0とします。
    • 5.もしもレイヤーがフローティング選択領域を添付されていて、かつ、その領域が処理中のピクセルを含むなら、以下の6-9の処理を行います。
      • 6.(a3,c3)が処理中のピクセルに対応するフローティング選択領域のピクセルとします。
      • 7.セレクションチャネルがあるなら、処理中のピクセルに対応するピクセルのその値をxとし、a3=a3*xとします。
      • 8. m3はそのフローティング選択領域のモードとします。
      • 9. (a2,c2)をCOMPOSITE(a2,c2, a3,c3,m3)にセットします。COMPOSITEについては、後述。
    • 10. レイヤーがレイヤーマスクを持っていて有効になっている場合、xが相当するレイヤーマスクのピクセルの値として、 a2=a2*xとします。
    • 11. m2 は処理中のレイヤーのモードとします。
    • 12 もしもレイヤーが最下層の可視レイヤであり、(可視レイヤーのうちの最後のもの、と言い換えてもいい)かつm2がノーマルまたはDissolveではない場合、m2をNormalに設定します。
    • 13 (a1,c2)をCOMPOSITE(a1,c1, a2,c2,m2).にセットします。
  • 14 .もしも結合されたピクセルが背景の色c0上に表示されるなら、最終的に見えるピクセルはCOMPOSITE(1.0,c0, a1,c1,Normal).になります。

全てのレイヤーがNormalモードでない限り、(a1,c1) を (1.0,c0).と設定して計算し始めることは、間違った結果を与える可能性があります。

Helper functions
----------------
The following auxiliary functions are used in the definition of
COMPOSITE below:

以下の補助的な関数が、COMPOSITEを定義する際に使われています。

 MIN(x1,...,xn) is the least value of x1...xn

MIN: 引数の最小値

 MAX(x1,...,xn) is the largest value of x1..xn

MAX: 引数の最大値

 MID(x1,...,xn) = (MIN(x1,...,xn)+MAX(x1,...,xn))/2

MID: 引数の最大値と最小値の平均

[実際には、最大でも3つの値の比較しか使いません]

 CLAMP(x) = if x < 0 then 0.0 else if x > 1 then 1.0 else x

CLAMP: xを0.0-1.0の間に収める。x<0なら0.0、x>1なら1.0

 BLEND(a1,x1, a2,x2) = (1-k)*x1 + k*x2
                       where k = a2/(1-(1-a1)*(1-a2))

BLEND: [原文参照]

The layer modes
---------------

[レイヤーモードの日本語名については、Windows版のGIMP2.6を元にしています。]

This and the following sections define the COMPOSITE function used in
the general flattening algorithm. 

この節と次の説では、一般的な結合で使われるCOMPOSITE関数を説明しています。

"Normal" mode for RGB or grayscale images is the usual mode of
compositeing in computer graphics with alpha channels. In indexed
mode, the alpha value gets rounded to either 1.0 or 0.0 such that
no colors outside the colormap get produced:

RGBとグレースケールにおける「標準」モードは、コンピュータでの透明度を持った画像の処理におけるごくごく普通のモードです。インデックスカラーの「標準」モードでは、カラーマップに含まれない色が計算で現れないように、アルファ値は1.0か0.0に丸められます。

  COMPOSITE(a1,y1, a2,y2,Normal)
     = ( 1-(1-a1)*(1-a2), BLEND(a1,y1, a2,y2) )

[グレースケールでの計算式:式は原文を見てください]

  COMPOSITE(a1,r1,g1,b1, a2,r2,g2,b2,Normal)
     = ( 1-(1-a1)*(1-a2), BLEND(a1,r1, a2,r2),
                          BLEND(a1,g1, a2,g2),
                          BLEND(a1,b1, a2,b2) )

[RGBでの計算式:式は原文を見てください]

  COMPOSITE(a1,i1, a2,i2,Normal) = if a2 > 0.5 then (1.0,i2) else (a1,i1)

[インデックスカラーでの計算式:式は原文を見てください]

"Dissolve" mode corresponds to randomly dithering the alpha channel to
the set {0.0, 1.0}:

「ディザ合成」[直訳すれば「消散」]モードは、アルファ値をランダムに0.0と1.0にしてディザリングすることに相当します。

  COMPOSITE(a1,c1, a2,c2,Dissolve) = chose pseudo-randomly between
                                     (1.0,c2) with probability a2
                                     (a1,c1)  with probability 1-a2

[上側のlayerのアルファ値に応じた確率で、上のlayerの色と下のlayerの色が選択される。例:アルファ値0.6(a2=0.6)なら、6/10の確率で(1.0,c2)、4/10の確率で(a1,c1)]

These two modes are the only ones that make sense for all of the RGB,
grayscale and indexed color models. In the indexed color model, all
layer modes except Dissolve are treated as Normal.

RGB、グレースケール、インデックスカラーの3つのカラーモデルで共通して有効なモードは上の2つだけです。インデックスカラーの画像では、これら以外のlayerは全てNormalとして扱われます。

Most layer modes belong to the following group, which makes sense for
RGB and grayscale images, but not for indexed ones:

以下に示すモードのほとんどは、RGBとグレースケールで意味を持ちますが、インデックスカラーでは意味を持ちません。

  COMPOSITE(a1,y2, a2,y2,m)
     = ( a1, BLEND(a1,y1, MIN(a1,a2),f(y1,y2, m)) )
     
  COMPOSITE(a1,r1,g1,b1, a2,r2,g2,b2,m)
     = ( a1, BLEND(a1,r2, MIN(a1,a2),f(r1,r2, m)),
             BLEND(a1,g1, MIN(a1,a2),f(g1,g2, m)),
             BLEND(a1,b1, MIN(a1,a2),f(b1,g2, m)) )

[モード3~10、15~21で共通して使われるCOMPOSITE関数の形式]

[fの計算式が以下に示されています。]

when 3 <= m <= 10 or 15 <= m <= 21.
The  following table defines f(x1,x2,m):
  Multiply:      f(x1,x2,  3) = x1*x2
  Screen:        f(x1,x2,  4) = 1-(1-x1)*(1-x2)
  Overlay:       f(x1,x2,  5) = (1-x2)*x1^2 + x2*(1-(1-x2)^2)
  Difference:    f(x1,x2,  6) = if x1 > x2 then x1-x2 else x2-x1
  Addition:      f(x1,x2,  7) = CLAMP(x1+x2)
  Subtract:      f(x1,x2,  8) = CLAMP(x1-x2)
  Darken Only:   f(x1,x2,  9) = MIN(x1,x2)
  Lighten Only:  f(x1,x2, 10) = MAX(x1,x2)
  Divide:        f(x1,x2, 15) = CLAMP(x1/x2)
  Dodge:         f(x1,x2, 16) = CLAMP(x1/(1-x2))
  Burn           f(x1,x2, 17) = CLAMP(1-(1-x1)/x2)
  Hard Light:    f(x1,x2, 18) = if x2 < 0.5 then 2*x1*x2 else 1-2*(1-x1)(1-x2)
  Soft Light:    f(x1,x2, 19) = (1-x2)*x1^2 + x2*(1-(1-x2)^2)
  Grain Extract: f(x1,x2, 20) = CLAMP(x1-x2+0.5)
  Grain Merge:   f(x1,x2, 21) = CLAMP(x1+x2-0.5)

[それぞれのモードの、対応する日本語名を列挙します。計算式は原文を見てください。]
Multiply: 乗算
Screen: スクリーン
Overlay: オーバーレイ
Difference: 差の絶対値
Addition: 加算
Subtract: 減算
Darken Only: 比較(暗)
Lighten Only: 比較(明)
Divide: 除算
Dodge: 覆い焼き
Burn: 焼きこみ
Hard Light: ハードライト
Soft Light: ソフトライト

Note that the "Overlay" and "Soft Light" modes have identical effects.
In the "Divide", "Dodge", and "Burn" modes, division by zero should
be considered to produce a number so large that CLAMP(x/0) = 1 unless
x=0, in which case CLAMP(0/0) = 0.

メモ:「オーバーレイ」と「ソフトライト」は同じです。「除算」と「覆い焼き」、「焼きこみ」においてゼロ除算が発生した場合、CLAMP(x/0)=1とみなします。ただし、x=0のときは、CLAMP(0/0)=0と処理します。

The remaining four layer modes only make sense in the RGB color model;
if the color mode of the image is grayscale or indexed they will be
interpreted as Normal.

今までで述べなかったモードは、RGB形式でのみ有効です。グレースケールやインデックスカラーの画像でこれらのモードが現れた際は、「標準」モードとして扱われます。

  COMPOSITE(a1,r1,g1,b1, a2,r2,g2,b2,m)
     = ( a1, BLEND(a1,r2, MIN(a1,a2),r0),
             BLEND(a1,g1, MIN(a1,a2),g0),
             BLEND(a1,b1, MIN(a1,a2),b0) )
       where (r0,g0,b0) = h(r1,g1,b1, r2,g2,b2, m)

[モード11~14において共通して用いられるCOMPOSITEの形式です。]

[r0,g0,b0がどのように算出されるか、以下に記述してあります。]

when 11 <= m <= 14.
For defining these modes, we say that

これらのモードを定義する際、以下の様な用語を用います。

(r,g,b) has the _hue_ of (r',g',b')
  if r' = g' = b' and r >= g = b
  or there exist p and q such that p>=0 and r=p*r'+q and b=p*b'+q and g=p*g'+q
  
(r,g,b) has the _value_ of (r',g',b')
  if MAX(r,g,b) = MAX(r',g',b')
  
(r,g,b) has the _HSV-saturation_ of (r',g',b')
  if r' = g' = b' = 0 and r = g = b
  or MIN(r,g,b) = MAX(r,g,b)*MIN(r',g',b')/MAX(r',g',b')
  
(r,g,b) has the _luminosity_ of (r',g',b')
  if MID(r,g,b) = MID(r',g',b')
  
(r,g,b) has the _HSL-saturation_ of (r',g',b')
  if r' = g' = b' and r = g = b
  or MAX(r,g,b)-MIN(r,g,b) = MIN(MID(r,g,b),1-MID(r,g,b)) *
            (MAX(r',g',b')-MIN(r',g',b'))/MIN(MID(r',g',b'),1-MID(r',g',b'))
            

[訳注: (r',g',b')が与えられている色情報で、(r,g,b)が求めようとしている色情報です。(上で述べられている(r0,g0,b0)に対応)
以下のように、3つの条件が与えられ、全てを満たすように(r0,g0,b0)を算出します。]

Mode 11: Hue (H of HSV)

モード11: 「色相」

  h(r1,g1,b1, r2,g2,b2, 11) is
   if r2=g2=b2 then (r1,g1,b1) unchanged
   otherwise: the color that has
                the hue of (r1,g2,b2)
                the value of (r1,g1,b1)
                the HSV-saturation of (r1,g1,b1)
                
Mode 12: Saturation (S of HSV)

モード12: 「彩度」

  h(r1,g1,b1, r2,g2,b2, 12) is the color that has
    the hue of (r1,g1,b1)
    the value of (r1,g1,b1)
    the HSV-saturation of (r2,g2,b2)
    
Mode 13: Color (H and S of HSL)

モード13: 「色」

  h(r1,g1,b1, r2,g2,b2, 13) is the color that has
    the hue of (r2,g2,b2)
    the luminosity of (r1,g1,b1)
    the HSL-saturation of (r2,g2,b2)
    
Mode 14: Value (V of HSV)

モード14: 「明度」

  h(r1,g1,b1, r2,g2,b2, 14) is the color that has
    the hue of (r1,g1,b1)
    the value of (r2,g2,b2)
    the HSV-saturation of (r1,g1,b1)
    

[条件は訳すまでもないと思いますので原文を見てください。]

0 件のコメント:

コメントを投稿