Toshihiro Shimizu 890ddd
#include "plugin_tile_interface.h"
Toshihiro Shimizu 890ddd
#include "ttile.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int tile_interface_get_raw_address_unsafe(toonz_tile_handle_t handle, void **address)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (!handle || !address)
Toshihiro Shimizu 890ddd
		return -1; // inval
Toshihiro Shimizu 890ddd
	TTile *tile = reinterpret_cast<ttile *="">(handle);</ttile>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	tile->getRaster()->lock();
Toshihiro Shimizu 890ddd
	*address = tile->getRaster()->getRawData();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int tile_interface_safen(toonz_tile_handle_t handle)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (!handle) {
Toshihiro Shimizu 890ddd
		return -1; // inval
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TTile *tile = reinterpret_cast<ttile *="">(handle);</ttile>
Toshihiro Shimizu 890ddd
	tile->getRaster()->unlock();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int tile_interface_get_raw_stride(toonz_tile_handle_t handle, int *stride)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (!handle || !stride)
Toshihiro Shimizu 890ddd
		return -1; // inval
Toshihiro Shimizu 890ddd
	TTile *tile = reinterpret_cast<ttile *="">(handle);</ttile>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//	return stride in byte, not in pixel
Toshihiro Shimizu 890ddd
	*stride = tile->getRaster()->getWrap() * tile->getRaster()->getPixelSize();
Toshihiro Shimizu 890ddd
	return 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int tile_interface_get_element_type(toonz_tile_handle_t handle, int *element)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (!handle || !element)
Toshihiro Shimizu 890ddd
		return -1; // inval
Toshihiro Shimizu 890ddd
	TTile *tile = reinterpret_cast<ttile *="">(handle);</ttile>
Toshihiro Shimizu 890ddd
	TRasterP raster = tile->getRaster();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (false)
Toshihiro Shimizu 890ddd
		;
Toshihiro Shimizu 890ddd
	else if (TRaster32P(raster))
Toshihiro Shimizu 890ddd
		*element = TOONZ_TILE_TYPE_32P;
Toshihiro Shimizu 890ddd
	else if (TRaster64P(raster))
Toshihiro Shimizu 890ddd
		*element = TOONZ_TILE_TYPE_64P;
Toshihiro Shimizu 890ddd
	else if (TRasterGR8P(raster))
Toshihiro Shimizu 890ddd
		*element = TOONZ_TILE_TYPE_GR8P;
Toshihiro Shimizu 890ddd
	else if (TRasterGR16P(raster))
Toshihiro Shimizu 890ddd
		*element = TOONZ_TILE_TYPE_GR16P;
Toshihiro Shimizu 890ddd
	else if (TRasterGRDP(raster))
Toshihiro Shimizu 890ddd
		*element = TOONZ_TILE_TYPE_GRDP;
Toshihiro Shimizu 890ddd
	else if (TRasterYUV422P(raster))
Toshihiro Shimizu 890ddd
		*element = TOONZ_TILE_TYPE_YUV422P;
Toshihiro Shimizu 890ddd
	else {
Toshihiro Shimizu 890ddd
		*element = TOONZ_TILE_TYPE_NONE;
Toshihiro Shimizu 890ddd
		return -1;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int tile_interface_copy_rect(toonz_tile_handle_t handle, int left, int top,
Toshihiro Shimizu 890ddd
							 int width, int height, void *dst, int dststride)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (!handle || !dst || !dststride)
Toshihiro Shimizu 890ddd
		return -1; // inval
Toshihiro Shimizu 890ddd
	if (!width || !height)
Toshihiro Shimizu 890ddd
		return 0; // do nothing
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TTile *tile = reinterpret_cast<ttile *="">(handle);</ttile>
Toshihiro Shimizu 890ddd
	TRasterP raster = tile->getRaster();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (!(0 <= left && left + width <= raster->getLx() &&
Toshihiro Shimizu 890ddd
		  0 <= top && top + height <= raster->getLy())) {
Toshihiro Shimizu 890ddd
		return -1;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (int y = 0; y < height; y++) {
Toshihiro Shimizu 890ddd
		memcpy((UCHAR *)dst + y * dststride, raster->getRawData(left, top + y),
Toshihiro Shimizu 890ddd
			   width * raster->getPixelSize());
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int tile_interface_create_from(toonz_tile_handle_t handle, toonz_tile_handle_t *newhandle)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (!handle || !newhandle)
Toshihiro Shimizu 890ddd
		return -1; // inval
Toshihiro Shimizu 890ddd
	TTile *tile = reinterpret_cast<ttile *="">(handle);</ttile>
Toshihiro Shimizu 890ddd
	TTile *newtile = new TTile(tile->getRaster());
Toshihiro Shimizu 890ddd
	*newhandle = reinterpret_cast<toonz_tile_handle_t *="">(newtile);</toonz_tile_handle_t>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int tile_interface_create(toonz_tile_handle_t *newhandle)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (!newhandle)
Toshihiro Shimizu 890ddd
		return -1; // inval
Toshihiro Shimizu 890ddd
	*newhandle = reinterpret_cast<toonz_tile_handle_t *="">(new TTile());</toonz_tile_handle_t>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int tile_interface_destroy(toonz_tile_handle_t handle)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (!handle)
Toshihiro Shimizu 890ddd
		return -1; // inval
Toshihiro Shimizu 890ddd
	TTile *tile = reinterpret_cast<ttile *="">(handle);</ttile>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	delete tile;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int tile_interface_get_rectangle(toonz_tile_handle_t handle, toonz_rect_t *rect)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (!handle || !rect) {
Toshihiro Shimizu 890ddd
		return -1; // inval
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TTile *tile = reinterpret_cast<ttile *="">(handle);</ttile>
Toshihiro Shimizu 890ddd
	rect->x0 = tile->m_pos.x;
Toshihiro Shimizu 890ddd
	rect->y0 = tile->m_pos.y;
Toshihiro Shimizu 890ddd
	rect->x1 = tile->m_pos.x + tile->getRaster()->getLx();
Toshihiro Shimizu 890ddd
	rect->y1 = tile->m_pos.y + tile->getRaster()->getLy();
Toshihiro Shimizu 890ddd
	return 0;
Toshihiro Shimizu 890ddd
}