I've been trying to implement a BC1 (DXT1) decompression algorithm in Java. Everything seems to work pretty precise but I've ran into problem with some blocks around transparent ones. I've been trying to resolve it for a few hours without success.
In short, after decompressing all blocks everything looks good except for the blocks whose are around transparent ones. During the development I've been checking results with results from DirectXTex (texconv) which is written in C++.
This is my result compared to DirectXTex one:
Here is the code I'm using:
BufferedImage decompress(byte[] buffer, int width, int height)
and implementation:
BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
int[] scanline = new int[4 * width]; //stores 4 horizontal lines (width/4 blocks)
RGBA[] blockPalette = new RGBA[4]; //stores RGBA values of current block
int bufferOffset = 0;
for (int row = 0; row < height / 4; row++) {
for (int col = 0; col < width / 4; col++) {
short rgb0 = Short.reverseBytes(Bytes.getShort(buffer, bufferOffset));
short rgb1 = Short.reverseBytes(Bytes.getShort(buffer, bufferOffset + 2));
int bitmap = Integer.reverseBytes(Bytes.getInt(buffer, bufferOffset + 4));
bufferOffset += 8;
blockPalette[0] = R5G6B5.decode(rgb0);
blockPalette[1] = R5G6B5.decode(rgb1);
if(rgb0 <= rgb1) {
int c2r = (blockPalette[0].getRed() + blockPalette[1].getRed()) / 2;
int c2g = (blockPalette[0].getGreen() + blockPalette[1].getGreen()) / 2;
int c2b = (blockPalette[0].getBlue() + blockPalette[1].getBlue()) / 2;
blockPalette[2] = new RGBA(c2r, c2g, c2b, 255);
blockPalette[3] = new RGBA(0, 0, 0, 0);
} else {
int c2r = (2 * blockPalette[0].getRed() + blockPalette[1].getRed()) / 3;
int c2g = (2 * blockPalette[0].getGreen() + blockPalette[1].getGreen()) / 3;
int c2b = (2 * blockPalette[0].getBlue() + blockPalette[1].getBlue()) / 3;
int c3r = (blockPalette[0].getRed() + 2 * blockPalette[1].getRed()) / 3;
int c3g = (blockPalette[0].getGreen() + 2 * blockPalette[1].getGreen()) / 3;
int c3b = (blockPalette[0].getBlue() + 2 * blockPalette[1].getBlue()) / 3;
blockPalette[2] = new RGBA(c2r, c2g, c2b, 255);
blockPalette[3] = new RGBA(c3r, c3g, c3b, 255);
}
for (int i = 0; i < 16; i++, bitmap >>= 2) {
int pi = (i / 4) * width + (col * 4 + i % 4);
int index = bitmap & 3;
scanline[pi] = A8R8G8B8.encode(blockPalette[index]);
}
}
//copy scanline to buffered image
result.setRGB(0, row * 4, width, 4, scanline, 0, width);
}
return result;
Does anyone have idea where is the problem? I've been doing exactly the same steps as specification says: Block Compression (Direct3D 10)
Is it that blockPalette[2].set(c2r, c2g, c2b); should be blockPalette[2].set(c2r, c2g, c2b, 255);? (in two locations)
For those who are interested, I've found that the problem was in comparing short values.
I've just changed:
if(rgb0 <= rgb1) {
to either
if(Short.compareUnsigned(rgb0, rgb1) <= 0) {
or
if((rgb0 & 0xffff) <= (rgb1 & 0xffff)) {
and this ensures that color values are compared as unsigned shorts (positive integers).
Related
The LWJGL3 library contains bindings to STB TrueType and other libraries made by Sean Barrett.
In order to modify the packing API provided by this library to render SDF glyphs into the backing texture instead of normal bitmaps, I am reproducing the texture-rendering code from the library in java.
I managed to get it to almost work but I am hitting a stumbling stone where I am getting mangled garbage data for the very top-left corner of the texture. I am somewhat confident that the error must be located somewhere in the code for my version of the stbtt__h_prefilter(...), as this is where the assertion fails.
Edit: I forgot to take into consideration the current buffer position when doing read/write operations on the buffer. Now I still have some garbage data in the bitmap, but it's more evenly distributed.
In fact looking at the updated second picture it seems that somehow the very left-most part of every glyph is shifted half the glyph height down. I cannot find out where or why it happens, especially considering that the bitmap processing works on each glyph individually after it is rendered into the font, so to my understanding the next line of glyphs should just overwrite this..?
Bitmap generated by the original library:
Bitmap generated by my version (see the offset half-lines cutting into some letters):
Addendum: Bitmap generated by my version without the prefilter_... methods:
Below you find my versions of the methods from the library. The originals can be found here.
The references to STB... functions refer to the generated bindings form lwjgl3.
private static boolean packFontRangesRenderIntoRectsSDF(
STBTTPackContext context, STBTTFontinfo fontinfo,
STBTTPackRange.Buffer ranges, STBRPRect.Buffer rects) {
int i, j, k;
boolean returnValue = true;
int curr_hOversample = context.h_oversample();
int curr_vOversample = context.v_oversample();
k = 0;
for(i = 0 ; i < ranges.remaining() ; i++) {
float fh = ranges.get(i).font_size();
float scale = fh > 0.0f ? stbtt_ScaleForPixelHeight(fontinfo, fh) : stbtt_ScaleForMappingEmToPixels(fontinfo, -fh);
float recip_h, recip_v, sub_x, sub_y;
curr_hOversample = STBTTPackRange.nh_oversample(ranges.get(i).address()) & 0xFF;
curr_vOversample = STBTTPackRange.nv_oversample(ranges.get(i).address()) & 0xFF;
recip_h = 1.0f / (float)curr_hOversample;
recip_v = 1.0f / (float)curr_vOversample;
sub_x = __oversample_shift(curr_hOversample);
sub_y = __oversample_shift(curr_vOversample);
for(j = 0 ; j < ranges.get(i).num_chars() ; j++) {
STBRPRect r = rects.get(k);
if(r.was_packed()) {
STBTTPackedchar bc = ranges.get(i).chardata_for_range().get(j);
IntBuffer advance = ByteBuffer.allocateDirect(Integer.BYTES)
.order(ByteOrder.nativeOrder())
.asIntBuffer();
IntBuffer lsb = ByteBuffer.allocateDirect(Integer.BYTES)
.order(ByteOrder.nativeOrder())
.asIntBuffer();
IntBuffer x0 = ByteBuffer.allocateDirect(Integer.BYTES)
.order(ByteOrder.nativeOrder())
.asIntBuffer();
IntBuffer x1 = ByteBuffer.allocateDirect(Integer.BYTES)
.order(ByteOrder.nativeOrder())
.asIntBuffer();
IntBuffer y0 = ByteBuffer.allocateDirect(Integer.BYTES)
.order(ByteOrder.nativeOrder())
.asIntBuffer();
IntBuffer y1 = ByteBuffer.allocateDirect(Integer.BYTES)
.order(ByteOrder.nativeOrder())
.asIntBuffer();
int codepoint = ranges.get(i).array_of_unicode_codepoints() == null ? ranges.get(i).first_unicode_codepoint_in_range() + j : ranges.get(i).array_of_unicode_codepoints().get(j);
int glyph = stbtt_FindGlyphIndex(fontinfo, codepoint);
int pad = context.padding();
r.x((short) (r.x() + pad));
r.y((short) (r.y() + pad));
r.w((short) (r.w() - pad));
r.h((short) (r.h() - pad));
stbtt_GetGlyphHMetrics(fontinfo, glyph, advance, lsb);
stbtt_GetGlyphBitmapBox(fontinfo, glyph,
scale * curr_hOversample,
scale * curr_vOversample,
x0, y0, x1, y1);
//TODO replace below with SDF func
ByteBuffer buff = context.pixels(context.height() * context.width());
buff.position(r.x() + r.y() * context.stride_in_bytes());
stbtt_MakeGlyphBitmapSubpixel(fontinfo, buff,
r.w() - curr_hOversample + 1,
r.h() - curr_vOversample + 1,
context.stride_in_bytes(),
scale * curr_hOversample,
scale * curr_vOversample,
0, 0,
glyph);
if(curr_hOversample > 1) {
//FIXME __h_prefilter(..) function
buff.position(r.x() + r.y() * context.stride_in_bytes());
__h_prefilter(buff,
r.w(), r.h(), context.stride_in_bytes(),
curr_hOversample);
}
if(curr_vOversample > 1) {
//FIXME __v_prefilter(..) function
buff.position(r.x() + r.y() * context.stride_in_bytes());
__v_prefilter(buff,
r.w(), r.h(), context.stride_in_bytes(),
curr_vOversample);
}
bc.x0(r.x());
bc.y0(r.y());
bc.x1((short) (r.x() + r.w()));
bc.y1((short) (r.y() + r.h()));
bc.xadvance(scale * advance.get(0));
bc.xoff((float) (x0.get(0) * recip_h + sub_x));
bc.yoff((float) (y0.get(0) * recip_v + sub_y));
bc.xoff2((x0.get(0) + r.w()) * recip_h + sub_x);
bc.yoff2((y0.get(0) + r.h()) * recip_v + sub_y);
} else {
returnValue = false;
}
++k;
}
}
return returnValue;
}
//copy of stbtt__oversample_shift(..) as it's inaccessible
private static float __oversample_shift(int oversample) {
if(oversample == 0) {
return 0.0f;
}
return (float)-(oversample - 1) / (2.0f * (float)oversample);
}
private static final int MAX_OVERSAMPLE = 8;
private static final int __OVER_MASK = MAX_OVERSAMPLE - 1;
private static void __h_prefilter(ByteBuffer pixels, int w, int h, int stride_in_bytes, int kernel_width) {
final int pixels_offset = pixels.position();
int pixelstride = 0;
byte[] buffer = new byte[MAX_OVERSAMPLE];
int safe_w = w - kernel_width;
int j;
Arrays.fill(buffer, 0, MAX_OVERSAMPLE, (byte)0);
for(j = 0 ; j < h ; j++) {
int i;
int total;
Arrays.fill(buffer, 0, kernel_width, (byte)0);
total = 0;
for(i = 0 ; i <= safe_w ; i++) {
total += Byte.toUnsignedInt(pixels.get(pixels_offset + (pixelstride + i))) - Byte.toUnsignedInt(buffer[i & __OVER_MASK]);
buffer[(i + kernel_width) & __OVER_MASK] = pixels.get(pixels_offset + (pixelstride + i));
pixels.put(pixels_offset + (pixelstride + i), (byte) Integer.divideUnsigned(total, kernel_width));
}
for(; i < w ; ++i) {
// if(Byte.toUnsignedInt(pixels.get(pixels_offset + (pixelstride + i))) != 0) {
// throw new RuntimeException("Expected '0' but was '" + Byte.toUnsignedInt(pixels.get(pixels_offset + (pixelstride + i))) + "'");
// }
total -= Byte.toUnsignedInt(buffer[i & __OVER_MASK]);
pixels.put(pixels_offset + (pixelstride + i), (byte) Integer.divideUnsigned(total, kernel_width));
}
pixelstride += stride_in_bytes;
}
}
private static void __v_prefilter(ByteBuffer pixels, int w, int h, int stride_in_bytes, int kernel_width) {
final int pixels_offset = pixels.position();
int pixelstride = 0;
byte[] buffer = new byte[MAX_OVERSAMPLE];
int safe_h = h - kernel_width;
int j;
Arrays.fill(buffer, 0, MAX_OVERSAMPLE, (byte)0);
for(j = 0 ; j < w ; j++) {
int i;
int total;
Arrays.fill(buffer, 0, kernel_width, (byte)0);
total = 0;
for(i = 0 ; i <= safe_h ; i++) {
total += Byte.toUnsignedInt(pixels.get(pixels_offset + ((pixelstride + i) * stride_in_bytes))) - Byte.toUnsignedInt(buffer[i & __OVER_MASK]);
buffer[(i + kernel_width) & __OVER_MASK] = pixels.get(pixels_offset + ((pixelstride + i) * stride_in_bytes));
pixels.put(pixels_offset + ((pixelstride + i) * stride_in_bytes), (byte) Integer.divideUnsigned(total, kernel_width));
}
for(; i < h ; ++i) {
// if(Byte.toUnsignedInt(pixels.get(pixels_offset + ((pixelstride + i) * stride_in_bytes))) != 0) {
// throw new RuntimeException("Expected '0' but was '" + Byte.toUnsignedInt(pixels.get(pixels_offset + ((pixelstride + i) * stride_in_bytes))) + "'");
// }
total -= Byte.toUnsignedInt(buffer[i & __OVER_MASK]);
pixels.put(pixels_offset + ((pixelstride + i) * stride_in_bytes), (byte) Integer.divideUnsigned(total, kernel_width));
}
pixelstride += 1;
}
}
It seems to work out fine when I remove the offset from the __v_prefilter(..) method.
Thus changing final int pixels_offset = pixels.position(); to final int pixels_offset = 0; (or removing it altogether from the code).
I say it seems because I have not done any bitwise comparisons of the produced maps between my, now working, and the original code. There are just no, to me at least, discernible mangled bits in the texture anymore.
Why are those pixel rgb values sometimes equal and sometimes not equal? I am learning image processing. It would be great if someone help me out here.
public class ColorTest1 {
Color p1;
Color p2;
ColorTest1() throws IOException, InterruptedException {
BufferedImage bi = ImageIO.read(new File("d:\\x.jpg"));
for (int y = 0; y < bi.getHeight(); y++) {
for (int x = 0; x < bi.getWidth() - 1; x++) {
p1 = new Color(bi.getRGB(x, y));
p2 = new Color(bi.getRGB(x + 1, y));
int a = (p1.getAlpha() + p2.getAlpha()) / 2;
int r = (p1.getRed() + p2.getRed()) / 2;
int g = (p1.getGreen() + p2.getGreen()) / 2;
int b = (p1.getBlue() + p2.getBlue()) / 2;
int x1 = p1.getRGB();
int x2 = p2.getRGB();
int sum1 = (x1 + x2) / 2;
int sum2 = a * 16777216 + r * 65536 + g * 256 + b;
System.out.println(sum1 == sum2);
}
}
}
public static void main(String... areg) throws IOException, InterruptedException {
new ColorTest1();
}
}
This is the image:
Take two pixels. One is black. The other is nearly black but with a slight bit of red in it, just 1/255. Ignore alpha. r will be (0 + 1) / 2 = 0. g and b will be 0 too. x1 will be 0. x2 will be 65536, right? So sum1 will be 65536 / 2 = 32768. sum2 obviously will be 0.
Whenever the sum of either red or green of the two colours is odd, the int division will set the high bit of the next colour in RGB, leading to an unexpected result.
I need to implement Gaussian Blur in Java for 3x3, 5x5 and 7x7 matrix. Can you correct me if I'm wrong:
I've a matrix(M) 3x3 (middle value is M(0, 0)):
1 2 1
2 4 2
1 2 1
I take one pixel(P) from image and for each nearest pixel:
s = M(-1, -1) * P(-1, -1) + M(-1, 0) * P(-1, 0) + ... + M(1, 1) * P(1, 1)
An then division it total value of matrix:
P'(i, j) = s / M(-1, -1) + M(-1, 0) + ... + M(1, 1)
That's all that my program do. I leave extreme pixels not changed.
My program:
for(int i = 1; i < height - 1; i++){
for(int j = 1; j < width - 1; j++){
int sum = 0, l = 0;
for(int m = -1; m <= 1; m++){
for(int n = -1; n <= 1; n++){
try{
System.out.print(l + " ");
sum += mask3[l++] * Byte.toUnsignedInt((byte) source[(i + m) * height + j + n]);
} catch(ArrayIndexOutOfBoundsException e){
int ii = (i + m) * height, jj = j + n;
System.out.println("Pixels[" + ii + "][" + jj + "] " + i + ", " + j);
System.exit(0);
}
}
System.out.println();
}
System.out.println();
output[i * width + j] = sum / maskSum[0];
}
}
I get source from a BufferedImage like this:
int[] source = image.getRGB(0, 0, width, height, null, 0, width);
So for this image:
Result is this:
Can you describe me, what is wrong with my program?
First of all, your formula for calculating the index in the source array is wrong. The image data is stored in the array one pixel row after the other. Therefore the index given x and y is calculated like this:
index = x + y * width
Furthermore the color channels are stored in different bits of the int cannot simply do the calculations with the whole int, since this allows channels to influence other channels.
The following solution should work (even though it just leaves the pixels at the bounds transparent):
public static BufferedImage blur(BufferedImage image, int[] filter, int filterWidth) {
if (filter.length % filterWidth != 0) {
throw new IllegalArgumentException("filter contains a incomplete row");
}
final int width = image.getWidth();
final int height = image.getHeight();
final int sum = IntStream.of(filter).sum();
int[] input = image.getRGB(0, 0, width, height, null, 0, width);
int[] output = new int[input.length];
final int pixelIndexOffset = width - filterWidth;
final int centerOffsetX = filterWidth / 2;
final int centerOffsetY = filter.length / filterWidth / 2;
// apply filter
for (int h = height - filter.length / filterWidth + 1, w = width - filterWidth + 1, y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
int r = 0;
int g = 0;
int b = 0;
for (int filterIndex = 0, pixelIndex = y * width + x;
filterIndex < filter.length;
pixelIndex += pixelIndexOffset) {
for (int fx = 0; fx < filterWidth; fx++, pixelIndex++, filterIndex++) {
int col = input[pixelIndex];
int factor = filter[filterIndex];
// sum up color channels seperately
r += ((col >>> 16) & 0xFF) * factor;
g += ((col >>> 8) & 0xFF) * factor;
b += (col & 0xFF) * factor;
}
}
r /= sum;
g /= sum;
b /= sum;
// combine channels with full opacity
output[x + centerOffsetX + (y + centerOffsetY) * width] = (r << 16) | (g << 8) | b | 0xFF000000;
}
}
BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
result.setRGB(0, 0, width, height, output, 0, width);
return result;
}
int[] filter = {1, 2, 1, 2, 4, 2, 1, 2, 1};
int filterWidth = 3;
BufferedImage blurred = blur(img, filter, filterWidth);
I was using this code placed here to generate bar-charts for my datasets. However, the colours were all the same (red in the code), so I decided to generate a colour ramp for this. I wrote the following code:
Color[] getColorRamp(int numColours)
{
Color[] colours = new Color[numColours];
int red_1 = 255;
int green_1 = 0;
int blue_1 = 0;
int red_2 = 0;
int green_2 = 0;
int blue_2 = 255;
int count = 0;
for (float t=0.0f;t<1.0f;t+=1.0/(float)numColours) {
colours[count] = new Color((int)(t*red_2 + (1-t)*red_1),
(int)(t*green_2 + (1-t)*green_1),
(int)(t*blue_2 + (1-t)*blue_1),34);
//System.out.print((int)(t*red_2 + (1-t)*red_1) +",");
//System.out.print((int)(t*green_2 + (1-t)*green_1) +",");
//System.out.println((int)(t*blue_2 + (1-t)*blue_1));
}
return colours;
}
It is here, where the problem starts. Only the first colour (pretty light blue) get rendered properly. Other colours are rendered as black! You can see that I have put System.out.println to verify the colours generated (commented in the code posted here). I saw that colours were generated as perfect RGB combinations.
The modified barchart function is posted here:
void drawBarChart(Graphics g, double[] values, String[] names, String title)
{
if (values == null || values.length == 0)
return;
double minValue = 0;
double maxValue = 0;
for (int i = 0; i < values.length; i++) {
if (minValue > values[i])
minValue = values[i];
if (maxValue < values[i])
maxValue = values[i];
}
//Graphics2D g = (Graphics2D)gg;
Dimension d = getSize();
int clientWidth = d.width;
int clientHeight = d.height;
int barWidth = clientWidth / values.length;
Font titleFont = new Font("SansSerif", Font.BOLD, 20);
FontMetrics titleFontMetrics = g.getFontMetrics(titleFont);
Font labelFont = new Font("SansSerif", Font.PLAIN, 10);
FontMetrics labelFontMetrics = g.getFontMetrics(labelFont);
int titleWidth = titleFontMetrics.stringWidth(title);
int y = titleFontMetrics.getAscent();
int x = (clientWidth - titleWidth) / 2;
g.setFont(titleFont);
g.drawString(title, x, y);
int top = titleFontMetrics.getHeight();
int bottom = labelFontMetrics.getHeight();
if (maxValue == minValue)
return;
double scale = (clientHeight - top - bottom) / (maxValue - minValue);
y = clientHeight - labelFontMetrics.getDescent();
g.setFont(labelFont);
Color[] colours = getColorRamp(values.length);
for (int i = 0; i < values.length; i++) {
int valueX = i * barWidth + 1;
int valueY = top;
int height = (int) (values[i] * scale);
if (values[i] >= 0)
valueY += (int) ((maxValue - values[i]) * scale);
else {
valueY += (int) (maxValue * scale);
height = -height;
}
g.setColor(colours[i]);
g.fillRect(valueX, valueY, barWidth - 2, height);
g.setColor(Color.black);
g.drawRect(valueX, valueY, barWidth - 2, height);
int labelWidth = labelFontMetrics.stringWidth(names[i]);
x = i * barWidth + (barWidth - labelWidth) / 2;
g.drawString(names[i], x, y);
}
//paintComponent(g);
}
I wish to know, what mistake I am making!
You're probably going to hit yourself on the head now. The reason it fails is that you forget to increase the variable count after setting the first colour, so you're constantly overwriting the first element of the Color array, and leaving all the other values in the array as their initial default (null).
Fixed code:
for (float t=0.0f;t<1.0f;t+=1.0/(float)numColours) {
colours[count++] = new Color((int)(t*red_2 + (1-t)*red_1),
(int)(t*green_2 + (1-t)*green_1),
(int)(t*blue_2 + (1-t)*blue_1),34);
}
(Notice the colours[count++])
90x90 image is divided into 9x9 blocks with 8 chaincode direction and to minimize the feature downsample is required!! how can i downsample the image into 5x5 block using 5x5 gaussian filter. Is there a library function in java to implement gaussian filter.
I don't quite know what you are going for, but only for resizing the image, you could use AffineTransform at = AffineTransform.getTranslateInstance(x, y); and then at.scale(scaleX, scaleY); but I suppose you actually want to blur the image.
This is actually a quite simple process, I wrote a method for that once.
public BufferedImage blur(int range, int angle)
{
BufferedImage b = new BufferedImage(main_image.getWidth() * 2, main_image.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g = b.createGraphics();
for(int x = 0; x < main_image.getWidth(); x++)
{
for(int y = 0; y < main_image.getHeight(); y++)
{
int red[] = new int[range * 2], green[] = new int[range * 2], blue[] = new int[range * 2];
int pixels[] = new int[range * 2];
for(int i = 0; i < pixels.length; i++)
{
pixels[i] = main_image.getRGB(clamp(x - clamp(range / 2, 0, range) + i, 0, main_image.getWidth() - 1), clamp(y - clamp(range / 2, 0, range) + (int)(i * Math.toRadians(angle)), 0, main_image.getHeight() - 1));
red[i] = (pixels[i] >> 16) & 0xff;
green[i] = (pixels[i] >> 8) & 0xff;
blue[i] = (pixels[i]) & 0xff;
}
int red_t = 0, green_t = 0, blue_t = 0;
for(int i = 0; i < pixels.length; i++)
{
red_t += red[i];
green_t += green[i];
blue_t += blue[i];
}
int r = red_t / (range * 2);
int gr = green_t / (range * 2);
int bl = blue_t / (range * 2);
//System.out.println(r + ", " + gr + ", " + bl);
g.setColor(new Color(r, gr, bl));
g.fillRect(x, y, 1, 1);
}
}
g.dispose();
return b;
}
This, is actually a pretty simple linear fiter. For a gaussian filter you would have to make the pixels, red, green and blue variable into a 2-dimensional array, and just repeat the whole procces for the y axis. If you understand, how the gaussian blue works, you should have no problem implementing this or changing it to a gaussian filter. If you want, you can ask me stuff about it.