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

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