scaled_bitmap.cpp 3.21 KB
Newer Older
1
2
3
4
/*****************************************************************************
 * scaled_bitmap.cpp
 *****************************************************************************
 * Copyright (C) 2003 VideoLAN
5
 * $Id$
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
 *
 * Authors: Cyril Deguet     <asmax@via.ecp.fr>
 *          Olivier Teulire <ipkiss@via.ecp.fr>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

#include "scaled_bitmap.hpp"


ScaledBitmap::ScaledBitmap( intf_thread_t *pIntf, const GenericBitmap &rBitmap,
                            int width, int height ):
    GenericBitmap( pIntf ), m_width( width ), m_height( height )
{
    // XXX We should check that width and height are positive...

    // Allocate memory for the buffer
    m_pData = new uint8_t[m_height * m_width * 4];

    int srcWidth = rBitmap.getWidth();
    int srcHeight = rBitmap.getHeight();
    uint32_t *pSrcData = (uint32_t*)rBitmap.getData();
    uint32_t *pDestData = (uint32_t*)m_pData;

    // Algorithm for horizontal enlargement
    if( width > srcWidth )
    {
45
46
47
48
        // Decision variables for Bresenham algorithm
        int incX1 = 2 * (srcWidth-1);
        int incX2 = incX1 - 2 * (width-1);
        int dX = incX1 - (width-1);
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73

        for( int y = 0; y < height; y++ )
        {
            uint32_t yOffset = ((y * srcHeight) / height) * srcWidth;
            pSrcData = ((uint32_t*)rBitmap.getData()) + yOffset;

            for( int x = 0; x < width; x++ )
            {
                *(pDestData++) = *pSrcData;

                if( dX <= 0 )
                {
                    dX += incX1;
                }
                else
                {
                    dX += incX2;
                    pSrcData++;
                }
            }
        }
    }
    // Algorithm for horizontal reduction
    else
    {
74
75
76
77
        // Decision variables for Bresenham algorithm
        int incX1 = 2 * (width-1);
        int incX2 = incX1 - 2 * (srcWidth-1);
        int dX = incX1 - (srcWidth-1);
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108

        for( int y = 0; y < height; y++ )
        {
            uint32_t yOffset = ((y * srcHeight) / height) * srcWidth;
            pSrcData = ((uint32_t*)rBitmap.getData()) + yOffset;

            for( int x = 0; x < width; x++ )
            {
                *(pDestData++) = *(pSrcData++);

                while( dX <= 0 )
                {
                    dX += incX1;
                    pSrcData++;
                }
                dX += incX2;
            }
        }

    }
}


ScaledBitmap::~ScaledBitmap()
{
    if( m_pData )
    {
        delete[] m_pData;
    }
}