public MissingEncodingViewModel() { EncodingList = new EncodingState[] { new EncodingState(EncodingName.Base16), new EncodingState(EncodingName.Base43), new EncodingState(EncodingName.Base58), new EncodingState(EncodingName.Base58Check), new EncodingState(EncodingName.Base64), }; IObservable <bool> isFindEnabled = this.WhenAnyValue( x => x.Input, (input) => !string.IsNullOrEmpty(input)); FindCommand = ReactiveCommand.Create(Find, isFindEnabled); }
/* * Encode a run of pixels. */ private bool PackBitsEncode(byte[] buf, int offset, int cc, short s) { int op = m_tif.m_rawcp; EncodingState state = EncodingState.BASE; int lastliteral = 0; int bp = offset; while (cc > 0) { /* * Find the longest string of identical bytes. */ int b = buf[bp]; bp++; cc--; int n = 1; for (; cc > 0 && b == buf[bp]; cc--, bp++) { n++; } bool stop = false; while (!stop) { if (op + 2 >= m_tif.m_rawdatasize) { /* insure space for new data */ /* * Be careful about writing the last * literal. Must write up to that point * and then copy the remainder to the * front of the buffer. */ if (state == EncodingState.LITERAL || state == EncodingState.LITERAL_RUN) { int slop = op - lastliteral; m_tif.m_rawcc += lastliteral - m_tif.m_rawcp; if (!m_tif.flushData1()) { return(false); } op = m_tif.m_rawcp; while (slop-- > 0) { m_tif.m_rawdata[op] = m_tif.m_rawdata[lastliteral]; lastliteral++; op++; } lastliteral = m_tif.m_rawcp; } else { m_tif.m_rawcc += op - m_tif.m_rawcp; if (!m_tif.flushData1()) { return(false); } op = m_tif.m_rawcp; } } switch (state) { case EncodingState.BASE: /* initial state, set run/literal */ if (n > 1) { state = EncodingState.RUN; if (n > 128) { int temp = -127; m_tif.m_rawdata[op] = (byte)temp; op++; m_tif.m_rawdata[op] = (byte)b; op++; n -= 128; continue; } m_tif.m_rawdata[op] = (byte)(-n + 1); op++; m_tif.m_rawdata[op] = (byte)b; op++; } else { lastliteral = op; m_tif.m_rawdata[op] = 0; op++; m_tif.m_rawdata[op] = (byte)b; op++; state = EncodingState.LITERAL; } stop = true; break; case EncodingState.LITERAL: /* last object was literal string */ if (n > 1) { state = EncodingState.LITERAL_RUN; if (n > 128) { int temp = -127; m_tif.m_rawdata[op] = (byte)temp; op++; m_tif.m_rawdata[op] = (byte)b; op++; n -= 128; continue; } m_tif.m_rawdata[op] = (byte)(-n + 1); /* encode run */ op++; m_tif.m_rawdata[op] = (byte)b; op++; } else { /* extend literal */ m_tif.m_rawdata[lastliteral]++; if (m_tif.m_rawdata[lastliteral] == 127) { state = EncodingState.BASE; } m_tif.m_rawdata[op] = (byte)b; op++; } stop = true; break; case EncodingState.RUN: /* last object was run */ if (n > 1) { if (n > 128) { int temp = -127; m_tif.m_rawdata[op] = (byte)temp; op++; m_tif.m_rawdata[op] = (byte)b; op++; n -= 128; continue; } m_tif.m_rawdata[op] = (byte)(-n + 1); op++; m_tif.m_rawdata[op] = (byte)b; op++; } else { lastliteral = op; m_tif.m_rawdata[op] = 0; op++; m_tif.m_rawdata[op] = (byte)b; op++; state = EncodingState.LITERAL; } stop = true; break; case EncodingState.LITERAL_RUN: /* literal followed by a run */ /* * Check to see if previous run should * be converted to a literal, in which * case we convert literal-run-literal * to a single literal. */ int atemp = -1; if (n == 1 && m_tif.m_rawdata[op - 2] == (byte)atemp && m_tif.m_rawdata[lastliteral] < 126) { m_tif.m_rawdata[lastliteral] += 2; state = (m_tif.m_rawdata[lastliteral] == 127 ? EncodingState.BASE : EncodingState.LITERAL); m_tif.m_rawdata[op - 2] = m_tif.m_rawdata[op - 1]; /* replicate */ } else { state = EncodingState.RUN; } continue; } } } m_tif.m_rawcc += op - m_tif.m_rawcp; m_tif.m_rawcp = op; return(true); }
private void LoadFromStream(Stream fileStream, StreamReader reader) { fTree.State = GEDCOMState.osLoading; try { ProgressEventHandler progressHandler = fTree.OnProgress; fSourceEncoding = DEFAULT_ENCODING; fEncodingState = EncodingState.esUnchecked; long fileSize = fileStream.Length; int progress = 0; GEDCOMCustomRecord curRecord = null; GEDCOMTag curTag = null; int lineNum = 0; while (reader.Peek() != -1) { lineNum++; string str = reader.ReadLine(); str = GEDCOMUtils.TrimLeft(str); if (str.Length == 0) { continue; } if (!ConvertHelper.IsDigit(str[0])) { FixFTBLine(curRecord, curTag, lineNum, str); } else { int tagLevel; string tagXRef = "", tagName, tagValue = ""; try { var strTok = new StringTokenizer(str); strTok.RecognizeDecimals = false; strTok.IgnoreWhiteSpace = false; strTok.RecognizeIdents = true; var token = strTok.Next(); // already trimmed if (token.Kind != TokenKind.Number) { // syntax error throw new EGEDCOMException(string.Format("The string {0} doesn't start with a valid number", str)); } tagLevel = (int)token.ValObj; token = strTok.Next(); if (token.Kind != TokenKind.WhiteSpace) { // syntax error } token = strTok.Next(); if (token.Kind == TokenKind.Symbol && token.Value[0] == '@') { token = strTok.Next(); while (token.Kind != TokenKind.Symbol && token.Value[0] != '@') { tagXRef += token.Value; token = strTok.Next(); } // FIXME: check for errors //throw new EGEDCOMException(string.Format("The string {0} contains an unterminated XRef pointer", str)); //throw new EGEDCOMException(string.Format("The string {0} is expected to start with an XRef pointer", str)); token = strTok.Next(); strTok.SkipWhiteSpaces(); } token = strTok.CurrentToken; if (token.Kind != TokenKind.Word && token.Kind != TokenKind.Ident) { // syntax error } tagName = token.Value.ToUpperInvariant(); token = strTok.Next(); if (token.Kind == TokenKind.WhiteSpace) { tagValue = strTok.GetRest(); } } catch (EGEDCOMException ex) { throw new EGEDCOMException("Syntax error in line " + Convert.ToString(lineNum) + ".\r" + ex.Message); } // convert codepages if (!string.IsNullOrEmpty(tagValue) && fEncodingState == EncodingState.esChanged) { tagValue = ConvertStr(fSourceEncoding, tagValue); } if (tagLevel == 0) { if (curRecord == fTree.Header && fEncodingState == EncodingState.esUnchecked) { // beginning recognition of the first is not header record // to check for additional versions of the code page DefineEncoding(reader); } if (tagName == "INDI") { curRecord = fTree.AddRecord(new GEDCOMIndividualRecord(fTree, fTree, "", "")); } else if (tagName == "FAM") { curRecord = fTree.AddRecord(new GEDCOMFamilyRecord(fTree, fTree, "", "")); } else if (tagName == "OBJE") { curRecord = fTree.AddRecord(new GEDCOMMultimediaRecord(fTree, fTree, "", "")); } else if (tagName == "NOTE") { curRecord = fTree.AddRecord(new GEDCOMNoteRecord(fTree, fTree, "", tagValue)); } else if (tagName == "REPO") { curRecord = fTree.AddRecord(new GEDCOMRepositoryRecord(fTree, fTree, "", "")); } else if (tagName == "SOUR") { curRecord = fTree.AddRecord(new GEDCOMSourceRecord(fTree, fTree, "", "")); } else if (tagName == "SUBN") { curRecord = fTree.AddRecord(new GEDCOMSubmissionRecord(fTree, fTree, "", "")); } else if (tagName == "SUBM") { curRecord = fTree.AddRecord(new GEDCOMSubmitterRecord(fTree, fTree, "", "")); } else if (tagName == "_GROUP") { curRecord = fTree.AddRecord(new GEDCOMGroupRecord(fTree, fTree, "", "")); } else if (tagName == "_RESEARCH") { curRecord = fTree.AddRecord(new GEDCOMResearchRecord(fTree, fTree, "", "")); } else if (tagName == "_TASK") { curRecord = fTree.AddRecord(new GEDCOMTaskRecord(fTree, fTree, "", "")); } else if (tagName == "_COMM") { curRecord = fTree.AddRecord(new GEDCOMCommunicationRecord(fTree, fTree, "", "")); } else if (tagName == "_LOC") { curRecord = fTree.AddRecord(new GEDCOMLocationRecord(fTree, fTree, "", "")); } else if (tagName == "HEAD") { curRecord = fTree.Header; } else if (tagName == "TRLR") { break; } else { curRecord = null; } if (curRecord != null && tagXRef != "") { curRecord.XRef = tagXRef; } curTag = null; } else { if (curRecord != null) { if (curTag == null || tagLevel == 1) { curTag = curRecord.AddTag(tagName, tagValue, null); } else { while (tagLevel <= curTag.Level) { curTag = (curTag.Parent as GEDCOMTag); } curTag = curTag.AddTag(tagName, tagValue, null); } } } } if (progressHandler != null) { int newProgress = (int)Math.Min(100, (fileStream.Position * 100.0f) / fileSize); if (progress != newProgress) { progress = newProgress; progressHandler(fTree, progress); } } } } finally { fTree.State = GEDCOMState.osReady; } }
private void SetEncoding(Encoding encoding) { fSourceEncoding = encoding; fEncodingState = EncodingState.esChanged; }
private void DefineEncoding(StreamReader reader) { var format = GetGEDCOMFormat(fTree); GEDCOMCharacterSet charSet = fTree.Header.CharacterSet; switch (charSet) { case GEDCOMCharacterSet.csUTF8: if (!SysUtils.IsUnicodeEncoding(reader.CurrentEncoding)) { SetEncoding(Encoding.UTF8); // file without BOM } else { fEncodingState = EncodingState.esUnchanged; } break; case GEDCOMCharacterSet.csUNICODE: if (!SysUtils.IsUnicodeEncoding(reader.CurrentEncoding)) { SetEncoding(Encoding.Unicode); // file without BOM } else { fEncodingState = EncodingState.esUnchanged; } break; case GEDCOMCharacterSet.csANSEL: if (format == GEDCOMFormat.gf_ALTREE) { // Agelong Tree 4.0 with ANSEL is actually characteristic // for the Russian-language data export SetEncoding(Encoding.GetEncoding(1251)); } else { SetEncoding(new AnselEncoding()); } break; case GEDCOMCharacterSet.csASCII: if (format == GEDCOMFormat.gf_Native) { // GEDKeeper native format (old) and ASCII charset SetEncoding(Encoding.GetEncoding(1251)); } else { var fmtProps = GEDCOMFormats[(int)format]; if (fmtProps.PredefCharset > -1) { SetEncoding(Encoding.GetEncoding(fmtProps.PredefCharset)); } else { string cpVers = fTree.Header.CharacterSetVersion; if (!string.IsNullOrEmpty(cpVers)) { int sourceCodepage = ConvertHelper.ParseInt(cpVers, DEF_CODEPAGE); SetEncoding(Encoding.GetEncoding(sourceCodepage)); } else { if (fTree.Header.Language.Value == GEDCOMLanguageID.Russian) { SetEncoding(Encoding.GetEncoding(1251)); } else { SetEncoding(Encoding.GetEncoding(DEF_CODEPAGE)); } } } } break; } }
private void DefineEncoding(StreamReader reader) { GEDCOMCharacterSet charSet = fTree.Header.CharacterSet; switch (charSet) { case GEDCOMCharacterSet.csUTF8: if (!SysUtils.IsUnicodeEncoding(reader.CurrentEncoding)) { SetEncoding(Encoding.UTF8); // file without BOM } else { fEncodingState = EncodingState.esUnchanged; } break; case GEDCOMCharacterSet.csUNICODE: if (!SysUtils.IsUnicodeEncoding(reader.CurrentEncoding)) { SetEncoding(Encoding.Unicode); // file without BOM } else { fEncodingState = EncodingState.esUnchanged; } break; case GEDCOMCharacterSet.csANSEL: SetEncoding(new AnselEncoding()); break; case GEDCOMCharacterSet.csASCII: var format = GetGEDCOMFormat(fTree); if (format == GEDCOMFormat.gf_Native) { // GEDKeeper native format (old) and ASCII charset SetEncoding(Encoding.GetEncoding(1251)); } else { var fmtProps = GEDCOMFormats[(int)format]; if (fmtProps.PredefCharset > -1) { SetEncoding(Encoding.GetEncoding(fmtProps.PredefCharset)); } else { string cpVers = fTree.Header.CharacterSetVersion; if (!string.IsNullOrEmpty(cpVers)) { int sourceCodepage = SysUtils.ParseInt(cpVers, DEF_CODEPAGE); SetEncoding(Encoding.GetEncoding(sourceCodepage)); } else { SetEncoding(Encoding.GetEncoding(DEF_CODEPAGE)); } } } break; } }
public void SetEncodingState(string projectId, EncodingState state, EncodingStage stage, string errorMessage = null) { }
public void SetEncodingState(string projectId, EncodingState state, EncodingStage stage, string errorMessage = null) { }
public LRLE(Bitmap image) { magic = 0x454C524C; version = 0x32303056; int imageWidth = image.Width; int imageHeight = image.Height; width = (ushort)imageWidth; height = (ushort)imageHeight; List <Command>[] commandLists = new List <Command> [9]; EncodingState currentState = EncodingState.StartNew; uint runLength = 0; List <byte[]> colorArray = new List <byte[]>(); byte[] lastPixel = new byte[4]; ColorTable colors = new ColorTable(); List <byte[]> runColors = new List <byte[]>(); for (int m = 0; m < 9; m++) { byte[] mipPixels = GetMipBytes(image); commandLists[m] = new List <Command>(); Array.Copy(mipPixels, 0, lastPixel, 0, 4); currentState = EncodingState.StartNew; int x = 0, y = 0; int w1 = image.Width * 4; for (int i = 0; i < mipPixels.Length; i += 64) { for (int j = 0; j < 4; j++) { for (int k = 0; k < 16; k += 4) { byte[] tmp = new byte[4]; Array.Copy(mipPixels, (y * w1) + x + k, tmp, 0, 4); switch (currentState) { case EncodingState.StartNew: currentState = EncodingState.Unknown; runLength = 1; colorArray = new List <byte[]>(); colorArray.Add(tmp); break; case EncodingState.Unknown: if (tmp.SequenceEqual(lastPixel)) { currentState = EncodingState.RepeatRun; } else { currentState = EncodingState.ColorRun; } colorArray.Add(tmp); Array.Copy(tmp, 0, lastPixel, 0, 4); runLength++; break; case EncodingState.ColorRun: if (!tmp.SequenceEqual(lastPixel)) { runLength++; } else { colorArray.RemoveAt(colorArray.Count - 1); runLength--; commandLists[m].Add(new Command(currentState, runLength, colorArray)); foreach (var item in colorArray) { colors.AddColor(item); } runLength = 2; colorArray = new List <byte[]>(); currentState = EncodingState.RepeatRun; } Array.Copy(tmp, 0, lastPixel, 0, 4); colorArray.Add(tmp); break; case EncodingState.RepeatRun: if (tmp.SequenceEqual(lastPixel)) { runLength++; Array.Copy(tmp, 0, lastPixel, 0, 4); } else { commandLists[m].Add(new Command(currentState, runLength, colorArray)); foreach (var item in colorArray) { runColors.Add(item); } Array.Copy(tmp, 0, lastPixel, 0, 4); runLength = 1; colorArray = new List <byte[]>(); colorArray.Add(tmp); currentState = EncodingState.Unknown; } break; default: break; } } y++; } x += 16; if (x >= w1) { x = 0; } else { y -= 4; } } commandLists[m].Add(new Command(currentState, runLength, colorArray)); foreach (var item in colorArray) { colors.AddColor(item); } mipPixels = null; imageWidth /= 2; imageHeight /= 2; if (m < 1) { Bitmap mip = new Bitmap(imageWidth, imageHeight); using (Graphics g = Graphics.FromImage(mip)) { g.SmoothingMode = SmoothingMode.Default; g.InterpolationMode = InterpolationMode.High; g.PixelOffsetMode = PixelOffsetMode.HighQuality; g.CompositingQuality = CompositingQuality.HighQuality; g.DrawImage(image, new Rectangle(0, 0, imageWidth, imageHeight)); } image = mip; } else if (m < 3) { image = new Bitmap(image, imageWidth, imageHeight); } else { Bitmap mip = new Bitmap(imageWidth, imageHeight); using (Graphics g = Graphics.FromImage(mip)) { g.SmoothingMode = SmoothingMode.Default; g.InterpolationMode = InterpolationMode.NearestNeighbor; g.PixelOffsetMode = PixelOffsetMode.HighSpeed; g.CompositingQuality = CompositingQuality.HighSpeed; g.DrawImage(image, new Rectangle(0, 0, imageWidth, imageHeight)); } image = mip; } } colors.SortColors(); foreach (var item in runColors) { if (!colors.HasColor(item)) { colors.AddColor(item); } } List <byte[]> tmpPixels = new List <byte[]>(); foreach (var item in colors.colors.OrderBy(x => x.Value.Index).Where(x => x.Value.Index < 0x10000).Select(x => x.Key)) { tmpPixels.Add(item.color); } pixelArray = tmpPixels.ToArray(); mips = new byte[9][]; for (int i = 0; i < 9; i++) { List <byte> tmpCommands = new List <byte>(); foreach (var item in commandLists[i]) { tmpCommands.AddRange(item.ToOps(colors).ToArray()); } mips[i] = tmpCommands.ToArray(); } }
public Command(EncodingState s, uint len, List <byte[]> cols) { state = s; length = len; colors = cols; }