About this Blog

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



This post is a personal translation of Henning Makholm's xcf.txt to Japanese. : Index here


osakana.factory - ブレンドモード詳説
レイヤーモードについて: 晴れときどきGIMP ]


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

ほとんどの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


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.


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.


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.


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


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


 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: 引数の最大値と最小値の平均


 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


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


"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:


  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) )


  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}:


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


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.


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


  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)) )



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.


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.


  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)



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)) *

[訳注: (r',g',b')が与えられている色情報で、(r,g,b)が求めようとしている色情報です。(上で述べられている(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 件のコメント: