Skip to content

Commit

Permalink
[STB] Added STBImageResize2; rewriting STBVorbis
Browse files Browse the repository at this point in the history
  • Loading branch information
squid233 committed Jan 27, 2024
1 parent d3f4670 commit 2efa449
Show file tree
Hide file tree
Showing 8 changed files with 674 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public final class OverrunGL {
/**
* The version of STB native libraries.
*/
public static final String STB_VERSION = "0.1.0.0";
public static final String STB_VERSION = "0.1.0.1";
private static final Consumer<String> DEFAULT_LOGGER = System.err::println;
private static Consumer<String> apiLogger = DEFAULT_LOGGER;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* MIT License
*
* Copyright (c) 2024 Overrun Organization
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*/

package overrungl.stb;

import overrun.marshal.Upcall;
import overrungl.NativeType;

import java.lang.foreign.Arena;
import java.lang.foreign.MemorySegment;

/**
* INPUT CALLBACK: this callback is used for input scanlines
* <p>
* The input callback is super flexible - it calls you with the input address
* (based on the stride and base pointer), it gives you an optional_output
* pointer that you can fill, or you can just return your own pointer into
* your own data.
* <p>
* You can also do conversion from non-supported data types if necessary - in
* this case, you ignore the input_ptr and just use the x and y parameters to
* calculate your own input_ptr based on the size of each non-supported pixel.
* (Something like the third example below.)
* <p>
* You can also install just an input or just an output callback by setting the
* callback that you don't want to zero.
* <p>
* First example, progress: (getting a callback that you can monitor the progress):
* <pre>{@code void const * my_callback( void * optional_output, void const * input_ptr, int num_pixels, int x, int y, void * context )
* {
* percentage_done = y / input_height;
* return input_ptr; // use buffer from call
* }}</pre>
* <p>
* Next example, copying: (copy from some other buffer or stream):
* <pre>{@code void const * my_callback( void * optional_output, void const * input_ptr, int num_pixels, int x, int y, void * context )
* {
* CopyOrStreamData( optional_output, other_data_src, num_pixels * pixel_width_in_bytes );
* return optional_output; // return the optional buffer that we filled
* }}</pre>
* <p>
* Third example, input another buffer without copying: (zero-copy from other buffer):
* <pre>{@code void const * my_callback( void * optional_output, void const * input_ptr, int num_pixels, int x, int y, void * context )
* {
* void * pixels = ( (char*) other_image_base ) + ( y * other_image_stride ) + ( x * other_pixel_width_in_bytes );
* return pixels; // return pointer to your data without copying
* }}</pre>
*
* @author squid233
* @since 0.1.0
*/
@FunctionalInterface
public interface STBIRInputCallback extends Upcall {
/**
* the type
*/
Type<STBIRInputCallback> TYPE = Upcall.type();

@NativeType("void const *")
@Stub
MemorySegment invoke(@NativeType("void *") MemorySegment optional_output, @NativeType("void const *") MemorySegment input_ptr, int num_pixels, int x, int y, @NativeType("void *") MemorySegment context);

@Override
default MemorySegment stub(Arena arena) {
return TYPE.of(arena, this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* MIT License
*
* Copyright (c) 2024 Overrun Organization
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*/

package overrungl.stb;

import overrun.marshal.Upcall;
import overrungl.NativeType;

import java.lang.foreign.Arena;
import java.lang.foreign.MemorySegment;

/**
* callbacks for user installed filters
*
* @author squid233
* @since 0.1.0
*/
@FunctionalInterface
public interface STBIRKernelCallback extends Upcall {
/**
* the type
*/
Type<STBIRKernelCallback> TYPE = Upcall.type();

// centered at zero
@Stub
float invoke(float x, float scale, @NativeType("void *") MemorySegment user_data);

@Override
default MemorySegment stub(Arena arena) {
return TYPE.of(arena, this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* MIT License
*
* Copyright (c) 2024 Overrun Organization
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*/

package overrungl.stb;

import overrun.marshal.Upcall;
import overrungl.NativeType;

import java.lang.foreign.Arena;
import java.lang.foreign.MemorySegment;

/**
* OUTPUT CALLBACK: this callback is used for output scanlines
* <p>
* The output callback is considerably simpler - it just calls you so that you can dump
* out each scanline. You could even directly copy out to disk if you have a simple format
* like TGA or BMP. You can also convert to other output types here if you want.
* <p>
* Simple example:
* <pre>{@code void const * my_output( void * output_ptr, int num_pixels, int y, void * context )
* {
* percentage_done = y / output_height;
* fwrite( output_ptr, pixel_width_in_bytes, num_pixels, output_file );
* }}</pre>
*
* @author squid233
* @since 0.1.0
*/
@FunctionalInterface
public interface STBIROutputCallback extends Upcall {
/**
* the type
*/
Type<STBIROutputCallback> TYPE = Upcall.type();

@Stub
void invoke(@NativeType("void const *") MemorySegment output_ptr, int num_pixels, int y, @NativeType("void *") MemorySegment context);

@Override
default MemorySegment stub(Arena arena) {
return TYPE.of(arena, this);
}
}
111 changes: 111 additions & 0 deletions modules/overrungl.stb/src/main/java/overrungl/stb/STBIRRESIZE.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* MIT License
*
* Copyright (c) 2024 Overrun Organization
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*/

package overrungl.stb;

import overrun.marshal.struct.Struct;

import java.lang.foreign.*;

import static java.lang.foreign.ValueLayout.*;

/**
* {@code STBIR_RESIZE}
*
* @author squid233
* @since 0.1.0
*/
public final class STBIRRESIZE extends Struct {
/**
* Layout
*/
public static final StructLayout LAYOUT = MemoryLayout.structLayout(
ADDRESS.withName("user_data"),
ADDRESS.withName("input_pixels"),
JAVA_INT.withName("input_w"),
JAVA_INT.withName("input_h"),
JAVA_DOUBLE.withName("input_s0"),
JAVA_DOUBLE.withName("input_t0"),
JAVA_DOUBLE.withName("input_s1"),
JAVA_DOUBLE.withName("input_t1"),
ADDRESS.withName("input_cb"),
ADDRESS.withName("output_pixels"),
JAVA_INT.withName("output_w"),
JAVA_INT.withName("output_h"),
JAVA_INT.withName("output_subx"),
JAVA_INT.withName("output_suby"),
JAVA_INT.withName("output_subw"),
JAVA_INT.withName("output_subh"),
ADDRESS.withName("output_cb"),
JAVA_INT.withName("input_stride_in_bytes"),
JAVA_INT.withName("output_stride_in_bytes"),
JAVA_INT.withName("splits"),
JAVA_INT.withName("fast_alpha"),
JAVA_INT.withName("needs_rebuild"),
JAVA_INT.withName("called_alloc"),
JAVA_INT.withName("input_pixel_layout_public"),
JAVA_INT.withName("output_pixel_layout_public"),
JAVA_INT.withName("input_data_type"),
JAVA_INT.withName("output_data_type"),
JAVA_INT.withName("horizontal_filter"),
JAVA_INT.withName("vertical_filter"),
JAVA_INT.withName("horizontal_edge"),
JAVA_INT.withName("vertical_edge"),
ADDRESS.withName("horizontal_filter_kernel"),
ADDRESS.withName("horizontal_filter_support"),
ADDRESS.withName("vertical_filter_kernel"),
ADDRESS.withName("vertical_filter_support"),
ADDRESS.withName("samplers")
);

/**
* Creates a struct with the given layout.
*
* @param segment the segment
* @param elementCount the element count
*/
public STBIRRESIZE(MemorySegment segment, long elementCount) {
super(segment, elementCount, LAYOUT);
}

/**
* Allocates a struct with the given layout.
*
* @param allocator the allocator
* @param elementCount the element count
*/
public STBIRRESIZE(SegmentAllocator allocator, long elementCount) {
super(allocator, elementCount, LAYOUT);
}

/**
* Creates a struct with the given layout.
*
* @param segment the segment
*/
public STBIRRESIZE(MemorySegment segment) {
super(segment, LAYOUT);
}

/**
* Allocates a struct with the given layout.
*
* @param allocator the allocator
*/
public STBIRRESIZE(SegmentAllocator allocator) {
super(allocator, LAYOUT);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* MIT License
*
* Copyright (c) 2024 Overrun Organization
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*/

package overrungl.stb;

import overrun.marshal.Upcall;
import overrungl.NativeType;

import java.lang.foreign.Arena;
import java.lang.foreign.MemorySegment;

/**
* callbacks for user installed filters
*
* @author squid233
* @since 0.1.0
*/
@FunctionalInterface
public interface STBIRSupportCallback extends Upcall {
/**
* the type
*/
Type<STBIRSupportCallback> TYPE = Upcall.type();

// centered at zero
@Stub
float invoke(float scale, @NativeType("void *") MemorySegment user_data);

@Override
default MemorySegment stub(Arena arena) {
return TYPE.of(arena, this);
}
}
Loading

0 comments on commit 2efa449

Please sign in to comment.