gpt4 book ai didi

java - 将 C 库翻译成 Java : Getting mangled garbage data in top-left of resulting bitmap

转载 作者:太空宇宙 更新时间:2023-11-04 04:19:39 26 4
gpt4 key购买 nike

LWJGL3库包含对 STB TrueType 的绑定(bind)和其他库 Sean Barrett .

为了修改本库提供的打包API来渲染SDF字形而不是普通位图,我正在从 java 库中复制纹理渲染代码。

我设法让它几乎可以正常工作,但我遇到了一 block 绊脚石,我在纹理的左上角得到了乱七八糟的垃圾数据。我有点确信错误一定位于我的 stbtt__h_prefilter(...) 版本的代码中的某处,因为这是断言失败的地方。

编辑:我在对缓冲区进行读/写操作时忘记考虑当前缓冲区位置。现在我的位图中仍然有一些垃圾数据,但分布更均匀。

事实上,查看更新的第二张图片,似乎每个字形的最左侧部分都以某种方式向下移动了字形高度的一半。我无法找出它发生的位置或原因,特别是考虑到位图处理在每个字形呈现为字体后单独作用于每个字形,所以根据我的理解,下一行字形应该只是覆盖这个..?

原库生成的位图: Correct bitmap

我的版本生成的位图(见偏移半线切割成一些字母): Mangled bitmap

附录:我的版本生成的位图没有 prefilter_... 方法: Unfiltered bitmap

您可以在下面找到我的库方法版本。原文可见here .


STB... 函数的引用是指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;
}
}

最佳答案

当我从 __v_prefilter(..) 方法中删除偏移量时,它似乎工作正常。

因此将 final int pixels_offset = pixels.position(); 更改为 final int pixels_offset = 0;(或将其从代码中完全删除)。

我说它似乎是因为我还没有对我现在正在工作的代码和原始代码之间生成的映射进行任何按位比较。至少对我来说,纹理中不再有可辨别的破损位。

关于java - 将 C 库翻译成 Java : Getting mangled garbage data in top-left of resulting bitmap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48022431/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com