Changeset 1979 for www


Ignore:
Timestamp:
Nov 16, 2007, 4:11:14 PM (13 years ago)
Author:
Sam Hocevar
Message:
  • Colour dithering: separate-space dithering.
Location:
www/study
Files:
3 added
2 edited

Legend:

Unmodified
Added
Removed
  • www/study/part5.html

    r1976 r1979  
    3333<h2> 5. Colour dithering </h2>
    3434
    35 <p> Coming soon. </p>
     35<p> Dithering colour images means dithering three-dimensional elements (RGB
     36pixels) instead of one-dimensional gray values. It is very complex and
     37depends on the output media even more than grayscale dithering. </p>
     38
     39<h3> Separate-space dithering </h3>
     40
     41<p> In some cases it is possible to perform three one-dimensional dithering
     42operations instead of one three-dimensional one. Consider for instance the
     43following palette:
     44</p>
     45
     46<p style="text-align: center;">
     47  <img src="pat5-1-1.png" width="512" height="64"
     48       class="inline" alt="8-colour RGB palette" />
     49</p>
     50
     51<p> It is made of the eight possible red/green/blue combinations made of 0.0
     52and 1.0 values. So one way of dithering an image using this palette is to
     53convert the image into three grayscale images (separating the red, green and
     54blue channels), dither each subimage to two colours separately and recombine
     55the images into three channels. </p>
     56
     57<p> Here are the results with Floyd-Steinberg dithering applied to each
     58channel. On the left, no colour correction, as The Gimp or Photoshop would
     59do; on the right, gamma-corrected dithering: </p>
     60
     61<p style="text-align: center;">
     62  <img src="out5-1-1.png" width="256" height="256"
     63       class="inline" alt="Floyd-Steinberg, 8 colours" />
     64  <img src="out5-1-2.png" width="256" height="256"
     65       class="inline" alt="Floyd-Steinberg, 8 colours, gamma-corrected" />
     66</p>
    3667
    3768<div style="float: left;">
  • www/study/study.py

    r1978 r1979  
    496496test42x(gradient256bw, ERROR_FSTEIN, threshold_3).writePng("grad4-2-2.png")
    497497
     498# Pattern 5.0.1: 8-colour palette
     499dest = Image((512, 64))
     500for x in range(512):
     501    d = x / 64
     502    r = (d & 2) >> 1
     503    g = (d & 4) >> 2
     504    b = d & 1
     505    for y in range(64):
     506        dest.setRgb(x, y, r, g, b)
     507dest.writePng("pat5-1-1.png")
     508
     509# Load the 256x256 colour Lenna image
     510lenna256 = Image("lenna256.png")
     511
     512# Output 5.0.1: RGB dithering
     513def test501(src, mat, func):
     514    (w, h) = src.size()
     515    dest = Image((w, h))
     516    tmp = [Image((w, h)), Image((w, h)), Image((w, h))]
     517    for y in range(h):
     518        for x in range(w):
     519            rgb = src.getRgb(x, y)
     520            for i in range(3):
     521                tmp[i].setGray(x, y, rgb[i])
     522    for i in range(3):
     523        tmp[i] = func(tmp[i], mat, threshold_2)
     524    for y in range(h):
     525        for x in range(w):
     526            (r, g, b) = [tmp[i].getGray(x, y) for i in range(3)]
     527            dest.setRgb(x, y, r, g, b)
     528    return dest
     529
     530test501(lenna256, ERROR_FSTEIN, test3xx).writePng("out5-1-1.png")
     531test501(lenna256, ERROR_FSTEIN, test42x).writePng("out5-1-2.png")
     532
    498533##############################################################################
    499534# Place temporary cruft below this
Note: See TracChangeset for help on using the changeset viewer.