[395] | 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.
|
---|
[13] | 6 |
|
---|
[89] | 7 | #include "nv/gfx/image.hh"
|
---|
[383] | 8 | #include "nv/stl/algorithm.hh"
|
---|
[13] | 9 |
|
---|
| 10 | using namespace nv;
|
---|
| 11 |
|
---|
[398] | 12 | image::image( ivec2 size, nv::size_t depth )
|
---|
[13] | 13 | : m_size( size ), m_depth( depth ), m_data( nullptr )
|
---|
| 14 | {
|
---|
[128] | 15 | m_data = new uint8[ static_cast<uint32>( m_size.x * m_size.y ) * m_depth ];
|
---|
[13] | 16 | }
|
---|
| 17 |
|
---|
[90] | 18 | image::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 |
|
---|
[398] | 25 | image::image( ivec2 size, nv::size_t depth, const uint8 * data, bool reversed )
|
---|
[21] | 26 | : m_size( size ), m_depth( depth ), m_data( nullptr )
|
---|
| 27 | {
|
---|
[487] | 28 | NV_ASSERT( size.x >= 0 && size.y >= 0, "bad parameters passed to image!" );
|
---|
[121] | 29 | sint32 bsize = m_size.x * m_size.y * static_cast<sint32>( m_depth );
|
---|
[487] | 30 | m_data = new uint8[ size_t( bsize ) ];
|
---|
[21] | 31 |
|
---|
| 32 | if ( reversed )
|
---|
| 33 | {
|
---|
[121] | 34 | sint32 bline = m_size.x * static_cast<sint32>( m_depth );
|
---|
[90] | 35 | for( int i = 0; i < m_size.y; ++i )
|
---|
[21] | 36 | {
|
---|
[383] | 37 | raw_copy( data + i * bline, data + (i + 1) * bline, m_data + bsize - ( i + 1 ) * bline );
|
---|
[21] | 38 | }
|
---|
| 39 |
|
---|
| 40 | }
|
---|
| 41 | else
|
---|
| 42 | {
|
---|
[383] | 43 | raw_copy( data, data + bsize, m_data );
|
---|
[21] | 44 | }
|
---|
| 45 | }
|
---|
| 46 |
|
---|
[13] | 47 | void image::fill( uint8 value )
|
---|
| 48 | {
|
---|
[406] | 49 | raw_fill( m_data, m_data + m_size.x * m_size.y * static_cast<int>( m_depth ), value );
|
---|
[13] | 50 | }
|
---|
| 51 |
|
---|
[228] | 52 | void image::fill( region r, uint8 value, int stride )
|
---|
| 53 | {
|
---|
| 54 | if ( stride == 0 ) stride = r.size.x * static_cast<sint32>( m_depth );
|
---|
| 55 |
|
---|
| 56 | sint32 bpos = (r.pos.y*m_size.x + r.pos.x ) * static_cast<sint32>( m_depth );
|
---|
| 57 | sint32 bline = m_size.x*static_cast<sint32>( m_depth );
|
---|
| 58 |
|
---|
| 59 | for( int i = 0; i < r.size.y; ++i )
|
---|
| 60 | {
|
---|
| 61 | // TODO: test
|
---|
[383] | 62 | raw_fill( m_data + bpos + bline * i, m_data + bpos + bline * i + stride, value );
|
---|
[228] | 63 | }
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 |
|
---|
[121] | 67 | void image::set_region( region r, const uint8 * data, int stride )
|
---|
[13] | 68 | {
|
---|
[121] | 69 | if ( stride == 0 ) stride = r.size.x * static_cast<sint32>( m_depth );
|
---|
[90] | 70 |
|
---|
[121] | 71 | sint32 bpos = (r.pos.y*m_size.x + r.pos.x ) * static_cast<sint32>( m_depth );
|
---|
| 72 | sint32 bline = m_size.x*static_cast<sint32>( m_depth );
|
---|
[13] | 73 |
|
---|
[26] | 74 | for( int i = 0; i < r.size.y; ++i )
|
---|
| 75 | {
|
---|
[90] | 76 | // TODO: test if same as old:
|
---|
| 77 | // memcpy( m_data+((r.pos.y+i)*m_size.x + r.pos.x ) * m_depth,
|
---|
| 78 | // data + (i*stride), r.size.x * m_depth );
|
---|
[383] | 79 | raw_copy( data + i*stride, data + (i+1)*stride, m_data + bpos + bline * i );
|
---|
[26] | 80 | }
|
---|
[13] | 81 | }
|
---|
| 82 |
|
---|
[228] | 83 | void image::set_region( region r, const image_data* idata )
|
---|
| 84 | {
|
---|
| 85 | if ( idata->get_depth() == m_depth )
|
---|
| 86 | {
|
---|
| 87 | set_region( r, idata->get_data() );
|
---|
| 88 | return;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | fill( r, 255 );
|
---|
| 92 |
|
---|
[323] | 93 | uint32 bpos = static_cast< uint32 >( r.pos.y*m_size.x + r.pos.x ) * m_depth;
|
---|
| 94 | uint32 bline = static_cast< uint32 >( m_size.x ) * m_depth;
|
---|
[228] | 95 |
|
---|
[323] | 96 | uint32 rsizex = static_cast< uint32 >( r.size.x );
|
---|
| 97 | uint32 rsizey = static_cast< uint32 >( r.size.y );
|
---|
[228] | 98 | const uint8* data = idata->get_data();
|
---|
[323] | 99 | size_t depth = idata->get_depth();
|
---|
| 100 | uint32 dstride = rsizex * depth;
|
---|
[228] | 101 |
|
---|
[323] | 102 | for( uint32 y = 0; y < rsizey; ++y )
|
---|
[228] | 103 | {
|
---|
[323] | 104 | uint32 pos = bpos + bline * y;
|
---|
| 105 | for( uint32 x = 0; x < rsizex; ++x )
|
---|
[228] | 106 | {
|
---|
[323] | 107 | uint32 xy = pos + x * m_depth;
|
---|
| 108 | for( size_t e = 0; e < depth; ++e )
|
---|
[228] | 109 | {
|
---|
| 110 | m_data[ xy + e ] = data[ y*dstride + x * depth + e ];
|
---|
| 111 | }
|
---|
| 112 | }
|
---|
| 113 | }
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 |
|
---|
[13] | 117 | image::~image()
|
---|
| 118 | {
|
---|
| 119 | delete[] m_data;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
[21] | 122 |
|
---|