Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Image Compression Using Laplacian Pyramid Encoding


December 1997/Image Compression Using Laplacian Pyramid Encoding/Listing 3

Listing 3: The Expand function

void LP::Expand (unsigned level)
{
    if (level >= _levels || level + 1 >= _levels)
        throw "Level is out of range";

    unsigned char *src = _r_images[level + 1];
    unsigned char *dest = _l_images[level];

    unsigned width = _width >> level;
    unsigned height = _height >> level;

    for (int y = 0; y < (int) (height - 3); y += 2)
    {
        for (int x = 0; x < (int) (width - 3); x += 2)
        {
            // Expand argument is the top left corner
            // of the sample block

            Expand3x3 (src[((y / 2) * (width / 2)) + x / 2],
                       &dest[y * width + x], width);
        }
    }

    // Fix the edges

    // If the width is a multiple of two, then the reduced 
    // image has a column on the right that was not expanded.


    if (!(width & 1))
    {
        for (int y = 0; y < (int) (height - 3); y += 2)
        {
            Expand2x3 (src[((y/2) * (width/2)) + (width - 2) / 2],
                       &dest[y * width + (width - 2)], width);
        }
    }

    // If the height is a multiple of two, then the reduced 
    // image has a row on the bottom that was not expanded.

    if (!(height & 1))
    {
        for (int x = 0; x < (int) (width - 3); x += 2)
        {
            Expand3x2 (src[(((height - 2)/2) * (width/2)) + x/2],
                       &dest[(height - 2) * width + x], width);
        }
    }

    // If both the width and height are multiples of two, then
    // the bottom right corner was never expanded.

    if (!(width & 1) && !(height & 1))
    {
        Expand2x2 (src[(((height-2)/2) * (width/2)) + (width-2)/2],
                   &dest[(height-2) * width + (width-2)], width);
    }

    // When expanding, the outer edges need a little more
    // fixing because they have not been interpolated enough.

    // The top row only got interpolated with half the number 
    // of pixels needed.

    for (int x = 0; x < (int) (width); x++)
        dest[x] *= 2;

    // Same with the left column.  The top left corner only got 1/4
    // the number of pixels it needed, but we're multiplying it
    // by 2 twice, so it's OK.

    for (y = 0; y < (int) (height); y++)
        dest[y * width] *= 2;

    // If the width is an odd number, the number of pixels needed
    // to interpolate the right column is 1/2.  If it's an even
    // number, then it's OK.

    if (width & 1)
        for (int y = 1; y <= (int) (height); y++)
            dest[y * width - 1] *= 2;

    // If the height is an odd number, then the bottom row needs
    // twice as many pixels to interpolate correctly.

    if (height & 1)
        for (int x = 0; x < (int) (width); x++)
            dest[(height - 1) * width + x] *= 2;

    // Notice that the NE, SE, and SW corners are OK -- they
    // were multiplied by 2 or 4 automatically when the sides
    // were being fixed.
}
//End of File

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.