/// <summary> /// ColorConverts the source BufferedImage. /// If the destination image is null, /// a BufferedImage will be created with an appropriate ColorModel. </summary> /// <param name="src"> the source <code>BufferedImage</code> to be converted </param> /// <param name="dest"> the destination <code>BufferedImage</code>, /// or <code>null</code> </param> /// <returns> <code>dest</code> color converted from <code>src</code> /// or a new, converted <code>BufferedImage</code> /// if <code>dest</code> is <code>null</code> </returns> /// <exception cref="IllegalArgumentException"> if dest is null and this op was /// constructed using the constructor which takes only a /// RenderingHints argument, since the operation is ill defined. </exception> public BufferedImage Filter(BufferedImage src, BufferedImage dest) { ColorSpace srcColorSpace, destColorSpace; BufferedImage savdest = null; if (src.ColorModel is IndexColorModel) { IndexColorModel icm = (IndexColorModel)src.ColorModel; src = icm.ConvertToIntDiscrete(src.Raster, true); } srcColorSpace = src.ColorModel.ColorSpace; if (dest != null) { if (dest.ColorModel is IndexColorModel) { savdest = dest; dest = null; destColorSpace = null; } else { destColorSpace = dest.ColorModel.ColorSpace; } } else { destColorSpace = null; } if ((CSList != null) || (!(srcColorSpace is ICC_ColorSpace)) || ((dest != null) && (!(destColorSpace is ICC_ColorSpace)))) { /* non-ICC case */ dest = NonICCBIFilter(src, srcColorSpace, dest, destColorSpace); } else { dest = ICCBIFilter(src, srcColorSpace, dest, destColorSpace); } if (savdest != null) { Graphics2D big = savdest.CreateGraphics(); try { big.DrawImage(dest, 0, 0, null); } finally { big.Dispose(); } return(savdest); } else { return(dest); } }
void DestroyContext() { if (!IsContextValid) { return; } if (graphics2DContext != null) { graphics2DContext.Dispose(); } }
protected override void PaintComponent(Graphics g) { base.PaintComponent(g); // Dimensions Graphics2D g2d = (Graphics2D)g.Create(); g.SetFont(new Font("Arial", Font.Plain, 10)); int width = this.GetWidth(); int height = this.GetHeight(); int cellWidth = width / this.columnCount; int cellHeight = height / this.rowCount; int xOffset = (width - (this.columnCount * cellWidth)) / 2; int yOffset = (height - (this.rowCount * cellHeight)) / 2; // Get label index IList <U> labels = this._enclosing._enclosing.UniqueLabels().Stream().Collect(Collectors.ToList()); // Get color gradient int maxDiag = 0; int maxOffdiag = 0; foreach (KeyValuePair <Pair <U, U>, int> entry in this._enclosing._enclosing.confTable) { if (entry.Key.first == entry.Key.second) { maxDiag = Math.Max(maxDiag, entry.Value); } else { maxOffdiag = Math.Max(maxOffdiag, entry.Value); } } // Render the grid float[] hsb = new float[3]; for (int row = 0; row < this.rowCount; row++) { for (int col = 0; col < this.columnCount; col++) { // Position int x = xOffset + (col * cellWidth); int y = yOffset + (row * cellHeight); float xCenter = xOffset + (col * cellWidth) + cellWidth / 3.0f; float yCenter = yOffset + (row * cellHeight) + cellHeight / 2.0f; // Get text + Color string text; Color bg = Color.White; if (row == 0 && col == 0) { text = "V guess | gold >"; } else { if (row == 0) { text = labels[col - 1].ToString(); } else { if (col == 0) { text = labels[row - 1].ToString(); } else { // Set value int count = this._enclosing._enclosing.confTable[Pair.MakePair(labels[row - 1], labels[col - 1])]; if (count == null) { count = 0; } text = string.Empty + count; // Get color if (row == col) { double percentGood = ((double)count) / ((double)maxDiag); hsb = Color.RGBtoHSB((int)(255 - (255.0 * percentGood)), (int)(255 - (255.0 * percentGood / 2.0)), (int)(255 - (255.0 * percentGood)), hsb); bg = Color.GetHSBColor(hsb[0], hsb[1], hsb[2]); } else { double percentBad = ((double)count) / ((double)maxOffdiag); hsb = Color.RGBtoHSB((int)(255 - (255.0 * percentBad / 2.0)), (int)(255 - (255.0 * percentBad)), (int)(255 - (255.0 * percentBad)), hsb); bg = Color.GetHSBColor(hsb[0], hsb[1], hsb[2]); } } } } // Draw Rectangle cell = new Rectangle(x, y, cellWidth, cellHeight); g2d.SetColor(bg); g2d.Fill(cell); g2d.SetColor(Color.Black); g2d.DrawString(text, xCenter, yCenter); this.cells.Add(cell); } } // Mouse over if (this.selectedCell != null && this.selectedCell.x > 0 && this.selectedCell.y > 0) { int index = this.selectedCell.x + (this.selectedCell.y * this.columnCount); Rectangle cell = this.cells[index]; this.OnMouseOver(g2d, cell, labels[this.selectedCell.y - 1], labels[this.selectedCell.x - 1]); } // Clean up g2d.Dispose(); }
private BufferedImage ICCBIFilter(BufferedImage src, ColorSpace srcColorSpace, BufferedImage dest, ColorSpace destColorSpace) { int nProfiles = ProfileList.Length; ICC_Profile srcProfile = null, destProfile = null; srcProfile = ((ICC_ColorSpace)srcColorSpace).Profile; if (dest == null) { /* last profile in the list defines * the output color space */ if (nProfiles == 0) { throw new IllegalArgumentException("Destination ColorSpace is undefined"); } destProfile = ProfileList [nProfiles - 1]; dest = CreateCompatibleDestImage(src, null); } else { if (src.Height != dest.Height || src.Width != dest.Width) { throw new IllegalArgumentException("Width or height of BufferedImages do not match"); } destProfile = ((ICC_ColorSpace)destColorSpace).Profile; } /* Checking if all profiles in the transform sequence are the same. * If so, performing just copying the data. */ if (srcProfile == destProfile) { bool noTrans = true; for (int i = 0; i < nProfiles; i++) { if (srcProfile != ProfileList[i]) { noTrans = false; break; } } if (noTrans) { Graphics2D g = dest.CreateGraphics(); try { g.DrawImage(src, 0, 0, null); } finally { g.Dispose(); } return(dest); } } /* make a new transform if needed */ if ((ThisTransform == null) || (ThisSrcProfile != srcProfile) || (ThisDestProfile != destProfile)) { UpdateBITransform(srcProfile, destProfile); } /* color convert the image */ ThisTransform.colorConvert(src, dest); return(dest); }