<commit_before>package info.ata4.minecraft.minema.client.capture;

import java.nio.ByteBuffer;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.OpenGlHelper;
import net.minecraft.client.shader.Framebuffer;
import org.lwjgl.opengl.GLContext;
import org.lwjgl.opengl.Util;
import org.lwjgl.util.Dimension;

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL12.*;
import static org.lwjgl.opengl.ARBBufferObject.*;
import static org.lwjgl.opengl.ARBPixelBufferObject.*;

public class Capturer {

    private static final Minecraft MC = Minecraft.getMinecraft();
    private static final int BPP = 3;
    private static final int TYPE = GL_UNSIGNED_BYTE;
    private static final int FORMAT = GL_BGR;

    private static final int PBO_TARGET = GL_PIXEL_PACK_BUFFER_ARB;
    private static final int PBO_USAGE = GL_STREAM_READ_ARB;
    private static final int PBO_ACCESS = GL_READ_ONLY_ARB;

    private final Dimension start;
    private final ByteBuffer buffer;

    private final boolean usePBO;
    private final boolean useFBO;

    // PBO state variables
    private int frontName;
    private int backName;
    private ByteBuffer bufferPBO;

    public Capturer() {
        this(true, true);
    }

    public Capturer(boolean pbo, boolean fbo) {
        start = new Dimension(MC.displayWidth, MC.displayHeight);
        
        usePBO = pbo && GLContext.getCapabilities().GL_ARB_pixel_buffer_object;
        useFBO = fbo && OpenGlHelper.isFramebufferEnabled();
        
        int bufferSize = start.getWidth() * start.getHeight() * BPP;
        buffer = ByteBuffer.allocateDirect(bufferSize);

        if (usePBO) {
            frontName = glGenBuffersARB();
            glBindBufferARB(PBO_TARGET, frontName);
            glBufferDataARB(PBO_TARGET, bufferSize, PBO_USAGE);

            backName = glGenBuffersARB();
            glBindBufferARB(PBO_TARGET, backName);
            glBufferDataARB(PBO_TARGET, bufferSize, PBO_USAGE);

            glBindBufferARB(PBO_TARGET, 0);
        }
    }

    public Dimension getCaptureDimension() {
        return start;
    }

    public ByteBuffer capture() {
        // check if the dimensions are still the same
        int currentWidth = MC.displayWidth;
        int currentHeight = MC.displayHeight;
        if (currentWidth != start.getWidth() || currentHeight != start.getHeight()) {
            throw new IllegalStateException(
                    String.format("Display size changed! %dx%d not equals the start dimension of %dx%d", currentWidth,
                            currentHeight, start.getWidth(), start.getHeight()));
        }

        if (usePBO) {
            glBindBufferARB(PBO_TARGET, frontName);
        }

        // read pixels
        readPixels();

        if (usePBO) {
            // copy back buffer
            glBindBufferARB(PBO_TARGET, backName);
            bufferPBO = glMapBufferARB(PBO_TARGET, PBO_ACCESS, buffer.capacity(), bufferPBO);
            bufferPBO.rewind();
            buffer.rewind();
            buffer.put(bufferPBO);
            glUnmapBufferARB(PBO_TARGET);
            glBindBufferARB(PBO_TARGET, 0);
            
            // swap PBOs
            int swapName = frontName;
            frontName = backName;
            backName = swapName;

            // If mapping threw an error -> crash immediately please
            Util.checkGLError();
        }<commit_msg>Added missing rewind() before readPixels<commit_after>package info.ata4.minecraft.minema.client.capture;

import java.nio.ByteBuffer;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.OpenGlHelper;
import net.minecraft.client.shader.Framebuffer;
import org.lwjgl.opengl.GLContext;
import org.lwjgl.opengl.Util;
import org.lwjgl.util.Dimension;

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL12.*;
import static org.lwjgl.opengl.ARBBufferObject.*;
import static org.lwjgl.opengl.ARBPixelBufferObject.*;

public class Capturer {

    private static final Minecraft MC = Minecraft.getMinecraft();
    private static final int BPP = 3;
    private static final int TYPE = GL_UNSIGNED_BYTE;
    private static final int FORMAT = GL_BGR;

    private static final int PBO_TARGET = GL_PIXEL_PACK_BUFFER_ARB;
    private static final int PBO_USAGE = GL_STREAM_READ_ARB;
    private static final int PBO_ACCESS = GL_READ_ONLY_ARB;

    private final Dimension start;
    private final ByteBuffer buffer;

    private final boolean usePBO;
    private final boolean useFBO;

    // PBO state variables
    private int frontName;
    private int backName;
    private ByteBuffer bufferPBO;

    public Capturer() {
        this(true, true);
    }

    public Capturer(boolean pbo, boolean fbo) {
        start = new Dimension(MC.displayWidth, MC.displayHeight);
        
        usePBO = pbo && GLContext.getCapabilities().GL_ARB_pixel_buffer_object;
        useFBO = fbo && OpenGlHelper.isFramebufferEnabled();
        
        int bufferSize = start.getWidth() * start.getHeight() * BPP;
        buffer = ByteBuffer.allocateDirect(bufferSize);

        if (usePBO) {
            frontName = glGenBuffersARB();
            glBindBufferARB(PBO_TARGET, frontName);
            glBufferDataARB(PBO_TARGET, bufferSize, PBO_USAGE);

            backName = glGenBuffersARB();
            glBindBufferARB(PBO_TARGET, backName);
            glBufferDataARB(PBO_TARGET, bufferSize, PBO_USAGE);

            glBindBufferARB(PBO_TARGET, 0);
        }
    }

    public Dimension getCaptureDimension() {
        return start;
    }

    public ByteBuffer capture() {
        // check if the dimensions are still the same
        int currentWidth = MC.displayWidth;
        int currentHeight = MC.displayHeight;
        if (currentWidth != start.getWidth() || currentHeight != start.getHeight()) {
            throw new IllegalStateException(
                    String.format("Display size changed! %dx%d not equals the start dimension of %dx%d", currentWidth,
                            currentHeight, start.getWidth(), start.getHeight()));
        }

        if (usePBO) {
            glBindBufferARB(PBO_TARGET, frontName);
        }
        
        // read pixels
        buffer.rewind();
        readPixels();

        if (usePBO) {
            // copy back buffer
            glBindBufferARB(PBO_TARGET, backName);
            bufferPBO = glMapBufferARB(PBO_TARGET, PBO_ACCESS, buffer.capacity(), bufferPBO);
            bufferPBO.rewind();
            buffer.rewind();
            buffer.put(bufferPBO);
            glUnmapBufferARB(PBO_TARGET);
            glBindBufferARB(PBO_TARGET, 0);
            
            // swap PBOs
            int swapName = frontName;
            frontName = backName;
            backName = swapName;

            // If mapping threw an error -> crash immediately please
            Util.checkGLError();
        }
{'fraction_non_alphanumeric': 0.08656629994209612, 'fraction_numerical': 0.0023161551823972205, 'mean_word_length': 2.480604534005038, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3634420', 'n_tokens_mistral': 2025, 'n_tokens_neox': 2024, 'n_words': 471}