source: trunk/src/gfx/image.cc @ 458

Last change on this file since 458 was 406, checked in by epyon, 10 years ago
  • code compiles cleanly on maximum warning level
File size: 3.2 KB
Line 
1// Copyright (C) 2011-2015 ChaosForge Ltd
2// http://chaosforge.org/
3//
4// This file is part of Nova libraries.
5// For conditions of distribution and use, see copying.txt file in root folder.
6
7#include "nv/gfx/image.hh"
8#include "nv/stl/algorithm.hh"
9
10using namespace nv;
11
12image::image( ivec2 size, nv::size_t depth )
13        : m_size( size ), m_depth( depth ), m_data( nullptr )
14{
15        m_data = new uint8[ static_cast<uint32>( m_size.x * m_size.y ) * m_depth ];
16}
17
18image::image( image_data* data )
19        : m_size( data->get_size() ), m_depth( data->get_depth() ), m_data( data->release_data() )
20{
21        NV_ASSERT( m_data, "image created from empty image_data!" );
22}
23
24
25image::image( ivec2 size, nv::size_t depth, const uint8 * data, bool reversed )
26        : m_size( size ), m_depth( depth ), m_data( nullptr )
27{
28        sint32 bsize = m_size.x * m_size.y * static_cast<sint32>( m_depth );
29        m_data = new uint8[ bsize ];
30
31        if ( reversed )
32        {
33                sint32 bline = m_size.x * static_cast<sint32>( m_depth );
34                for( int i = 0; i < m_size.y; ++i )
35                {
36                        raw_copy( data + i * bline, data + (i + 1) * bline, m_data + bsize - ( i + 1 ) * bline );
37                }
38
39        }
40        else
41        {
42                raw_copy( data, data + bsize, m_data );
43        }
44}
45
46void image::fill( uint8 value )
47{
48        raw_fill( m_data, m_data + m_size.x * m_size.y * static_cast<int>( m_depth ), value );
49}
50
51void image::fill( region r, uint8 value, int stride )
52{
53        if ( stride == 0 ) stride = r.size.x * static_cast<sint32>( m_depth );
54
55        sint32 bpos  = (r.pos.y*m_size.x + r.pos.x ) * static_cast<sint32>( m_depth );
56        sint32 bline = m_size.x*static_cast<sint32>( m_depth );
57
58        for( int i = 0; i < r.size.y; ++i )
59        {
60                // TODO: test
61                raw_fill( m_data + bpos + bline * i, m_data + bpos + bline * i + stride, value );
62        }
63}
64
65
66void image::set_region( region r, const uint8 * data, int stride )
67{
68        if ( stride == 0 ) stride = r.size.x * static_cast<sint32>( m_depth );
69       
70        sint32 bpos  = (r.pos.y*m_size.x + r.pos.x ) * static_cast<sint32>( m_depth );
71        sint32 bline = m_size.x*static_cast<sint32>( m_depth );
72
73        for( int i = 0; i < r.size.y; ++i )
74        {
75// TODO: test if same as old:
76//              memcpy( m_data+((r.pos.y+i)*m_size.x + r.pos.x ) * m_depth,
77//                      data + (i*stride), r.size.x * m_depth );
78                raw_copy( data + i*stride, data + (i+1)*stride, m_data + bpos + bline * i );
79        }
80}
81
82void image::set_region( region r, const image_data* idata )
83{
84        if ( idata->get_depth() == m_depth )
85        {
86                set_region( r, idata->get_data() );
87                return;
88        }
89
90        fill( r, 255 );
91
92        uint32 bpos       = static_cast< uint32 >( r.pos.y*m_size.x + r.pos.x ) * m_depth;
93        uint32 bline      = static_cast< uint32 >( m_size.x ) * m_depth;
94
95        uint32 rsizex     = static_cast< uint32 >( r.size.x );
96        uint32 rsizey     = static_cast< uint32 >( r.size.y );
97        const uint8* data = idata->get_data();
98        size_t depth      = idata->get_depth();
99        uint32 dstride    = rsizex * depth;
100
101        for( uint32 y = 0; y < rsizey; ++y )
102        {
103                uint32 pos = bpos + bline * y;
104                for( uint32 x = 0; x < rsizex; ++x )
105                {
106                        uint32 xy = pos + x * m_depth;
107                        for( size_t e = 0; e < depth; ++e )
108                        {
109                                m_data[ xy + e ] = data[ y*dstride + x * depth + e ];
110                        }
111                }
112        }
113}
114
115
116image::~image()
117{
118        delete[] m_data;
119}
120
121
Note: See TracBrowser for help on using the repository browser.