Blame c++/perspective/src/surface.cpp

531f67
531f67
#include "surface.h"
531f67
531f67
531f67
Surface::Surface():
531f67
	m_width(), m_height(), m_pitch(), m_origin() { }
531f67
531f67
Surface::Surface(const Surface &):
531f67
	m_width(), m_height(), m_pitch(), m_origin() { }
531f67
531f67
void
531f67
Surface::reset() {
531f67
	m_width = m_height = m_pitch = 0;
531f67
	m_origin = NULL;
531f67
}
531f67
531f67
void Surface::copy(int width, int height, const Color *src_origin, Color *dest_origin, int src_pitch, int dest_pitch) {
531f67
#ifndef NDEBUG
531f67
	{ // check source and destination ranges
531f67
		assert(dest_origin);
531f67
		assert(src_origin);
531f67
		
531f67
		Color *dest_begin = dest_origin + begin_offset(width, height, dest_pitch),
531f67
		      *dest_end   = dest_origin + end_offset(width, height, dest_pitch);
531f67
		const Color *src_begin = src_origin + begin_offset(width, height, src_pitch),
531f67
		            *src_end   = src_origin + end_offset(width, height, src_pitch);
531f67
		assert(src_end <= dest_begin || dest_end <= src_begin);
531f67
	}
531f67
#endif
531f67
	
531f67
	if (!src_pitch) src_pitch = width;
531f67
	if (!dest_pitch) dest_pitch = width;
531f67
531f67
	const Color *src_row = src_origin;
531f67
	for( Color *dest_row = dest_origin, *dest_row_end = dest_row + height*dest_pitch;
531f67
		 dest_row != dest_row_end;
531f67
		 dest_row += dest_pitch, src_row += src_pitch )
531f67
	{
531f67
		const Color *src_col = src_row;
531f67
		for( Color *dest_col = dest_row, *dest_col_end = dest_col + width;
531f67
			dest_col != dest_col_end;
531f67
			++dest_col, ++src_col )
531f67
		{ *dest_col = *src_col; }
531f67
	}
531f67
}
531f67
531f67
531f67
void DataSurface::clear() {
531f67
	if (Color *data = data_begin()) delete[] data;
531f67
	reset();
531f67
}
531f67
531f67
void DataSurface::init(int width, int height, int pitch, const Color *src_origin, int src_pitch) {
531f67
	Color *origin = NULL;
531f67
	if (width > 0 && height > 0) {
531f67
		if (!pitch) pitch = width;
531f67
		
531f67
		int count = data_count(width, height, pitch);
531f67
		int offset = begin_offset(width, height, pitch);
531f67
		Color *data = new Color[count];
531f67
		origin = data - offset;
531f67
		
531f67
		if (src_origin)
531f67
			copy(width, height, src_origin, origin, src_pitch, pitch);
531f67
	} else {
531f67
		width = 0;
531f67
		height = 0;
531f67
		pitch = 0;
531f67
	}
531f67
	
531f67
	clear();
531f67
	m_width = width;
531f67
	m_height = height;
531f67
	m_pitch = pitch;
531f67
	m_origin = origin;
531f67
}
531f67
531f67
531f67
void AliasSurface::init(Color *origin, int width, int height, int pitch) {
531f67
	clear();
531f67
	if (origin && width > 0 && height > 0) {
531f67
		m_width = width;
531f67
		m_height = height;
531f67
		m_pitch = pitch ? pitch : width;
531f67
		m_origin = origin;
531f67
	}
531f67
}
531f67