<commit_before><commit_msg>Added horrible 'dump_images' utility.<commit_after>-- This Source Code Form is subject to the terms of the Mozilla Public
-- License, v. 2.0. If a copy of the MPL was not distributed with this
-- file, You can obtain one at http://mozilla.org/MPL/2.0/.
--
-- Copyright 2016 [bk]door.maus

local glurg = require "glurg"

local args = { ... }
if #args < 2 then
	io.stderr:write("dump_images <file.trace> <output_dir>\n")
	os.exit(1)
end

local stream = glurg.common.SnappyFileStream.new()
stream:open(args[1], glurg.common.file_mode_read)

local trace = glurg.trace.TraceFile.new()
if not trace:verify_is_compatible_version(stream) then
	io.stderr.write("trace file is not compatible");
	os.exit(1)
end

local textures = {}
local function extract_texture(call)
	local target = call:get_argument_at(0):to_enumeration()
	local GL_TEXTURE_2D =
		target.signature:get_value_by_name("GL_TEXTURE_2D"):to_integer()
	if target.value:to_integer() ~= GL_TEXTURE_2D then
		return false
	end

	local textureBuilder = glurg.resources.TextureBlobBuilder.new()
	textureBuilder:set_texture_type(glurg.resources.texture.type_2d)
	textureBuilder:set_mipmap_level(call:get_argument_at(1):to_integer())
	textureBuilder:set_width(call:get_argument_at(3):to_integer())
	textureBuilder:set_height(call:get_argument_at(4):to_integer())
	textureBuilder:set_depth(1)
	textureBuilder:set_wrap_mode(0, glurg.resources.texture.wrap_edge_clamp)
	textureBuilder:set_wrap_mode(1, glurg.resources.texture.wrap_edge_clamp)
	textureBuilder:set_minification_filter(glurg.resources.texture.filter_nearest)
	textureBuilder:set_magnification_filter(glurg.resources.texture.filter_nearest)

	local blob = call:get_argument_at(8):to_blob()
	local data_format = call:get_argument_at(6):to_enumeration().value
	local data_type = call:get_argument_at(7):to_enumeration().value
	textureBuilder:extract_from_call_args(
		data_format:to_integer(), data_type:to_integer(), blob.data)

	local textureBlob = textureBuilder:build()
	if glurg.resources.is_compatible_texture_blob(textureBlob) then
		local textureResource = glurg.resources.Texture.new(textureBlob)
		local hash = textureBlob.hash:to_string()

		if not textures[hash] then
			local pixelBuffer = glurg.common.PixelDataBuffer.new()
			local outputPng = glurg.common.PixelDataBuffer.new()

			textureResource:decode_image(4, pixelBuffer)
			glurg.common.pixel_data_buffer_to_png(
				pixelBuffer, textureBlob.width, textureBlob.height, 4,
				outputPng)

			local fileStream = glurg.common.SimpleFileStream.new()
			fileStream:open(
				args[2] .. "/" .. hash .. ".png",
				glurg.common.file_mode_write)
			fileStream:write(outputPng.data, outputPng.size)

			textures[hash] = true
		end
	end

	return true
end

while not stream.is_end_of_file do
	local event = glurg.trace.read_event(trace, stream)
	if event.type == glurg.trace.event_leave then
		local call = trace:get_call(event.call_index)
		if call.signature.name == "glTexImage2D" then
			if extract_texture(call) then
				print("texture at call:", event.call_index)
			end
		end
		trace:delete_call(call)
	end
end
{'fraction_non_alphanumeric': 0.1103248633000965, 'fraction_numerical': 0.00964940495336121, 'mean_word_length': 5.850220264317181, 'pattern_counts': {'":': 0, '<': 6, '<?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': '5976402', 'n_tokens_mistral': 1120, 'n_tokens_neox': 1047, 'n_words': 198}