#include "mask.h" #define LOG( X ) m_pLog->Log( "ShadowMask", this, X ) #define LOG_ERROR( X ) m_pLog->Log( "ShadowMask", this, _LOG_ERROR, X ) ShadowMask::ShadowMask(){ m_pLog = basicLogCollection::getInstance(); m_bActive = false; #if defined( __linux__ ) && !defined( __GUI__ ) m_pVertexFileName = (CSTR)malloc(256); m_pRasterFileName = (CSTR)malloc(256); sprintf( m_pVertexFileName, "masking.dat" ); sprintf( m_pRasterFileName, "shadow.dat" ); LOG(m_pVertexFileName); loadVertexes(m_pVertexFileName); LOG("done"); LOG(m_pRasterFileName); loadRastered(m_pRasterFileName); LOG("done"); #endif }; ShadowMask::~ShadowMask(){ if( m_vVectorMask.size() ){ #if defined( __linux__ ) && !defined( __GUI__ ) saveVertexes(m_pVertexFileName); #else if( !m_bRasterized ){ if( rasterizeVertexes() ){ // Save rasterized // in Linux Section }; }; #endif for( vectorMasking::iterator index = m_vVectorMask.begin(); index < m_vVectorMask.end(); index++ ){ free( (*index)->points ); free( (*index) ); }; m_vVectorMask.clear(); }; #if defined( __linux__ ) && !defined( __GUI__ ) if( m_pVertexFileName ) free( m_pVertexFileName ); #endif if(m_vZoneMask.size()){ #if defined( __linux__ ) && !defined( __GUI__ ) saveRastered( m_pRasterFileName ); #endif // Clear Data for( vectorZoneMask::iterator index = m_vZoneMask.begin(); index < m_vZoneMask.end(); index++ ){ free( (*index)->zones ); free( (*index) ); }; m_vZoneMask.clear(); }; #if defined( __linux__ ) && !defined( __GUI__ ) if( m_pRasterFileName ) free( m_pRasterFileName ); #endif }; #if defined( __linux__ ) && !defined( __GUI__ ) bool ShadowMask::loadRastered( CSTR fName ){ FILE *fd; if (fName != NULL){ fd = fopen(fName, "rb"); } else { fd = fopen(m_pRasterFileName, "rb"); }; if( fd != NULL ){ char tBuffer[256]; bool InRay; rawRay *tRay; int tColumnID, tCount, tStart, tStop, tIndex; m_bActive = false; if( m_vZoneMask.size() ){ // Clear Data LOG("Clear Data"); for( vectorZoneMask::iterator index = m_vZoneMask.begin(); index < m_vZoneMask.end(); index++ ){ free( (*index)->zones ); free( (*index) ); }; m_vZoneMask.clear(); LOG("Done"); }; fscanf( fd, "%s\n", tBuffer ); InRay = false; tIndex = 0; while( !feof( fd ) ){ if( InRay ){ fscanf( fd, "<%d:%d>\n", &tStart, &tStop ); tRay->zones[tIndex].start = tStart; tRay->zones[tIndex].stop = tStop; tIndex++; if( tIndex == tCount ){ m_vZoneMask.push_back( tRay ); InRay = false; }; } else { fscanf( fd, "[%d]:[%d]\n", &tColumnID, &tCount ); if( tCount > 0 ){ tRay = (rawRay *) malloc( sizeof( rawRay ) ); if( tRay != NULL ){ tRay->zones = (raySegment *) malloc( sizeof(raySegment) * tCount ); if( tRay->zones != NULL ){ tRay->column = tColumnID; tRay->count = tCount; tIndex = 0; InRay = true; } else { free(tRay); // Handle Memory Alloc Error }; } else { // Handle Memory Alloc Error }; }; }; }; fclose( fd ); m_bActive = strncmp( tBuffer, "ENA", 3 )==0; m_bRasterized = true; LOG("Loading Done"); return true; } else { LOG_ERROR("Can not open Data File!"); }; return false; }; bool ShadowMask::saveRastered( CSTR fName ){ FILE *fd; if (fName == NULL){ fd = fopen( m_pRasterFileName, "wb+"); } else { fd = fopen(fName, "wb+"); }; if( fd != NULL ){ if( m_bActive ) fprintf( fd, "ENABLED\n" ); else fprintf( fd, "DISABLED\n" ); for( vectorZoneMask::iterator index = m_vZoneMask.begin(); index < m_vZoneMask.end(); index++ ){ fprintf( fd, "[%d]:[%d]\n", (*index)->column , (*index)->count ); for( int j = 0; j < (*index)->count; j++ ){ fprintf( fd, "<%d:%d>\n", (*index)->zones[j].start, (*index)->zones[j].stop ); }; }; fclose( fd ); m_bRasterized = true; return true; }; return false; }; #endif bool ShadowMask::isOutside( uint16_t x, uint16_t y ){ if( !m_bActive ) return true; for( vectorZoneMask::iterator index = m_vZoneMask.begin(); index < m_vZoneMask.end(); index++ ){ if( (*index)->column == x ){ for( int j = 0; j < (*index)->count; j++ ){ if( ( y >= (*index)->zones[j].start ) && ( y <= (*index)->zones[j].stop ) ) return false; }; break; }; }; return true; }; bool ShadowMask::isOutside( simplePoint A ){ if( !m_bActive ) return true; return isOutside( A.x(), A.y() ); }; void ShadowMask::setActive( bool tVal ){ m_bActive = tVal; }; bool ShadowMask::addRay( uint16_t column, raySegment RS ){ for( vectorZoneMask::iterator index = m_vZoneMask.begin(); index < m_vZoneMask.end(); index++ ){ if( (*index)->column == column ){ (*index)->zones = (raySegment *) realloc( (*index)->zones, sizeof(raySegment) * ((*index)->count+1) ); (*index)->zones[(*index)->count].start = RS.start; (*index)->zones[(*index)->count].stop = RS.stop; (*index)->count++; return true; }; }; // not found column rawRay *tRay; tRay = (rawRay *) malloc( sizeof( rawRay ) ); if( tRay != NULL ){ tRay->zones = (raySegment *) malloc( sizeof(raySegment) ); if( tRay->zones != NULL ){ tRay->column = column; tRay->count = 1; tRay->zones[0].start = RS.start; tRay->zones[0].stop = RS.stop; m_vZoneMask.push_back( tRay ); return true; } else { free(tRay); // Handle Memory Alloc Error }; }; return false; }; bool ShadowMask::addRay( uint16_t column, uint32_t RS ){// Binary Data to RaySegment raySegment segR; segR.start = ( RS >> 16 ) & 0xFFFF; segR.stop = RS & 0xFFFF; return addRay( column, segR ); }; bool ShadowMask::addPoly( simplePolygone *SP ){ m_vVectorMask.push_back( SP ); return true; }; simplePolygone *ShadowMask::getPoly(int id){ if (id >= m_vVectorMask.size()) return NULL; return m_vVectorMask.at(id); }; bool ShadowMask::clear( void ){ simplePolygone *tPoly; rawRay *tRay; for( vectorMasking::iterator index = m_vVectorMask.begin(); index < m_vVectorMask.end(); index++ ){ tPoly = *index; delete( tPoly->points ); delete( tPoly ); }; m_vVectorMask.clear(); #ifdef __linux__ unlink( m_pVertexFileName ); #endif for( vectorZoneMask::iterator index = m_vZoneMask.begin(); index < m_vZoneMask.end(); index++ ){ tRay = *index; delete( tRay->zones ); delete( tRay ); }; m_vZoneMask.clear(); #ifdef __linux__ unlink( m_pRasterFileName ); #endif m_bActive = false; return true; }; bool ShadowMask::deletePoly( int ID ){ if( m_vVectorMask.size() < ID ) return false; delete( m_vVectorMask.at(ID)->points ); delete( m_vVectorMask.at(ID) ); m_vVectorMask.erase( m_vVectorMask.begin()+ID ); #if defined( WIN32 ) || defined( __GUI__ ) m_vVectorMask.shrink_to_fit(); #endif return true; }; #if defined( __linux__ ) && !defined( __GUI__ ) bool ShadowMask::saveVertexes( CSTR fName ){ if( m_vVectorMask.size() == 0 ) return true; FILE *fd; if (fName == NULL){ fd = fopen( m_pVertexFileName, "wb+"); } else { fd = fopen(fName, "wb+"); }; if( fd == NULL ) return false; for( vectorMasking::iterator index = m_vVectorMask.begin(); index < m_vVectorMask.end(); index++ ){ fprintf( fd, "%d\n", (*index)->count ); fprintf( fd, "%d %d %d\n", (*index)->color[0], (*index)->color[1], (*index)->color[2] ); for( int j = 0; j < (*index)->count; j++){ int cX, cY; cX = (*index)->points[j].x(); cY = (*index)->points[j].y(); fprintf( fd, "%d %d\n", cX, cY ); }; }; fclose( fd ); return true; }; bool ShadowMask::loadVertexes( CSTR fName ){ if( m_vVectorMask.size() ){ for( vectorMasking::iterator index = m_vVectorMask.begin(); index < m_vVectorMask.end(); index++ ){ free( (*index)->points ); free( (*index) ); }; m_vVectorMask.clear(); }; FILE *fd; if (fName == NULL){ fd = fopen( m_pVertexFileName, "r"); } else { fd = fopen(fName, "r"); }; if( fd == NULL ) return false; int count = 0; while( !feof( fd ) ){ fscanf( fd, "%d\n", &count ); simplePolygone *tSP; tSP = (simplePolygone *) malloc( sizeof(simplePolygone)); if( tSP != NULL ){ #if defined( WIN32 ) || defined( __GUI__ ) tSP->points = (QPoint *) malloc( sizeof( QPoint ) * count ); #else tSP->points = (simplePoint *) malloc( sizeof( simplePoint ) * count ); #endif if( tSP->points != NULL ){ tSP->count = count; int r,g,b; fscanf( fd, "%d %d %d\n", &r, &g, &b ); tSP->color[0] = r; tSP->color[1] = g; tSP->color[2] = b; for( int index = 0; index < count; index++ ){ int tX, tY; fscanf( fd, "%d %d\n", &tX, &tY ); tSP->points[index].setX( tX ); tSP->points[index].setY( tY ); }; m_vVectorMask.push_back( tSP ); }; }; }; fclose( fd ); m_bRasterized = false; return true; }; #endif bool ShadowMask::getMPoly( vectorMasking &VM ){ VM.clear(); if( m_vVectorMask.size() == 0 ) return false; for( int index = 0; index < m_vVectorMask.size(); index++ ) { VM.push_back( m_vVectorMask.at( index ) ); }; return true; }; bool ShadowMask::getZoneMask(vectorZoneMask &ZM){ ZM.clear(); if (m_vZoneMask.size() == 0) return false; for (int index = 0; index < m_vZoneMask.size(); index++){ ZM.push_back(m_vZoneMask.at(index)); }; return true; }; #if defined( WIN32 ) || defined( __GUI__ ) bool ShadowMask::addVertex( uint16_t polyID, uint32_t SP ){// Binary Data to Vertex QPoint simpleP; simpleP.setX( ( SP >> 16 ) & 0xFFFF ); simpleP.setY( SP & 0xFFFF ); return addVertex( polyID, simpleP ); }; bool ShadowMask::addVertex( uint16_t polyID, QPoint SP ){ if( polyID >= m_vVectorMask.size() ){ // New Polygone simplePolygone *tPoly; tPoly = (simplePolygone *) malloc( sizeof( simplePolygone ) ); if( tPoly != NULL ){ tPoly->points = (QPoint *) malloc( sizeof(QPoint)); if( tPoly->points!= NULL ){ tPoly->count = 1; tPoly->points[0].setX(SP.x()); tPoly->points[0].setY(SP.y()); m_vVectorMask.push_back(tPoly); } else { free( tPoly ); return false; }; } else { return false; }; } else { m_vVectorMask.at( polyID )->points = (QPoint *) realloc( m_vVectorMask.at( polyID )->points, sizeof(QPoint) * ( m_vVectorMask.at( polyID )->count+1 )); if( m_vVectorMask.at( polyID )->points!= NULL ){ m_vVectorMask.at( polyID )->points[m_vVectorMask.at( polyID )->count].setX( SP.x() ); m_vVectorMask.at( polyID )->points[m_vVectorMask.at( polyID )->count].setY( SP.y() ); m_vVectorMask.at( polyID )->count++; } else { return false; }; }; m_bRasterized = false; return true; }; bool ShadowMask::rasterizeVertexes( void ){ vectorQPoly tVectorPoly; if( getQPoly( tVectorPoly ) ){ if( tVectorPoly.size() == 0 ) return false; //FILE *fd; //fd = fopen( m_pRasterFileName, "w" ); //if( fd == NULL ) return false; if( m_vZoneMask.size() ){ // Clear Data for (vectorZoneMask::iterator index = m_vZoneMask.begin(); index < m_vZoneMask.end(); index++){ free((*index)->zones); free((*index)); }; m_vZoneMask.clear(); }; // Check each TH Matrix on Polygones uint16_t cColumn = 0; uint16_t cStart = 0; uint16_t cStop = 0; raySegment RS; bool oldZone = false; bool inZone = false; // Check each point for polygons for( int x = 0; x < M_SCAN_POINTS; x++){ cColumn = x; oldZone = false; inZone = false; cStart = 0; cStop = 0; for( int y = 0; y < Q_SCAN_COUNT; y++ ){ QPoint tPoint; tPoint.setX( x ); tPoint.setY( y ); inZone = false; for( int i = 0; i < tVectorPoly.size(); i++ ){ if( !inZone ){ if( tVectorPoly.at(i)->containsPoint( tPoint, Qt::OddEvenFill) ){ if( cStart == 0 ) cStart = y; inZone = true; break; // No need to check Other Polygones }; }; }; if( inZone != oldZone ){ if( !inZone ){ cStop = y; RS.start = cStart; RS.stop = cStop; addRay(cColumn, RS); //fprintf( fd, "%d %d %d\n", cColumn, cStart, cStop ); cStart = 0; }; oldZone = inZone; }; }; if( inZone ){ cStop = Q_SCAN_COUNT; inZone = false; oldZone = false; RS.start = cStart; RS.stop = cStop; addRay(cColumn, RS); //fprintf( fd, "%d %d %d\n", cColumn, cStart, cStop ); }; }; //fclose( fd ); m_bRasterized = true; }; return true; }; bool ShadowMask::getQPoly( vectorQPoly &VP ){ QPolygon *tCurrentPoly; QPoint *tPoint; VP.clear(); if( m_vVectorMask.size() == 0 ) return false; for( int index = 0; index < m_vVectorMask.size(); index++ ) { tCurrentPoly = new QPolygon(); if( tCurrentPoly!= NULL ){ tCurrentPoly->clear(); for( int j = 0; j < m_vVectorMask.at( index )->count; j++ ){ tPoint = new QPoint( m_vVectorMask.at( index )->points[j].x(), m_vVectorMask.at( index )->points[j].y() ); if( tPoint!= NULL ) tCurrentPoly->push_back( *tPoint ); tPoint = NULL; }; VP.push_back( tCurrentPoly ); } else return false; }; return true; }; #endif #undef LOG #undef LOG_ERROR