public static string SerializeWithoutCompression(LightImage li) { StringBuilder res = new StringBuilder(); res.Append("0"); string formattedWidth = String.Format("{0:D5}", li.width); string formattedHeight = String.Format("{0:D5}", li.height); res.Append(formattedWidth + formattedHeight); for (int i = 0; i < li.height; i++) { for (int j = 0; j < li.width; j++) { int pixel = li.matrix[i, j]; byte a = (byte)((pixel >> 24) % 256); byte r = (byte)((pixel >> 16) % 256); byte g = (byte)((pixel >> 8) % 256); byte b = (byte)((pixel >> 0) % 256); a = shiftByteToAvoidSpecialCharacters(a); r = shiftByteToAvoidSpecialCharacters(r); g = shiftByteToAvoidSpecialCharacters(g); b = shiftByteToAvoidSpecialCharacters(b); res.Append((char)a); res.Append((char)r); res.Append((char)g); res.Append((char)b); } } return(res.ToString()); }
public static string Serialize(LightImage li) { if (BrainDuelsLib.web.SocketManager.ImageCompression.doCompress) { return(CompressColorClasterization(li)); } else { return(SerializeWithoutCompression(li)); } }
public LightImage CropToSize(int dx, int dy) { int needWidth = dx; int needHeight = dy; double currentK = (double)(width) / (double)(height); double needK = (double)(needWidth) / (double)needHeight; int[,] resMatrix = new int[needHeight, needWidth]; bool rot = needK >= currentK; if (rot) { double k = (double)needHeight / (double)height; LightImage resized = this.Resize(k); int left = (needWidth - resized.width) / 2; int right = needWidth - left; for (int i = 0; i < needHeight; i++) { for (int j = 0; j < needWidth; j++) { int pixel = 0; if (j > left && j < right && j - left >= 0 && j - left < resized.width && i < resized.height) { pixel = resized.matrix[i, j - left]; } resMatrix[i, j] = pixel; } } } else { double k = (double)needWidth / (double)width; LightImage resized = this.Resize(k); int top = (needHeight - resized.height) / 2; int bottom = needHeight - top; for (int i = 0; i < needHeight; i++) { for (int j = 0; j < needWidth; j++) { int pixel = 0; if (i > top && i < bottom && i - top >= 0 && i - top < resized.height && j < resized.width) { pixel = resized.matrix[i - top, j]; } resMatrix[i, j] = pixel; } } } return(new LightImage(resMatrix, needWidth, needHeight)); }
public static string CompressColorClasterization(LightImage li) { Clusterizer clusterizer = new Clusterizer(); for (int i = 0; i < li.height; i++) { for (int j = 0; j < li.width; j++) { int pixel = li.matrix[i, j]; byte a = (byte)((pixel >> 24) % 256); byte r = (byte)((pixel >> 16) % 256); byte g = (byte)((pixel >> 8) % 256); byte b = (byte)((pixel >> 0) % 256); double x = (double)j / (double)(li.width); double y = (double)i / (double)(li.height); clusterizer.AddPoint(a, r, g, b); } } KeyValuePair <List <int>, int[, ]> clus = clusterizer.Clusterize(BrainDuelsLib.web.SocketManager.ImageCompression.clustersCount, li.width, li.height); List <int> colors = clus.Key; int[,] marks = clus.Value; StringBuilder res = new StringBuilder(); res.Append("1"); string formattedWidth = String.Format("{0:D5}", li.width); string formattedHeight = String.Format("{0:D5}", li.height); res.Append(formattedWidth + formattedHeight); for (int i = 0; i < colors.Count; i++) { int color = colors[i]; byte a = (byte)((color >> 24) % 256); byte r = (byte)((color >> 16) % 256); byte g = (byte)((color >> 8) % 256); byte b = (byte)((color >> 0) % 256); a = shiftByteToAvoidSpecialCharacters(a); r = shiftByteToAvoidSpecialCharacters(r); g = shiftByteToAvoidSpecialCharacters(g); b = shiftByteToAvoidSpecialCharacters(b); res.Append((char)a); res.Append((char)r); res.Append((char)g); res.Append((char)b); } for (int i = 0; i < li.height; i++) { for (int j = 0; j < li.width; j++) { byte mark = (byte)marks[i, j]; mark = shiftByteToAvoidSpecialCharacters(mark); res.Append((char)mark); } } return(res.ToString()); }