protected override void EncodeBlock(ARGBPixel* sPtr, VoidPtr blockAddr, int width) { wRGB5A3Pixel* dPtr = (wRGB5A3Pixel*)blockAddr; for (int y = 0; y < BlockHeight; y++, sPtr += width) for (int x = 0; x < BlockWidth; ) *dPtr++ = (wRGB5A3Pixel)sPtr[x++]; }
protected override void DecodeBlock(VoidPtr blockAddr, ARGBPixel* dPtr, int width) { byte* sPtr = (byte*)blockAddr; byte b; if (_workingPalette != null) { for (int y = 0; y < BlockHeight; y++, dPtr += width) for (int x = 0; x < BlockWidth; ) { b = *sPtr++; dPtr[x++] = (ARGBPixel)_workingPalette.Entries[b >> 4]; dPtr[x++] = (ARGBPixel)_workingPalette.Entries[b & 0xF]; } } else { for (int y = 0; y < BlockHeight; y++, dPtr += width) for (int x = 0; x < BlockWidth; ) { b = *sPtr++; dPtr[x++] = new ARGBPixel((byte)(b & 0xF0)); dPtr[x++] = new ARGBPixel((byte)(b << 4)); } } }
protected override void DecodeBlock(VoidPtr blockAddr, ARGBPixel* dPtr, int width) { wRGB5A3Pixel* sPtr = (wRGB5A3Pixel*)blockAddr; //ARGBPixel* dPtr = (ARGBPixel*)destAddr; for (int y = 0; y < BlockHeight; y++, dPtr += width) for (int x = 0; x < BlockWidth; ) dPtr[x++] = (ARGBPixel)(*sPtr++); }
protected override void EncodeBlock(ARGBPixel* sPtr, VoidPtr blockAddr, int width) { byte* stPtr = (byte*)sPtr; byte* dPtr = (byte*)blockAddr; for (int y = 0; y < BlockHeight; y++, stPtr += width) for (int x = 0; x < BlockWidth; ) *dPtr++ = stPtr[x++]; }
private static void extractColorBlockRGB(ARGBPixel *colors, Vector3 *points) { for (int i = 0; i < 16; i++) { ARGBPixel p = colors[i]; points[i] = new Vector3(p.R, p.G, p.B); } }
protected override void EncodeBlock(ARGBPixel* sPtr, VoidPtr blockAddr, int width) { byte* stPtr = (byte*)sPtr; byte* dPtr = (byte*)blockAddr; for (int y = 0; y < BlockHeight; y++, stPtr += width / 2) for (int x = 0; x < BlockWidth / 2; ) *dPtr++ = stPtr[x++]; //*dPtr++ = (byte)((_workingPalette.FindMatch(sPtr[x++]) << 4) | (_workingPalette.FindMatch(sPtr[x++]) & 0x0F)); }
protected override void DecodeBlock(VoidPtr blockAddr, ARGBPixel* dPtr, int width) { I4Pixel* sPtr = (I4Pixel*)blockAddr; //RGBPixel* dPtr = (RGBPixel*)destAddr; for (int y = 0; y < BlockHeight; y++, dPtr += width) for (int x = 0; x < BlockWidth; ) { dPtr[x++] = (ARGBPixel)(*sPtr)[0]; dPtr[x++] = (ARGBPixel)(*sPtr++)[1]; } }
public int DistanceTo(ARGBPixel p) { int val = A - p.A; int dist = val * val; val = R - p.R; dist += val * val; val = G - p.G; dist += val * val; val = B - p.B; return(dist + val); }
public ColorInformation(ARGBPixel[] colors) { _colors = colors; _alphaColors = 0; _isGreyscale = true; foreach (ARGBPixel p in colors) { if (p.A != 0xFF) _alphaColors++; if ((_isGreyscale) && (!p.IsGreyscale())) _isGreyscale = false; } }
public static CMPRBlock optimalCompressDXT1a(ARGBPixel rgba) { if (rgba.A < 128) { CMPRBlock block = new CMPRBlock(); block._root0._data = 0; block._root1._data = 0; block._lookup = 0xFFFFFFFF; return(block); } return(optimalCompressDXT1(rgba)); }
private static int extractColorBlockRGBA(ARGBPixel *colors, Vector3 *points) { int num = 0; for (int i = 0; i < 16; i++) { ARGBPixel p = colors[i]; if (p.A > 127) { points[num++] = new Vector3(p.R, p.G, p.B); } } return(num); }
public static int FindMatch(this ColorPalette pal, ARGBPixel pixel) { int bestDist = Int32.MaxValue, bestIndex = 0; for (int i = 0, c = pal.Entries.Length; i < c; i++) { int dist = pixel.DistanceTo(pal.Entries[i]); if (dist < bestDist) { bestIndex = i; if (dist == 0) break; bestDist = dist; } } return bestIndex; }
private static CMPRBlock optimalCompressDXT1(ARGBPixel c) { CMPRBlock block = new CMPRBlock(); uint indices = 0xAAAAAAAA; block._root0._data = (ushort)((OMatch5[c.R, 0] << 11) | (OMatch6[c.G, 0] << 5) | OMatch5[c.B, 0]); block._root1._data = (ushort)((OMatch5[c.R, 1] << 11) | (OMatch6[c.G, 1] << 5) | OMatch5[c.B, 1]); if (block._root0 < block._root1) { VoidPtr.Swap((short *)&block._root0, (short *)&block._root1); indices ^= 0x55555555; } block._lookup = indices; return(block); }
private static uint computeIndices3(ARGBPixel *colors, Vector3 *maxColor, Vector3 *minColor) { float * pData = stackalloc float[9]; Vector3 *palette = (Vector3 *)pData; palette[0] = *minColor; palette[1] = *maxColor; palette[2] = (palette[0] + palette[1]) * 0.5f; uint indices = 0; for (int i = 0; i < 16; i++) { ARGBPixel p = colors[i]; Vector3 color = new Vector3(p.R, p.G, p.B); float d0 = colorDistance(&palette[0], &color); float d1 = colorDistance(&palette[1], &color); float d2 = colorDistance(&palette[2], &color); uint index; if (p.A < 128) { index = 3; } else if (d0 < d1 && d0 < d2) { index = 0; } else if (d1 < d2) { index = 1; } else { index = 2; } indices <<= 2; indices |= index; } return(indices); }
protected override void DecodeBlock(VoidPtr blockAddr, ARGBPixel* dPtr, int width) { byte* s1 = (byte*)blockAddr; byte* s2 = s1 + 32; byte* d2 = (byte*)dPtr; for (int y = 0; y < 4; y++, d2 += (width - 4) << 2) { for (int x = 0; x < 4; x++, d2 += 4) { d2[3] = *s1++; d2[2] = *s1++; d2[1] = *s2++; d2[0] = *s2++; } } //RGBA8Pixel* sPtr = (RGBA8Pixel*)blockAddr; ////ARGBPixel* dPtr = (ARGBPixel*)destAddr; //for (int y = 0; y < 4; y++, dPtr += width) // for (int x = 0; x < 4; sPtr = (RGBA8Pixel*)((int)sPtr + 2) ) // dPtr[x++] = (ARGBPixel)(*sPtr); }
protected override void DecodeBlock(VoidPtr blockAddr, ARGBPixel* dPtr, int width) { byte* sPtr = (byte*)blockAddr; if (_workingPalette != null) { for (int y = 0; y < BlockHeight; y++, dPtr += width) for (int x = 0; x < BlockWidth; x++) { byte b = *sPtr++; if (b >= _workingPalette.Entries.Length) dPtr[x] = new ARGBPixel(); else dPtr[x] = (ARGBPixel)_workingPalette.Entries[b]; } } else { for (int y = 0; y < BlockHeight; y++, dPtr += width) for (int x = 0; x < BlockWidth; x++) dPtr[x] = new ARGBPixel(*sPtr++); } }
public void MakeSolid(ARGBPixel color) { _numEntries = 0; _constant = true; _solidColor = color; SignalPropertyChange(); }
public void SetColor(int index, int id, ARGBPixel color) { if (Palette != null) { Palette.Entries[index] = (Color)color; SignalPropertyChange(); } }
private static void RGB5A3Converter(ushort *id, out ARGBPixel outPixel) { outPixel = (ARGBPixel)(*(wRGB5A3Pixel *)id); }
private static void RGB5A3Handler(ARGBPixel inPixel, ushort *id) { *(wRGB5A3Pixel *)id = (wRGB5A3Pixel)inPixel; }
public ARGBPixel Min(ARGBPixel p) { return new ARGBPixel(Math.Min(A, p.A), Math.Min(R, p.R), Math.Min(G, p.G), Math.Min(B, p.B)); }
public int DistanceTo(ARGBPixel p) { int val = A - p.A; int dist = val * val; val = R - p.R; dist += val * val; val = G - p.G; dist += val * val; val = B - p.B; return dist + val; }
private void SourceChanged() { lstColors.BeginUpdate(); lstColors.Items.Clear(); if (_colorSource != null) { int count = _colorSource.ColorCount(_colorId); for (int i = 0; i < count; i++) lstColors.Items.Add(_colorSource.GetColor(i, _colorId)); if (pnlPrimary.Visible = _colorSource.HasPrimary(_colorId)) { _primaryColor = _colorSource.GetPrimaryColor(_colorId); lblPrimary.Text = _colorSource.PrimaryColorName(_colorId); UpdateBase(); } } lstColors.EndUpdate(); }
private void lblBase_Click(object sender, EventArgs e) { if (_colorSource == null) return; _dlgColor.Color = (Color)_primaryColor; if (_dlgColor.ShowDialog(this) == DialogResult.OK) { _primaryColor = (ARGBPixel)_dlgColor.Color; _colorSource.SetPrimaryColor(_colorId, _primaryColor); UpdateBase(); } }
public void Decode(ARGBPixel* block, int width) { uint* pixelData = stackalloc uint[4]; ARGBPixel* pixel = (ARGBPixel*)pixelData; pixel[0] = (ARGBPixel)_root0; pixel[1] = (ARGBPixel)_root1; if (_root0._data > _root1._data) { pixel[2] = new ARGBPixel(255, (byte)(((pixel[0].R << 1) + pixel[1].R) / 3), (byte)(((pixel[0].G << 1) + pixel[1].G) / 3), (byte)(((pixel[0].B << 1) + pixel[1].B) / 3)); pixel[3] = new ARGBPixel(255, (byte)(((pixel[1].R << 1) + pixel[0].R) / 3), (byte)(((pixel[1].G << 1) + pixel[0].G) / 3), (byte)(((pixel[1].B << 1) + pixel[0].B) / 3)); } else { pixel[2] = new ARGBPixel(255, (byte)((pixel[0].R + pixel[1].R) >> 1), (byte)((pixel[0].G + pixel[1].G) >> 1), (byte)((pixel[0].B + pixel[1].B) >> 1)); pixel[3] = new ARGBPixel(); } uint lookup = _lookup; for (int y = 0, shift = 30; y < 4; y++, block += width) for (int x = 0; x < 4; shift -= 2) block[x++] = pixel[(lookup >> shift) & 0x03]; }
public void SetPrimaryColor(int id, ARGBPixel color) { }
protected override void EncodeBlock(ARGBPixel* sPtr, VoidPtr blockAddr, int width) { RGBA8Pixel* dPtr = (RGBA8Pixel*)blockAddr; for (int y = 0; y < BlockHeight; y++, sPtr += width) for (int x = 0; x < BlockWidth; dPtr = dPtr->Increase()) dPtr->Set(&sPtr[x++]); }
public void Set(ARGBPixel* p) { A = p->A; R = p->R; G = p->G; B = p->B; }
private void lstColors_DoubleClick(object sender, EventArgs e) { ListBox.SelectedIndexCollection indices = lstColors.SelectedIndices; if ((_colorSource == null) || (indices.Count <= 0)) return; int count = indices.Count; if (count == 1) { int index = indices[0]; _dlgColor.Color = (Color)(ARGBPixel)lstColors.Items[index]; if (_dlgColor.ShowDialog(this) == DialogResult.OK) { ARGBPixel p = (ARGBPixel)_dlgColor.Color; lstColors.Items[index] = p; _colorSource.SetColor(index, _colorId, p); } } else { //Sort indices int[] sorted = new int[count]; indices.CopyTo(sorted, 0); Array.Sort(sorted); _dlgGradient.StartColor = (Color)(ARGBPixel)lstColors.Items[sorted[0]]; _dlgGradient.EndColor = (Color)(ARGBPixel)lstColors.Items[sorted[count - 1]]; if (_dlgGradient.ShowDialog(this) == DialogResult.OK) { //Interpolate and apply to each in succession. ARGBPixel start = (ARGBPixel)_dlgGradient.StartColor; ARGBPixel end = (ARGBPixel)_dlgGradient.EndColor; float stepA = (end.A - start.A) / (float)count; float stepR = (end.R - start.R) / (float)count; float stepG = (end.G - start.G) / (float)count; float stepB = (end.B - start.B) / (float)count; for (int i = 0; i < count; i++) { ARGBPixel p = new ARGBPixel( (byte)(start.A + (i * stepA)), (byte)(start.R + (i * stepR)), (byte)(start.G + (i * stepG)), (byte)(start.B + (i * stepB))); lstColors.Items[sorted[i]] = p; _colorSource.SetColor(sorted[i], _colorId, p); } } } }
public void Decode(ARGBPixel* image, int imgX, int imgY, int imgW, int imgH) { Decode(image + (imgX + (imgY * imgW)), imgW); }
private void copyToolStripMenuItem_Click(object sender, EventArgs e) { if (lstColors.SelectedIndex >= 0) _copyColor = (ARGBPixel)lstColors.SelectedItem; }
public void SetColor(int index, int id, ARGBPixel color) { if (_numEntries == 0) _solidColor = color; else _colors[index] = color; SignalPropertyChange(); }
public ARGBPixel Max(ARGBPixel p) { return new ARGBPixel(Math.Max(A, p.A), Math.Max(R, p.R), Math.Max(G, p.G), Math.Max(B, p.B)); }
public void SetPrimaryColor(int id, ARGBPixel color) { _colorMask = color; SignalPropertyChange(); }
private static void IA8Handler(ARGBPixel inPixel, ushort *id) { *(IA8Pixel *)id = (IA8Pixel)inPixel; }
public ARGBPixel Max(ARGBPixel p) { return(new ARGBPixel(Math.Max(A, p.A), Math.Max(R, p.R), Math.Max(G, p.G), Math.Max(B, p.B))); }
private static void IA8Converter(ushort *id, out ARGBPixel outPixel) { outPixel = (ARGBPixel)(*(IA8Pixel *)id); }
//private List<CMPBlock> _blockCache = new List<CMPBlock>(); //private int _blockIndex; protected override void DecodeBlock(VoidPtr blockAddr, ARGBPixel* dPtr, int width) { CMPRBlock* sPtr = (CMPRBlock*)blockAddr; //ARGBPixel* dPtr = (ARGBPixel*)destAddr; //int index = 0; for (int y = 0; y < 8; y += 4) for (int x = 0; x < 8; x += 4, sPtr++) sPtr->Decode(&dPtr[(y * width) + x], width); //DXT1.DecodeBlock(&sPtr[index++], &dPtr[(y * width) + x], width); }
public ARGBPixel Min(ARGBPixel p) { return(new ARGBPixel(Math.Min(A, p.A), Math.Min(R, p.R), Math.Min(G, p.G), Math.Min(B, p.B))); }
protected override void EncodeBlock(ARGBPixel* sPtr, VoidPtr blockAddr, int width) { CMPRBlock* dPtr = (CMPRBlock*)blockAddr; for (int y = 0; y < 2; y++, sPtr += (width * 4)) for (int x = 0; x < 8; x += 4) *dPtr++ = CMPRBlock.Encode(&sPtr[x], width, false); }
public override bool OnInitialize() { _colorMask = (ARGBPixel)Header->_colorMask; _colors.Clear(); if (_constant) { _numEntries = 0; _solidColor = (ARGBPixel)Header->SolidColor; } else { _numEntries = ((CLR0Node)Parent.Parent)._numFrames; ABGRPixel* data = Header->Data; for (int i = 0; i < _numEntries; i++) _colors.Add((ARGBPixel)(*data++)); } _name = _target.ToString(); return false; }
public static CMPRBlock Encode(ARGBPixel* block, int width, bool fast) { CMPRBlock p = new CMPRBlock(); uint* pData = stackalloc uint[16]; ARGBPixel* pColor = (ARGBPixel*)pData; bool isSingle = true, hasAlpha = false, allAlpha = true; for (int y = 0, i = 0; y < 4; y++, block += width) { for (int x = 0; x < 4; i++) { pColor[i] = block[x++]; if (pData[i] != pData[0]) isSingle = false; if (pColor[i].A < 0x80) hasAlpha = true; else allAlpha = false; } } /* * Foreach block: * copy block to buffer * mirror remaning colors? * * If block is single color: * run optiml compress? * else * Initialize color set * Compress block using color set * * */ //BlockDecoder decoder = new BlockDecoder(width, 4, 4, 4); //bool isSingle = true, hasAlpha = false, allAlpha = true; //for (int i = 0; i < 16; i++) //{ // int index = decoder[i]; // pColor[i] = block[index]; // if (pData[i] != pData[0]) isSingle = false; // if (pColor[i].A < 0x80) hasAlpha = true; // else allAlpha = false; //} //bool isSingle = true, hasAlpha = false, allAlpha = true; //ARGBPixel* ptr = block; //int index = 0; //for (int y = 0; y < 4; y++, ptr += width - 4) //{ // for (int x = 0; x < 4; x++, ptr++, index++) // { // pColor[index] = block[(y * width) + x]; // if (pData[0] != pData[index]) isSingle = false; // if (pColor[index].A < 0x80) hasAlpha = true; // else allAlpha = false; // } //} //if (allAlpha) //{ // p._root0._data = 0; // p._root1._data = 0xFFFF; // p._lookup = 0xFFFFFFFF; //} //else if (isSingle) //{ // p._root0 = (RGB565Pixel)(*block); // p._root1._data = 0; // p._lookup = 0x00000000; //} //else //{ uint* palData = stackalloc uint[4]; ARGBPixel* palCol = (ARGBPixel*)palData; int bestDist = -1; for (int i = 0; i < 16; i++) { ARGBPixel p1 = pColor[i]; for (int x = i + 1; x < 16; x++) { ARGBPixel p2 = pColor[x]; int d = p1.DistanceTo(p2); if (d > bestDist) { bestDist = d; palCol[2] = p1; palCol[3] = p2; } } } wRGB565Pixel smax = (wRGB565Pixel)palCol[2]; wRGB565Pixel smin = (wRGB565Pixel)palCol[3]; if (smax < smin) { smax = (wRGB565Pixel)palCol[3]; smin = (wRGB565Pixel)palCol[2]; } if (hasAlpha) { p._root0 = smin; p._root1 = smax; palCol[0] = (ARGBPixel)smin; palCol[1] = (ARGBPixel)smax; palCol[2] = new ARGBPixel(255, (byte)((palCol[0].R + palCol[1].R) >> 1), (byte)((palCol[0].G + palCol[1].G) >> 1), (byte)((palCol[0].B + palCol[1].B) >> 1)); palCol[3] = new ARGBPixel(); } else { p._root0 = smax; p._root1 = smin; palCol[0] = (ARGBPixel)smax; palCol[1] = (ARGBPixel)smin; palCol[2] = new ARGBPixel(255, (byte)(((palCol[0].R << 1) + palCol[1].R) / 3), (byte)(((palCol[0].G << 1) + palCol[1].G) / 3), (byte)(((palCol[0].B << 1) + palCol[1].B) / 3)); palCol[3] = new ARGBPixel(255, (byte)(((palCol[1].R << 1) + palCol[0].R) / 3), (byte)(((palCol[1].G << 1) + palCol[0].G) / 3), (byte)(((palCol[1].B << 1) + palCol[0].B) / 3)); } uint indicies = 0; for (int i = 0, shift = 30; i < 16; i++, shift -= 2) { uint index = 3; if (pColor[i].A >= 0x80) { int bd = int.MaxValue; for (int x = 0; x < ((hasAlpha) ? 4 : 3); x++) { int dist = palCol[x].DistanceTo(pColor[i]); if (dist < bd) { bd = dist; index = (uint)x; } } } indicies |= index << shift; } p._lookup = indicies; //p = DXT1Fast.Compress(pColor); //} return p; }