Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef WIN32
Toshihiro Shimizu 890ddd
#include "Windows.h"
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#include <vector></vector>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <stdio.h></stdio.h>
Toshihiro Shimizu 890ddd
#include <stdlib.h></stdlib.h>
Toshihiro Shimizu 890ddd
#include <time.h></time.h>
Toshihiro Shimizu 890ddd
#include <sys types.h=""></sys>
Toshihiro Shimizu 890ddd
#include <sys timeb.h=""></sys>
Toshihiro Shimizu 890ddd
#include <string.h></string.h>
Toshihiro Shimizu 890ddd
#include <math.h></math.h>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "Params.h"
Toshihiro Shimizu 890ddd
#include "PatternMapParam.h"
Toshihiro Shimizu 890ddd
#include "STColSelPic.h"
Toshihiro Shimizu 890ddd
#include "BlurMatrix.h"
Toshihiro Shimizu 890ddd
#include "SDirection.h"
Toshihiro Shimizu 890ddd
#include "SDef.h"
Toshihiro Shimizu 890ddd
#include "CallCircle.h"
Toshihiro Shimizu 890ddd
#include "CallParam.h"
Toshihiro Shimizu 890ddd
#include "EraseContour.h"
Toshihiro Shimizu 890ddd
#include "PatternPosition.h"
Toshihiro Shimizu 890ddd
#include "Pattern.h"
Toshihiro Shimizu 890ddd
#include "SError.h"
Toshihiro Shimizu 890ddd
//#include "tmsg.h"
Toshihiro Shimizu 890ddd
#include "patternmap.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
using namespace std;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef __cplusplus
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
extern "C" {
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//#define P(d) tmsg_warning(" - %d -\n",d)
Toshihiro Shimizu 890ddd
#define COPY_RASTER(iras, oras, border) tP.copy_raster(iras, oras, border, border, iras->lx - border - 1, iras->ly - border - 1, 0, 0)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define DIRACCURACY 12
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// ----- PatternMapping for UCHAR pixels (range 0-255) --------------------------
Toshihiro Shimizu 890ddd
void patternmapUC(const RASTER *iras, RASTER *oras, CPatternMapParam &pmP,
Toshihiro Shimizu 890ddd
				  const int border, RASTER *imgContour) // throw (SMemAllocError,SWriteRasterError,SFileReadError)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	try {
Toshihiro Shimizu 890ddd
		SRECT rect = {border, border, iras->lx - border - 1, iras->ly - border - 1};
Toshihiro Shimizu 890ddd
		SPOINT p = {0, 0};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		CSTColSelPic<uc_pixel> ipUC, ipOri;</uc_pixel>
Toshihiro Shimizu 890ddd
		ipUC.read(iras);
Toshihiro Shimizu 890ddd
		ipUC.initSel();
Toshihiro Shimizu 890ddd
		ipOri = ipUC;
Toshihiro Shimizu 890ddd
		// Selection of pixels using CM
Toshihiro Shimizu 890ddd
		CCIL emptyCIL;
Toshihiro Shimizu 890ddd
		int nbContourPixel = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (pmP.m_isKeepContour) {
Toshihiro Shimizu 890ddd
			CEraseContour ec(ipUC);
Toshihiro Shimizu 890ddd
			nbContourPixel = ec.makeSelection(pmP.m_ink);
Toshihiro Shimizu 890ddd
			//			nbContourPixel=ipUC.makeSelectionCMAP(pmP.m_ink,emptyCIL);
Toshihiro Shimizu 890ddd
		} else {
Toshihiro Shimizu 890ddd
			CEraseContour ec(ipUC);
Toshihiro Shimizu 890ddd
			nbContourPixel = ec.doIt(pmP.m_ink);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		if (nbContourPixel > 0) {
Toshihiro Shimizu 890ddd
			if (!pmP.m_isRandomDir) {
Toshihiro Shimizu 890ddd
				CSDirection dir(ipUC.m_lX, ipUC.m_lY, ipUC.m_sel, DIRACCURACY);
Toshihiro Shimizu 890ddd
				dir.doDir();
Toshihiro Shimizu 890ddd
				dir.getResult(ipUC.m_sel);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			CPatternPosition pPos(ipUC, nbContourPixel, pmP.m_density, pmP.m_minDist, pmP.m_maxDist);
Toshihiro Shimizu 890ddd
			// Reads the pattern
Toshihiro Shimizu 890ddd
			CPattern pat(imgContour);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			for (vector<spoint>::iterator pp = pPos.m_pos.begin();</spoint>
Toshihiro Shimizu 890ddd
				 pp != pPos.m_pos.end();
Toshihiro Shimizu 890ddd
				 pp++) {
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				// Calculates the rotation angle
Toshihiro Shimizu 890ddd
				double angle = 0.0;
Toshihiro Shimizu 890ddd
				if (pmP.m_isRandomDir)
Toshihiro Shimizu 890ddd
					angle = pmP.m_minDirAngle + (double)(rand() % 1001) * 0.001 * (pmP.m_maxDirAngle - pmP.m_minDirAngle);
Toshihiro Shimizu 890ddd
				else {
Toshihiro Shimizu 890ddd
					if (pp->x >= 0 && pp->y >= 0 && pp->x < ipUC.m_lX && pp->y < ipUC.m_lY) {
Toshihiro Shimizu 890ddd
						UCHAR *sel = ipUC.m_sel + pp->y * ipUC.m_lX + pp->x;
Toshihiro Shimizu 890ddd
						if (*sel > (UCHAR)0)
Toshihiro Shimizu 890ddd
							angle = (double)(*sel) - 50.0;
Toshihiro Shimizu 890ddd
					}
Toshihiro Shimizu 890ddd
					angle += pmP.m_minDirAngle + (double)(rand() % 1001) * 0.001 * (pmP.m_maxDirAngle - pmP.m_minDirAngle);
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				// Calculates the scale
Toshihiro Shimizu 890ddd
				double scale = 1.0;
Toshihiro Shimizu 890ddd
				scale = pmP.m_minScale + (double)(rand() % 1001) * 0.001 * (pmP.m_maxScale - pmP.m_minScale);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				// Mapping of Pattern
Toshihiro Shimizu 890ddd
				pat.mapIt(ipUC, ipOri, pp->x, pp->y, scale, angle, pmP.m_isUseInkColor, pmP.m_isIncludeAlpha);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		//		ipUC.showSelection();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		ipUC.write(oras, rect, p);
Toshihiro Shimizu 890ddd
	} catch (SMemAllocError) {
Toshihiro Shimizu 890ddd
		//SMemAllocError();
Toshihiro Shimizu 890ddd
	} catch (SWriteRasterError) {
Toshihiro Shimizu 890ddd
		//	SWriteRasterError();
Toshihiro Shimizu 890ddd
	} catch (SFileReadError) {
Toshihiro Shimizu 890ddd
		//	SFileReadError();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// ----- PatternMapping for USHORT pixels (range 0-255) --------------------------
Toshihiro Shimizu 890ddd
void patternmapUS(const RASTER *iras, RASTER *oras, CPatternMapParam &pmP,
Toshihiro Shimizu 890ddd
				  const int border, RASTER *imgContour) // throw (SMemAllocError,SWriteRasterError,SFileReadError)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	try {
Toshihiro Shimizu 890ddd
		SRECT rect = {border, border, iras->lx - border - 1, iras->ly - border - 1};
Toshihiro Shimizu 890ddd
		SPOINT p = {0, 0};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		CSTColSelPic<us_pixel> ipUS, ipOri;</us_pixel>
Toshihiro Shimizu 890ddd
		ipUS.read(iras);
Toshihiro Shimizu 890ddd
		ipUS.initSel();
Toshihiro Shimizu 890ddd
		ipOri = ipUS;
Toshihiro Shimizu 890ddd
		// Selection of pixels using CM
Toshihiro Shimizu 890ddd
		CCIL emptyCIL;
Toshihiro Shimizu 890ddd
		int nbContourPixel = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (pmP.m_isKeepContour) {
Toshihiro Shimizu 890ddd
			CEraseContour ec(ipUS);
Toshihiro Shimizu 890ddd
			nbContourPixel = ec.makeSelection(pmP.m_ink);
Toshihiro Shimizu 890ddd
			// a 255-nek utananezni !!!
Toshihiro Shimizu 890ddd
			//			nbContourPixel=ipUS.makeSelectionCMAP(pmP.m_ink,emptyCIL);
Toshihiro Shimizu 890ddd
		} else {
Toshihiro Shimizu 890ddd
			CEraseContour ec(ipUS);
Toshihiro Shimizu 890ddd
			nbContourPixel = ec.doIt(pmP.m_ink);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		if (nbContourPixel > 0) {
Toshihiro Shimizu 890ddd
			if (!pmP.m_isRandomDir) {
Toshihiro Shimizu 890ddd
				CSDirection dir(ipUS.m_lX, ipUS.m_lY, ipUS.m_sel, DIRACCURACY);
Toshihiro Shimizu 890ddd
				dir.doDir();
Toshihiro Shimizu 890ddd
				dir.getResult(ipUS.m_sel);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			CPatternPosition pPos(ipUS, nbContourPixel, pmP.m_density, pmP.m_minDist, pmP.m_maxDist);
Toshihiro Shimizu 890ddd
			// Reads the pattern
Toshihiro Shimizu 890ddd
			CPattern pat(imgContour);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			for (vector<spoint>::iterator pp = pPos.m_pos.begin();</spoint>
Toshihiro Shimizu 890ddd
				 pp != pPos.m_pos.end();
Toshihiro Shimizu 890ddd
				 pp++) {
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				// Calculates the rotation angle
Toshihiro Shimizu 890ddd
				double angle = 0.0;
Toshihiro Shimizu 890ddd
				if (pmP.m_isRandomDir)
Toshihiro Shimizu 890ddd
					angle = pmP.m_minDirAngle + (double)(rand() % 1001) * 0.001 * (pmP.m_maxDirAngle - pmP.m_minDirAngle);
Toshihiro Shimizu 890ddd
				else {
Toshihiro Shimizu 890ddd
					if (pp->x >= 0 && pp->y >= 0 && pp->x < ipUS.m_lX && pp->y < ipUS.m_lY) {
Toshihiro Shimizu 890ddd
						UCHAR *sel = ipUS.m_sel + pp->y * ipUS.m_lX + pp->x;
Toshihiro Shimizu 890ddd
						if (*sel > (UCHAR)0)
Toshihiro Shimizu 890ddd
							angle = (double)(*sel) - 50.0;
Toshihiro Shimizu 890ddd
					}
Toshihiro Shimizu 890ddd
					angle += pmP.m_minDirAngle + (double)(rand() % 1001) * 0.001 * (pmP.m_maxDirAngle - pmP.m_minDirAngle);
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				// Calculates the scale
Toshihiro Shimizu 890ddd
				double scale = 1.0;
Toshihiro Shimizu 890ddd
				scale = pmP.m_minScale + (double)(rand() % 1001) * 0.001 * (pmP.m_maxScale - pmP.m_minScale);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				// Mapping of Pattern
Toshihiro Shimizu 890ddd
				pat.mapIt(ipUS, ipOri, pp->x, pp->y, scale, angle, pmP.m_isUseInkColor, pmP.m_isIncludeAlpha);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		//		ipUC.showSelection();
Toshihiro Shimizu 890ddd
		ipUS.write(oras, rect, p);
Toshihiro Shimizu 890ddd
	} catch (SMemAllocError) {
Toshihiro Shimizu 890ddd
		//SMemAllocError();
Toshihiro Shimizu 890ddd
	} catch (SWriteRasterError) {
Toshihiro Shimizu 890ddd
		//	SWriteRasterError();
Toshihiro Shimizu 890ddd
	} catch (SFileReadError) {
Toshihiro Shimizu 890ddd
		//	SFileReadError();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int patternmap(const RASTER *iras, RASTER *oras, const int border,
Toshihiro Shimizu 890ddd
			   int argc, const char *argv[], const int shrink, RASTER *imgContour)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	// The input raster must be RAS_CM16 or RAS_CM24!!!!
Toshihiro Shimizu 890ddd
	CSTPic<uc_pixel> tP;</uc_pixel>
Toshihiro Shimizu 890ddd
	if (iras->type != RAS_CM32) {
Toshihiro Shimizu 890ddd
		COPY_RASTER(iras, oras, border);
Toshihiro Shimizu 890ddd
		return 0;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	try {
Toshihiro Shimizu 890ddd
		CPatternMapParam pmP(argc, argv, shrink);
Toshihiro Shimizu 890ddd
		if (!pmP.isOK()) {
Toshihiro Shimizu 890ddd
			COPY_RASTER(iras, oras, border);
Toshihiro Shimizu 890ddd
			return 0;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		srand(10);
Toshihiro Shimizu 890ddd
		if (oras->type == RAS_RGBM) {
Toshihiro Shimizu 890ddd
			patternmapUC(iras, oras, pmP, border, imgContour);
Toshihiro Shimizu 890ddd
		} else if (oras->type == RAS_RGBM64) {
Toshihiro Shimizu 890ddd
			patternmapUS(iras, oras, pmP, border, imgContour);
Toshihiro Shimizu 890ddd
		} else {
Toshihiro Shimizu 890ddd
			COPY_RASTER(iras, oras, border);
Toshihiro Shimizu 890ddd
			return 0;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	} catch (SMemAllocError e) {
Toshihiro Shimizu 890ddd
		e.debug_print();
Toshihiro Shimizu 890ddd
		COPY_RASTER(iras, oras, border);
Toshihiro Shimizu 890ddd
		return 0;
Toshihiro Shimizu 890ddd
	} catch (SWriteRasterError e) {
Toshihiro Shimizu 890ddd
		e.debug_print();
Toshihiro Shimizu 890ddd
		COPY_RASTER(iras, oras, border);
Toshihiro Shimizu 890ddd
		return 0;
Toshihiro Shimizu 890ddd
	} catch (SFileReadError e) {
Toshihiro Shimizu 890ddd
		e.debug_print();
Toshihiro Shimizu 890ddd
		COPY_RASTER(iras, oras, border);
Toshihiro Shimizu 890ddd
		return 0;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return 1;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef __cplusplus
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
#endif