Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
mcychan authored Jan 22, 2025
1 parent e5025b5 commit 1a06258
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
14 changes: 11 additions & 3 deletions src/nQuant/j2se/GilbertCurve.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package nQuant.j2se;
/* Generalized Hilbert ("gilbert") space-filling curve for rectangular domains of arbitrary (non-power of two) sizes.
Copyright (c) 2021 - 2024 Miller Cy Chan
Copyright (c) 2021 - 2025 Miller Cy Chan
* A general rectangle with a known orientation is split into three regions ("up", "right", "down"), for which the function calls itself recursively, until a trivial path can be produced. */

import java.awt.Color;
Expand Down Expand Up @@ -106,13 +106,21 @@ private void ditherPixel(int x, int y){
int a_pix = (int) Math.min(0xFF, Math.max(error.p[3], 0.0));

Color c2 = new Color(r_pix, g_pix, b_pix, a_pix);
if (palette.length <= 32 && a_pix > 0xF0) {
if (saliencies != null && palette.length < 3) {
final int acceptedDiff = 1;
if(CIELABConvertor.Y_Diff(pixel, c2) > acceptedDiff) {
final float strength = 1 / 3f;
c2 = BlueNoise.diffuse(pixel, palette[qPixels[bidx]], 1 / saliencies[bidx], strength, x, y);
}
qPixels[bidx] = ditherable.nearestColorIndex(palette, c2, bidx);
}
else if (palette.length <= 32 && a_pix > 0xF0) {
int offset = ditherable.getColorIndex(c2);
if (lookup[offset] == 0)
lookup[offset] = ditherable.nearestColorIndex(palette, c2, bidx) + 1;
qPixels[bidx] = (short) (lookup[offset] - 1);

int acceptedDiff = Math.max(2, palette.length - margin);
final int acceptedDiff = Math.max(2, palette.length - margin);
if(saliencies != null && (CIELABConvertor.Y_Diff(pixel, c2) > acceptedDiff || CIELABConvertor.U_Diff(pixel, c2) > (2 * acceptedDiff))) {
final float strength = 1 / 3f;
c2 = BlueNoise.diffuse(pixel, palette[qPixels[bidx]], 1 / saliencies[bidx], strength, x, y);
Expand Down
8 changes: 6 additions & 2 deletions src/nQuant/j2se/PQCanvas.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -312,8 +314,10 @@ public void paintComponent(Graphics g) {
g2d.fill(new Rectangle(getSize()));
}

g2d.scale(zoom, zoom);
g2d.drawImage(image, null, 0, 0);
AffineTransform scaleInstance = AffineTransform.getScaleInstance(zoom, zoom);
AffineTransformOp scaleOp = new AffineTransformOp(scaleInstance, AffineTransformOp.TYPE_BILINEAR);

g2d.drawImage(image, scaleOp, 0, 0);
}
else {
g2d.setFont(new Font("Arial", Font.BOLD, 20));
Expand Down
17 changes: 15 additions & 2 deletions src/nQuant/j2se/PnnLABQuantizer.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package nQuant.j2se;

/* Fast pairwise nearest neighbor based algorithm with CIELAB color space advanced version
Copyright (c) 2018-2023 Miller Cy Chan
Copyright (c) 2018-2025 Miller Cy Chan
* error measure; time used is proportional to number of bins squared - WJ */

import java.awt.Color;
Expand Down Expand Up @@ -528,8 +528,21 @@ protected void clear()
protected short[] dither(Color[] palette, int width, int height, boolean dither)
{
Ditherable ditherable = getDitherFn();
if(hasSemiTransparency || isGA)
if(hasSemiTransparency)
weight *= -1;

if(dither && !hasAlpha() && palette.length < 3) {
saliencies = new float[pixels.length];
float saliencyBase = .1f;

/* Build histogram */
for (int i = 0; i < pixels.length; ++i) {
Color c = cPixels[i];
Lab lab1 = getLab(c.getRGB());

saliencies[i] = saliencyBase + (1 - saliencyBase) * lab1.L / 100f;
}
}
short[] qPixels = GilbertCurve.dither(width, height, pixels, palette, ditherable, saliencies, weight);

if(!dither) {
Expand Down

0 comments on commit 1a06258

Please sign in to comment.