aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Scripting/Tango.Scripting.Editors/Document/GapTextBuffer.cs
blob: a617284a948fbfbe25a11911e8a9b5c3d1f16203 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)

using System;
using System.Diagnostics;
using System.Text;

using Tango.Scripting.Editors.Utils;

namespace Tango.Scripting.Editors.Document
{
	/*
	/// <summary>
	/// Implementation of a gap text buffer.
	/// </summary>
	sealed class GapTextBuffer
	{
		char[] buffer = Empty<char>.Array;
		
		/// <summary>
		/// The current text content.
		/// Is set to null whenever the buffer changes, and gets a value only when the
		/// full text content is requested.
		/// </summary>
		string textContent;
		
		/// <summary>
		/// last GetText result
		/// </summary>
		string lastGetTextResult;
		int lastGetTextRequestOffset;
		
		int gapBeginOffset;
		int gapEndOffset;
		int gapLength; // gapLength == gapEndOffset - gapBeginOffset
		
		/// <summary>
		/// when gap is too small for inserted text or gap is too large (exceeds maxGapLength),
		/// a new buffer is reallocated with a new gap of at least this size.
		/// </summary>
		const int minGapLength = 128;
		
		/// <summary>
		/// when the gap exceeds this size, reallocate a smaller buffer
		/// </summary>
		const int maxGapLength = 4096;
		
		public int Length {
			get {
				return buffer.Length - gapLength;
			}
		}
		
		/// <summary>
		/// Gets the buffer content.
		/// </summary>
		public string Text {
			get {
				if (textContent == null)
					textContent = GetText(0, Length);
				return textContent;
			}
			set {
				Debug.Assert(value != null);
				textContent = value;  lastGetTextResult = null;
				buffer = new char[value.Length + minGapLength];
				value.CopyTo(0, buffer, 0, value.Length);
				gapBeginOffset = value.Length;
				gapEndOffset = buffer.Length;
				gapLength = gapEndOffset - gapBeginOffset;
			}
		}
		
		public char GetCharAt(int offset)
		{
			return offset < gapBeginOffset ? buffer[offset] : buffer[offset + gapLength];
		}
		
		public string GetText(int offset, int length)
		{
			if (length == 0)
				return string.Empty;
			if (lastGetTextRequestOffset == offset && lastGetTextResult != null && length == lastGetTextResult.Length)
				return lastGetTextResult;
			
			int end = offset + length;
			string result;
			if (end < gapBeginOffset) {
				result = new string(buffer, offset, length);
			} else if (offset > gapBeginOffset) {
				result = new string(buffer, offset + gapLength, length);
			} else {
				int block1Size = gapBeginOffset - offset;
				int block2Size = end - gapBeginOffset;
				
				StringBuilder buf = new StringBuilder(block1Size + block2Size);
				buf.Append(buffer, offset,       block1Size);
				buf.Append(buffer, gapEndOffset, block2Size);
				result = buf.ToString();
			}
			lastGetTextRequestOffset = offset;
			lastGetTextResult = result;
			return result;
		}
		
		/// <summary>
		/// Inserts text at the specified offset.
		/// </summary>
		public void Insert(int offset, string text)
		{
			Debug.Assert(offset >= 0 && offset <= Length);
			
			if (text.Length == 0)
				return;
			
			textContent = null; lastGetTextResult = null;
			PlaceGap(offset, text.Length);
			text.CopyTo(0, buffer, gapBeginOffset, text.Length);
			gapBeginOffset += text.Length;
			gapLength = gapEndOffset - gapBeginOffset;
		}
		
		/// <summary>
		/// Remove <paramref name="length"/> characters at <paramref name="offset"/>.
		/// Leave a gap of at least <paramref name="reserveGapSize"/>.
		/// </summary>
		public void Remove(int offset, int length, int reserveGapSize)
		{
			Debug.Assert(offset >= 0 && offset <= Length);
			Debug.Assert(length >= 0 && offset + length <= Length);
			Debug.Assert(reserveGapSize >= 0);
			
			if (length == 0)
				return;
			
			textContent = null; lastGetTextResult = null;
			PlaceGap(offset, reserveGapSize - length);
			gapEndOffset += length; // delete removed text
			gapLength = gapEndOffset - gapBeginOffset;
			if (gapLength - reserveGapSize > maxGapLength && gapLength - reserveGapSize > buffer.Length / 4) {
				// shrink gap
				MakeNewBuffer(gapBeginOffset, reserveGapSize + minGapLength);
			}
		}
		
		void PlaceGap(int newGapOffset, int minRequiredGapLength)
		{
			if (gapLength < minRequiredGapLength) {
				// enlarge gap
				MakeNewBuffer(newGapOffset, minRequiredGapLength + Math.Max(minGapLength, buffer.Length / 8));
			} else {
				while (newGapOffset < gapBeginOffset) {
					buffer[--gapEndOffset] = buffer[--gapBeginOffset];
				}
				while (newGapOffset > gapBeginOffset) {
					buffer[gapBeginOffset++] = buffer[gapEndOffset++];
				}
			}
		}
		
		void MakeNewBuffer(int newGapOffset, int newGapLength)
		{
			char[] newBuffer = new char[Length + newGapLength];
			Debug.WriteLine("GapTextBuffer was reallocated, new size=" + newBuffer.Length);
			if (newGapOffset < gapBeginOffset) {
				// gap is moving backwards
				
				// first part:
				Array.Copy(buffer, 0, newBuffer, 0, newGapOffset);
				// moving middle part:
				Array.Copy(buffer, newGapOffset, newBuffer, newGapOffset + newGapLength, gapBeginOffset - newGapOffset);
				// last part:
				Array.Copy(buffer, gapEndOffset, newBuffer, newBuffer.Length - (buffer.Length - gapEndOffset), buffer.Length - gapEndOffset);
			} else {
				// gap is moving forwards
				// first part:
				Array.Copy(buffer, 0, newBuffer, 0, gapBeginOffset);
				// moving middle part:
				Array.Copy(buffer, gapEndOffset, newBuffer, gapBeginOffset, newGapOffset - gapBeginOffset);
				// last part:
				int lastPartLength = newBuffer.Length - (newGapOffset + newGapLength);
				Array.Copy(buffer, buffer.Length - lastPartLength, newBuffer, newGapOffset + newGapLength, lastPartLength);
			}
			
			gapBeginOffset = newGapOffset;
			gapEndOffset = newGapOffset + newGapLength;
			gapLength = newGapLength;
			buffer = newBuffer;
		}
	}
	*/
}
an class="w"> namespace std; #define LFactor 100 #define abFactor 255 #define abOffset 128 #define InkFactor 100 #define Uint16Factor 65535 /*#define _CRTDBG_MAP_ALLOC #include <stdlib.h> #include <crtdbg.h> #include <cstdlib> #ifdef _DEBUG #define DBG_NEW new ( _NORMAL_BLOCK , __FILE__ , __LINE__ ) // Replace _NORMAL_BLOCK with _CLIENT_BLOCK if you want the // allocations to be of _CLIENT_BLOCK type #else #define DBG_NEW new #endif */ // NHedral interpolation given the NDimensional LUT. N=3 or N=4 are supported. //Assumes the right table is loaded ColorTransf::ColorTransf() : m_MSBShift(0), m_DataBuffer(NULL), m_SeparationsIn(0), m_SeparationsOut(0), m_nGridPoints(0), m_GamutLimitsNlperCM(NULL), m_NGamutRegions(0), m_ByteBuffer(NULL), m_InputCurves(NULL), m_OutputCurves(NULL) { } ColorTransf::~ColorTransf() { if (m_DataBuffer != NULL) { delete[] m_DataBuffer; m_DataBuffer = NULL; } if (m_GamutLimitsNlperCM != NULL) { delete[] m_GamutLimitsNlperCM; m_GamutLimitsNlperCM = NULL; } if (m_ByteBuffer != NULL) { delete[] m_ByteBuffer; m_ByteBuffer = NULL; } if (m_InputCurves != NULL) { delete[] m_InputCurves; m_InputCurves = NULL; } if (m_OutputCurves != NULL) { delete[] m_OutputCurves; m_OutputCurves = NULL; } } void ColorTransf::InitData(unsigned char *colorTransformBuffer, long colorTransformSize) { /* the whole file is now loaded in the memory buffer. */ /*Parse data*/ /* 0-3 'prec1', 'prec2' 4-7 reserved, must be 0 8 number of input channels, uint8 9 number of output channels, uint8 10 number of CLUT grid points, uint8 11 Most Significant bits shift 12 Number of gamut regions 13-14 Number of Input Table entries 15-16 Number of Output Table entries prec 2 (16bit) 17-n CLUT values, uint16 prec 1 17-n CLUT values, uint8 */ //long lSize, lSizeHalf; // obtain file size: int lSize = colorTransformSize; //lSizeHalf = lSize / 2; // allocate memory to contain the whole file: unsigned char *buffer = colorTransformBuffer; if (buffer == NULL) { throw std::exception("Memory Error, ColorTransf::InitData"); } NumConversions Conv; int bytesread = 0; int tmpB =Conv.ByteToInt(buffer, 0); bytesread += 4; int n = sizeof((char*)&tmpB); //char *tmpC = DBG_NEW char[n] ; char *tmpC = new char[n]; Conv.getchar(tmpB, n, tmpC); char *luttype = new char[n+1]; //char *luttype = DBG_NEW char[n + 1]; strncpy_s(luttype, n+1,tmpC, n); int TablePrecision; if (strncmp(luttype, "prc1",n)==0) TablePrecision = 1; else if (strncmp(luttype, "prc2",n)==0) TablePrecision = 2; else { throw std::exception("Wrong precision in Color Tables"); return; } if (luttype != NULL) { delete[] luttype; luttype = NULL; } if (tmpC != NULL) { delete[] tmpC; tmpC = NULL; } // Skip past reserved padding bytes bytesread += 4; uint8_t num_input_channels = buffer[ bytesread]; SetSeparationsIn((int)num_input_channels); //Numer of Separations In bytesread += 1; uint8_t num_output_channels = buffer[ bytesread]; SetSeparationsOut((int)num_output_channels); //Numer of Separations Out bytesread += 1; uint8_t num_clut_grid_points = buffer[ bytesread]; SetNGridpoints((int)num_clut_grid_points); //Number of Gridpoints bytesread += 1; uint8_t num_Sh4MSB = buffer[ bytesread]; bytesread += 1; unsigned short checkMSB = 256 >> (num_Sh4MSB); if (num_clut_grid_points != (checkMSB + 1)) { throw std::exception("Wrong Number of MSB's, ColorTransf::InitData"); } else SetMSBShift((int)num_Sh4MSB); //Number of MSB's unsigned short num_input_table_entries = Conv.ByteToShort(buffer, bytesread); bytesread += 2; Set_NumInputTableEntries((int)num_input_table_entries); unsigned short num_output_table_entries = Conv.ByteToShort(buffer, bytesread); bytesread += 2; Set_NumOutputTableEntries((int)num_output_table_entries); m_InputCurves = new Interp[m_SeparationsIn]; double *xIn = new double[m_num_input_table_entries]; double *yIn = new double[m_num_input_table_entries]; //read Input tables if (TablePrecision == 1) { double deltax = 255.0 / (m_num_input_table_entries - 1); for (int i = 0; i < m_num_input_table_entries; ++i) xIn[i] = deltax * i; for (int i = 0; i < m_SeparationsIn; ++i) { for (int j = 0; j < m_num_input_table_entries; ++j) { yIn[j] = buffer[bytesread]; bytesread += 1; } m_InputCurves[i].Init(xIn, yIn, m_num_input_table_entries); } } else { double deltax = 65535.0 / (m_num_input_table_entries - 1); for (int i = 0; i < m_num_input_table_entries; ++i) xIn[i] = deltax * i; for (int i = 0; i < m_SeparationsIn; ++i) { for (int j = 0; j < m_num_input_table_entries; ++j) { yIn[j] = Conv.ByteToShort(buffer, bytesread); bytesread += 2; } m_InputCurves[i].Init(xIn, yIn, m_num_input_table_entries); } } int clut_size = (int)pow(double(num_clut_grid_points), m_SeparationsIn)* m_SeparationsOut; // lut8Type and lut16Type if (TablePrecision == 1) { // int lsizeH2 = (lSize - bytesread + 1) / 2; m_ByteBuffer = new unsigned char[clut_size]; int lSizeperSep = clut_size / m_SeparationsOut; int indR = 0; for (int i = 0; i < lSizeperSep; ++i) { for (int j = 0; j < m_SeparationsOut; ++j) { //m_ByteBuffer[indR] = Conv.ByteToShort(buffer, bytesread); m_ByteBuffer[indR] = buffer[bytesread]; bytesread += 1; indR++; } } m_InterpColor.InitData(m_ByteBuffer, m_SeparationsIn, m_SeparationsOut, m_nGridPoints, m_MSBShift); } else { // int lsizeH4 = (lSize - bytesread + 1) / 2; m_DataBuffer = new unsigned short[clut_size]; //m_DataBuffer = DBG_NEW unsigned short[lsizeH4]; int lSizeperSep = clut_size / m_SeparationsOut; int indR = 0; for (int i = 0; i < lSizeperSep; ++i) { for (int j = 0; j < m_SeparationsOut; ++j) { m_DataBuffer[indR] = Conv.ByteToShort(buffer, bytesread); bytesread += 2; indR++; } } // terminate reading data file m_InterpColor.InitData(m_DataBuffer, m_SeparationsIn, m_SeparationsOut, m_nGridPoints, m_MSBShift); } //Read Output Curves double *xOut = new double[m_num_output_table_entries]; double *yOut = new double[m_num_output_table_entries]; m_OutputCurves = new Interp[m_SeparationsOut]; //read Input tables if (TablePrecision == 1) { double deltax = 255.0 / (m_num_output_table_entries - 1); for (int i = 0; i < m_num_output_table_entries; ++i) xOut[i] = deltax * i; for (int i = 0; i < m_SeparationsOut; ++i) { for (int j = 0; j < m_num_output_table_entries; ++j) { yOut[j] = buffer[bytesread]; bytesread += 1; } m_OutputCurves[i].Init(xOut, yOut, m_num_output_table_entries); } } else { double deltax = 65535.0 / (m_num_output_table_entries - 1); for (int i = 0; i < m_num_output_table_entries; ++i) xOut[i] = deltax * i; for (int i = 0; i < m_SeparationsOut; ++i) { for (int j = 0; j < m_num_output_table_entries; ++j) { yOut[j] = Conv.ByteToShort(buffer, bytesread); bytesread += 2; } m_OutputCurves[i].Init(xOut, yOut, m_num_output_table_entries); } } //clean up if (xIn != NULL) { delete[] xIn; xIn = NULL; } if (yIn != NULL) { delete[] yIn; yIn = NULL; } if (xOut != NULL) { delete[] xOut; xOut = NULL; } if (yOut != NULL) { delete[] yOut; yOut = NULL; } return; } void ColorTransf::SetGamutLimitsNlperCM(double *GamutLimitsNlperCM) { //m_GamutLimitsNlperCM = DBG_NEW double[m_NGamutRegions]; m_GamutLimitsNlperCM = new double[m_NGamutRegions]; for (int i = 0; i < m_NGamutRegions; ++i) m_GamutLimitsNlperCM[i] = GamutLimitsNlperCM[i]; } void ColorTransf::evalLab2InkP(double *ColorIn, double *&ColorOut, int &GamutRegion) { //double *tmpColorOut = new double[m_SeparationsOut]; C_RGB_XYZ_Lab tmpColorOut; tmpColorOut = tmpColorOut.labdouble_to_labuint16(ColorIn); /*Convert ColorIn to uint16 */ uint16_t *iColorIn = new uint16_t[m_SeparationsIn]; //uint16_t *iColorIn = DBG_NEW uint16_t[m_SeparationsIn]; iColorIn[0] = uint16_t(tmpColorOut.Get_x()); iColorIn[1] = uint16_t(tmpColorOut.Get_y()); iColorIn[2] = uint16_t(tmpColorOut.Get_z()); double *tmpColor = new double[m_SeparationsOut]; //double *tmpColor = DBG_NEW double[m_SeparationsIn]; double tmpIC = 0; //Aply Input Curves for (int i = 0; i < m_SeparationsIn; ++i) { m_InputCurves[i].Eval((double)iColorIn[i], tmpIC); iColorIn[i] = (unsigned short)tmpIC; } if (m_SeparationsIn == 3) m_InterpColor.ColorMap3(iColorIn, tmpColor); else if (m_SeparationsIn == 4) { m_InterpColor.ColorMap4(iColorIn, tmpColor); } else throw std::exception("Unsupported Number of Separations in ColorTransf::evalLab2Ink"); //Appy Output Curves for (int i = 0; i < m_SeparationsOut; ++i) { m_OutputCurves[i].Eval((double)tmpColor[i], tmpIC); tmpColor[i] = (unsigned short)tmpIC; } //tmpColorOut between 0and 255 //normalize to [0-100] for (int i = 0; i < m_SeparationsOut; ++i) ColorOut[i] = tmpColor[i] * InkFactor/ Uint16Factor; GamutRegion = 0; if(iColorIn !=NULL) { delete[] iColorIn; iColorIn = NULL; } if (tmpColor != NULL) { delete[] tmpColor; tmpColor = NULL; } return; } void ColorTransf::evalInCurve(double *ColorIn, double *&ColorOut) { //To be used to transform Nonlinear Inks to Linear with m_A2B transform /*Convert ColorIn to uint16 */ uint16_t *iColorIn = new uint16_t[m_SeparationsIn]; //uint16_t *iColorIn = DBG_NEW uint16_t[m_SeparationsIn]; double tmpIC = 0; for (int i = 0; i < m_SeparationsIn; ++i) { //convert to 16 bits iColorIn[i] = uint16_t(double(ColorIn[i] / InkFactor)*Uint16Factor); //Aply Input Curves m_InputCurves[i].Eval((double)iColorIn[i], tmpIC); ColorOut[i] = tmpIC * InkFactor / Uint16Factor; } if (iColorIn != NULL) { delete[] iColorIn; iColorIn = NULL; } return; } void ColorTransf::evalOutCurve(double *ColorIn, double *&ColorOut) { //To be used to transform Linear Inks to Nonlinear with m_B2A transform /*Convert ColorIn to uint16 */ uint16_t *iColorIn = new uint16_t[m_SeparationsOut]; //uint16_t *iColorIn = DBG_NEW uint16_t[m_SeparationsIn]; double tmpIC = 0; for (int i = 0; i < m_SeparationsOut; ++i) { //convert to 16 bits iColorIn[i] = uint16_t(double(ColorIn[i] / InkFactor)*Uint16Factor); //Aply Output Curves m_OutputCurves[i].Eval((double)iColorIn[i], tmpIC); ColorOut[i] = tmpIC * InkFactor / Uint16Factor; } if (iColorIn != NULL) { delete[] iColorIn; iColorIn = NULL; } return; } void ColorTransf::evalInkP2Lab(double *ColorIn, double *&ColorOut, int &GamutRegion) { double *tmpColorOut = new double[m_SeparationsOut]; //double *tmpColorOut = DBG_NEW double[m_SeparationsOut]; /*Convert ColorIn to uint16 */ uint16_t *iColorIn = new uint16_t[m_SeparationsIn]; //uint16_t *iColorIn = DBG_NEW uint16_t[m_SeparationsIn]; double tmpIC = 0; //convert to 16 bits for (int i=0; i<m_SeparationsIn; ++i) iColorIn[i] = uint16_t(double(ColorIn[i] / InkFactor)*Uint16Factor); //Aply Input Curves for (int i = 0; i < m_SeparationsIn; ++i) { m_InputCurves[i].Eval((double)iColorIn[i], tmpIC); iColorIn[i] = (unsigned short)tmpIC; } if (m_SeparationsIn == 3) m_InterpColor.ColorMap3(iColorIn, tmpColorOut); // return Value is double in units of 16 bits else if (m_SeparationsIn == 4) m_InterpColor.ColorMap4(iColorIn, tmpColorOut); // return value is double in units on 16 bits else throw std::exception("Unsupported Number of Separations in ColorTransf::evalInkP2Lab"); //Appy Output Curves for (int i = 0; i < m_SeparationsOut; ++i) { m_OutputCurves[i].Eval((double)tmpColorOut[i], tmpIC); tmpColorOut[i] = (unsigned short)tmpIC; } //Normalize to Lab Space C_RGB_XYZ_Lab tmpLabOut; uint16_t int16ColorOut[3]; for (int i=0; i<3; ++i) int16ColorOut[i] = (uint16_t )(tmpColorOut[i]); tmpLabOut = tmpLabOut.labuint16_to_labdouble(int16ColorOut); ColorOut[0] = tmpLabOut.Get_x(); ColorOut[1] = tmpLabOut.Get_y(); ColorOut[2] = tmpLabOut.Get_z(); //GamutRegion = 0; if (iColorIn != NULL) { delete[] iColorIn; iColorIn = NULL; } if (tmpColorOut != NULL) { delete[] tmpColorOut; tmpColorOut = NULL; } return; } /* __declspec(dllexport) int ColorTransf::evalCMY2RGB(double *ColorIn, double *ColorOut) { //Assumption: ColorIn is in the interval [0,100] double *tmpColorOut = new double[m_SeparationsIn]; //dimension of RGB //Apply nonlinear transformation convert to linear, inks are in nonlinear space double LinTmp = -1; int ret = evalLinSingleCurve(m_CalCyan, m_nCalCyan, ColorIn[0], &LinTmp); tmpColorOut[0] = LinTmp; ret = evalLinSingleCurve(m_CalMagenta, m_nCalMagenta, ColorIn[1], &LinTmp); tmpColorOut[1] = LinTmp; ret = evalLinSingleCurve(m_CalYellow, m_nCalYellow, ColorIn[2], &LinTmp); tmpColorOut[2] = LinTmp; //tmpColorOut is in the [0,100] interval //Convert tmpColorOut to unsigned char unsigned char *iColorOut = new unsigned char[m_SeparationsIn]; for (int i = 0; i < m_SeparationsIn; ++i) { iColorOut[i] = (unsigned char)(fmin(fmax(round(tmpColorOut[i] * 2.55), 0), 255)); } if (m_SeparationsIn == 3) m_InterpColor.ColorMap3(iColorOut, ColorOut); else if (m_SeparationsIn == 4) m_InterpColor.ColorMap4(iColorOut, ColorOut); delete[] iColorOut; delete[] tmpColorOut; return(0); } */