Changeset 1942 for www


Ignore:
Timestamp:
Nov 12, 2007, 11:01:52 AM (13 years ago)
Author:
Sam Hocevar
Message:
  • Added a figure to explain the 2×2 Bayer matrix results.
  • Started grayscale dithering.
Location:
www/study
Files:
7 added
2 edited
2 moved

Legend:

Unmodified
Added
Removed
  • www/study/fig4-1-1.svg

    r1941 r1942  
    1515   sodipodi:version="0.32"
    1616   inkscape:version="0.45.1"
    17    sodipodi:docname="fig00z.svg"
     17   sodipodi:docname="fig4-1-1.svg"
    1818   inkscape:output_extension="org.inkscape.output.svg.inkscape"
    1919   sodipodi:docbase="/home/sam/debian/pkg-misc/unstable/libcaca/www/study"
    2020   version="1.0"
    21    inkscape:export-filename="/home/sam/debian/pkg-misc/unstable/libcaca/www/study/fig00z.png"
     21   inkscape:export-filename="/home/sam/debian/pkg-misc/unstable/libcaca/www/study/fig4-1-1.png"
    2222   inkscape:export-xdpi="38.16"
    2323   inkscape:export-ydpi="38.16">
  • www/study/index.html

    r1940 r1942  
    201201thresholded with a value of 0.2. Pixels (0,1), (0, 3), (0, 4) etc. will be
    202202thresholded with a value of 0.8, and so on, resulting in the image seen
    203 in 2.1. </p>
     203in 2.1. Here is a sample of what happens to groups of 4 even-coloured pixels
     204of various gray values: </p>
     205
     206<p style="text-align: center;">
     207  <img src="fig2-3-5.png" width="453" height="173"
     208       alt="results of 2x2 dither matrix" />
     209</p>
    204210
    205211<p> Different matrices can give very different results. This is a 4×4
     
    427433-->
    428434
    429 <!--
    430 <h3> 3. Introducing gamma </h3>
    431 
    432 <p> If you are reading this document on a computer
    433 screen, you may have noticed that the above 50% pattern was closer to a 0.73
    434 grayscale (left) than to the intuitively expected 0.5 value (right). If you
    435 are reading a printed copy, it might be a different matter. </p>
    436 
    437 <p style="text-align: center;">
    438   <img src="pat004.png" width="240" height="80"
     435<h2> 4. Grayscale dithering </h2>
     436
     437<p> Generalising dithering to more than two colours is straightforward in the
     438grayscale palette. Here are the results with 4×4 Bayer ordered dithering and
     439with Floyd-Steinberg error diffusion: </p>
     440
     441<p style="text-align: center;">
     442  <img src="out4-0-1.png" width="256" height="256"
     443       class="inline" alt="4×4 Bayer ordered dithering, 3 colours" />
     444  <img src="grad4-0-1.png" width="32" height="256"
     445       class="inline" alt="4×4 Bayer ordered dithering gradient, 3 colours" />
     446  <img src="out4-0-2.png" width="256" height="256"
     447       class="inline" alt="Floyd-Steinberg error diffusion, 3 colours" />
     448  <img src="grad4-0-2.png" width="32" height="256"
     449       class="inline" alt="Floyd-Steinberg error diffusion gradient, 3 colours" />
     450</p>
     451
     452<p> Unfortunately the result is not as good as expected. The white pattern
     453on Lenna’s cheeks is visually disturbing, and the whole image looks darker
     454than with pure black-and-white dithering. But then, the previous dithering
     455results looked a lot brighter than the original image. This is due to the
     456output media’s <b>gamma</b>. </p>
     457
     458<h3> 4.1. Introducing gamma </h3>
     459
     460<p> If you are reading this document on a computer screen, you may have
     461noticed that the black and white 50% pattern was closer to a 0.73 grayscale
     462(left) than to the intuitively expected 0.5 value (right). If you are reading
     463a printed copy, it might be a different matter. </p>
     464
     465<p style="text-align: center;">
     466  <img src="pat4-1-1.png" width="240" height="80"
    439467       class="inline" alt="introducing gamma" />
    440468</p>
     
    449477
    450478<p style="text-align: center;">
    451   <img src="fig001.png" width="300" height="240" alt="introducing gamma" />
     479  <img src="fig4-1-1.png" width="300" height="240" alt="introducing gamma" />
    452480</p>
    453481
     
    460488emulated with a 50% dither pattern. </p>
    461489
     490<!--
    462491<p> So, instead of using 25%, 50% and 75% patterns (which give non-uniform
    463492gray values of 0.53, 0.73 and 0.88), one should rather use 6.25%, 25% and 50%
  • www/study/study.py

    r1941 r1942  
    335335test3xx(gradient256bw, mat, False, "grad3-2-9.png")
    336336
    337 ##############################################################################
    338 # Only temporary cruft below this
    339 import sys
    340 sys.exit(0)
    341 
    342 
    343 
    344 
    345 # Pattern 4: gamma-corrected 50% gray, black-white halftone, 50% gray
     337# Output 4.0.1: 4x4 Bayer dithering, 3 colours
     338def test401(src, mat, name):
     339    (w, h) = src.size()
     340    dest = Image((w, h))
     341    dx = len(mat[0])
     342    dy = len(mat)
     343    for y in range(h):
     344        for x in range(w):
     345            c = src.getGray(x, y)
     346            threshold = (1. + mat[y % dy][x % dx]) / (dx * dy + 1)
     347            if c < 0.5:
     348                c = 0.5 * (c > threshold / 2)
     349            else:
     350                c = 0.5 + 0.5 * (c > 0.5 + threshold / 2)
     351            dest.setGray(x, y, c)
     352    dest.writePng(name)
     353
     354mat = [[  0,  8,  3, 11],
     355       [ 15,  4, 12,  7],
     356       [  2, 10,  1,  9],
     357       [ 13,  6, 14,  5]]
     358test401(lenna256bw, mat, "out4-0-1.png")
     359test401(gradient256bw, mat, "grad4-0-1.png")
     360
     361# Output 4.0.2: standard Floyd-Steinberg, 3 colours
     362def test402(src, mat, serpentine, name):
     363    (w, h) = src.size()
     364    dest = Image((w, h))
     365    lines = len(mat)
     366    rows = len(mat[0])
     367    offset = mat[0].index(-1)
     368    ey = [[0.] * (w + rows - 1) for x in range(lines)]
     369    for y in range(h):
     370        ex = [0.] * (rows - offset)
     371        if serpentine and y & 1:
     372            xrange = range(w - 1, -1, -1)
     373        else:
     374            xrange = range(w)
     375        for x in xrange:
     376            # Set pixel
     377            c = src.getGray(x, y) + ex[0] + ey[0][x + offset]
     378            d = 0.5 * (c > 0.25) + 0.5 * (c > 0.75)
     379            dest.setGray(x, y, d)
     380            error = c - d
     381            # Propagate first line of error
     382            for dx in range(rows - offset - 2):
     383                ex[dx] = ex[dx + 1] + error * mat[0][offset + 1 + dx]
     384            ex[rows - offset - 2] = error * mat[0][rows - 1]
     385            # Propagate next lines
     386            if serpentine and y & 1:
     387                for dy in range(1, lines):
     388                    for dx in range(rows):
     389                        ey[dy][x + dx] += error * mat[dy][rows - 1 - dx]
     390            else:
     391                for dy in range(1, lines):
     392                    for dx in range(rows):
     393                        ey[dy][x + dx] += error * mat[dy][dx]
     394        for dy in range(lines - 1):
     395            ey[dy] = ey[dy + 1]
     396        ey[lines - 1] = [0.] * (w + rows - 1)
     397    dest.writePng(name)
     398
     399mat = [[    0.,    -1, 7./16],
     400       [ 3./16, 5./16, 1./16]]
     401test402(lenna256bw, mat, False, "out4-0-2.png")
     402test402(gradient256bw, mat, False, "grad4-0-2.png")
     403
     404# Pattern 4.1.1: gamma-corrected 50% gray, black-white halftone, 50% gray
    346405dest = Image((240, 80))
    347406for y in range(80):
     
    353412    for x in range(160, 240):
    354413        dest.setGray(x, y, 0.5)
    355 dest.writePng("pat004.png")
     414dest.writePng("pat4-1-1.png")
     415
     416##############################################################################
     417# Only temporary cruft below this
     418import sys
     419sys.exit(0)
     420
     421
     422
    356423
    357424# Pattern 5: gamma-corrected 50% gray, black-white halftone, 50% gray
Note: See TracChangeset for help on using the changeset viewer.