<commit_before>/* Copyright (c) 2013-2015 <NAME>
 *
 * 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/. */
#include "imagemagick-gif-encoder.h"

#include "gba/video.h"

static void _magickPostVideoFrame(struct GBAAVStream*, struct GBAVideoRenderer* renderer);
static void _magickPostAudioFrame(struct GBAAVStream*, int32_t left, int32_t right);

void ImageMagickGIFEncoderInit(struct ImageMagickGIFEncoder* encoder) {
	encoder->wand = 0;

	encoder->d.postVideoFrame = _magickPostVideoFrame;
	encoder->d.postAudioFrame = _magickPostAudioFrame;

	encoder->frameskip = 2;
}

bool ImageMagickGIFEncoderOpen(struct ImageMagickGIFEncoder* encoder, const char* outfile) {
	MagickWandGenesis();
	encoder->wand = NewMagickWand();
	encoder->outfile = strdup(outfile);
	encoder->frame = malloc(VIDEO_HORIZONTAL_PIXELS * VIDEO_VERTICAL_PIXELS * 4);
	encoder->currentFrame = 0;
	return true;
}
void ImageMagickGIFEncoderClose(struct ImageMagickGIFEncoder* encoder) {
	if (!encoder->wand) {
		return;
	}
	MagickWriteImages(encoder->wand, encoder->outfile, MagickTrue);
	free(encoder->outfile);
	free(encoder->frame);
	DestroyMagickWand(encoder->wand);
	encoder->wand = 0;
	MagickWandTerminus();
}

bool ImageMagickGIFEncoderIsOpen(struct ImageMagickGIFEncoder* encoder) {
	return !!encoder->wand;
}

static void _magickPostVideoFrame(struct GBAAVStream* stream, struct GBAVideoRenderer* renderer) {
	struct ImageMagickGIFEncoder* encoder = (struct ImageMagickGIFEncoder*) stream;

	if (encoder->currentFrame % (encoder->frameskip + 1)) {
		++encoder->currentFrame;
		return;
	}

	uint8_t* pixels;
	unsigned stride;
	renderer->getPixels(renderer, &stride, (void**) &pixels);
	size_t row;
	for (row = 0; row < VIDEO_VERTICAL_PIXELS; ++row) {
		memcpy(&encoder->frame[row * VIDEO_HORIZONTAL_PIXELS], &pixels[row * 4 *stride], VIDEO_HORIZONTAL_PIXELS * 4);
	}

	MagickConstituteImage(encoder->wand, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, "RGBP", CharPixel, encoder->frame);
	uint64_t ts = encoder->currentFrame;
	uint64_t nts = encoder->currentFrame + encoder->frameskip + 1;
	ts *= VIDEO_TOTAL_LENGTH * 100;
	nts *= VIDEO_TOTAL_LENGTH * 100;
	ts /= GBA_ARM7TDMI_FREQUENCY;
	nts /= GBA_ARM7TDMI_FREQUENCY;
	MagickSetImageDelay(encoder->wand, nts - ts);
	++encoder->currentFrame;
}

static void _magickPostAudioFrame(struct GBAAVStream* stream, int32_t left, int32_t right) {
	UNUSED(stream);
	UNUSED(left);
	UNUSED(right);
	// This is a video-only format...
}<commit_msg>Video: Fix warnings in ImageMagick<commit_after>/* Copyright (c) 2013-2015 <NAME>
 *
 * 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/. */
#include "imagemagick-gif-encoder.h"

#include "gba/video.h"

static void _magickPostVideoFrame(struct GBAAVStream*, struct GBAVideoRenderer* renderer);
static void _magickPostAudioFrame(struct GBAAVStream*, int16_t left, int16_t right);

void ImageMagickGIFEncoderInit(struct ImageMagickGIFEncoder* encoder) {
	encoder->wand = 0;

	encoder->d.postVideoFrame = _magickPostVideoFrame;
	encoder->d.postAudioFrame = _magickPostAudioFrame;

	encoder->frameskip = 2;
}

bool ImageMagickGIFEncoderOpen(struct ImageMagickGIFEncoder* encoder, const char* outfile) {
	MagickWandGenesis();
	encoder->wand = NewMagickWand();
	encoder->outfile = strdup(outfile);
	encoder->frame = malloc(VIDEO_HORIZONTAL_PIXELS * VIDEO_VERTICAL_PIXELS * 4);
	encoder->currentFrame = 0;
	return true;
}
void ImageMagickGIFEncoderClose(struct ImageMagickGIFEncoder* encoder) {
	if (!encoder->wand) {
		return;
	}
	MagickWriteImages(encoder->wand, encoder->outfile, MagickTrue);
	free(encoder->outfile);
	free(encoder->frame);
	DestroyMagickWand(encoder->wand);
	encoder->wand = 0;
	MagickWandTerminus();
}

bool ImageMagickGIFEncoderIsOpen(struct ImageMagickGIFEncoder* encoder) {
	return !!encoder->wand;
}

static void _magickPostVideoFrame(struct GBAAVStream* stream, struct GBAVideoRenderer* renderer) {
	struct ImageMagickGIFEncoder* encoder = (struct ImageMagickGIFEncoder*) stream;

	if (encoder->currentFrame % (encoder->frameskip + 1)) {
		++encoder->currentFrame;
		return;
	}

	uint8_t* pixels;
	unsigned stride;
	renderer->getPixels(renderer, &stride, (void**) &pixels);
	size_t row;
	for (row = 0; row < VIDEO_VERTICAL_PIXELS; ++row) {
		memcpy(&encoder->frame[row * VIDEO_HORIZONTAL_PIXELS], &pixels[row * 4 *stride], VIDEO_HORIZONTAL_PIXELS * 4);
	}

	MagickConstituteImage(encoder->wand, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, "RGBP", CharPixel, encoder->frame);
	uint64_t ts = encoder->currentFrame;
	uint64_t nts = encoder->currentFrame + encoder->frameskip + 1;
	ts *= VIDEO_TOTAL_LENGTH * 100;
	nts *= VIDEO_TOTAL_LENGTH * 100;
	ts /= GBA_ARM7TDMI_FREQUENCY;
	nts /= GBA_ARM7TDMI_FREQUENCY;
	MagickSetImageDelay(encoder->wand, nts - ts);
	++encoder->currentFrame;
}

static void _magickPostAudioFrame(struct GBAAVStream* stream, int16_t left, int16_t right) {
	UNUSED(stream);
	UNUSED(left);
	UNUSED(right);
	// This is a video-only format...
}
{'fraction_non_alphanumeric': 0.11992373689227837, 'fraction_numerical': 0.016396568160152527, 'mean_word_length': 5.9299867899603695, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 61, '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': '6031836', 'n_tokens_mistral': 2000, 'n_tokens_neox': 1896, 'n_words': 404}