<commit_before>/* GdkPixbuf library - SUNRAS image loader
 *
 * Copyright (C) 1999 The Free Software Foundation
 *
 * Authors: <NAME> <<EMAIL>>
 *          <NAME> <<EMAIL>>
 *
 * Based on io-gif.c, io-tiff.c and io-png.c
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
 
/*

Known bugs:
	* "Indexed" (incl grayscale) sunras files don't work
	* 1 bpp sunrasfiles don't work

*/

#include <config.h>
#include <stdio.h>
#include <glib.h>
#include <gdk/gdk.h>
#include <gtk/gtk.h>
#include "gdk-pixbuf.h"
#include "gdk-pixbuf-io.h"




/* 
   Header structure for sunras files.
   All values are in big-endian order on disk
 */

struct rasterfile {
	guint magic;
	guint width;
	guint height;
	guint depth;
	guint length;
	guint type;
	guint maptype;
	guint maplength;
};

/* 
	This does a byte-order swap. Does glib have something like
	be32_to_cpu() ??
*/

static unsigned int be32_to_cpu(guint i)
{
	unsigned int i2;
	i2 =
	    ((i & 255) << 24) | (((i >> 8) & 255) << 16) |
	    (((i >> 16) & 255) << 8) | ((i >> 24) & 255);
	return i2;
}

/* 
	Destroy notification function for the libart pixbuf 
*/

static void free_buffer(gpointer user_data, gpointer data)
{
	free(data);
}

/*

OneLineBGR_buf/file does what it says: Reads one line from file or buffer.
Note: It also changes BGR pixelorder to RGB as libart currently
doesn't support ART_PIX_BGR.

*/
static void OneLineBGR_buf(guchar * buffer, guint Width, guchar * pixels,
			   guint bpp)
{
	guint X;

	memcpy(pixels, buffer, (size_t) (Width * bpp));
	X = 0;
	while (X < Width) {
		guchar Blue;
		Blue = pixels[X * bpp];
		pixels[X * bpp] = pixels[X * bpp + 2];
		pixels[X * bpp + 2] = Blue;
		X++;
	}
}

static void OneLineBGR_file(FILE * f, guint Width, guchar * pixels,
			    guint bpp)
{
	size_t result;
	guint X;
	guchar DummyByte;

	result = fread(pixels, 1, (size_t) (Width * bpp), f);

	g_assert(result == (size_t) (Width * bpp));
	if (((Width * bpp) & 1) != 0)	/*  Not 16 bit aligned */
		(void) fread(&DummyByte, 1, 1, f);
	X = 0;
	while (X < Width) {
		guchar Blue;
		Blue = pixels[X * bpp];
		pixels[X * bpp] = pixels[X * bpp + 2];
		pixels[X * bpp + 2] = Blue;
		X++;
	}
}

static void OneLineMapped_file(FILE * f, guint Width, guchar * pixels,
			    guint bpp, guchar *Map)
{
	size_t result;
	guint X;
	guchar DummyByte;
	guchar *buffer;
	
	
	buffer =g_malloc((size_t) (Width * bpp));
	
	g_assert(buffer!=NULL);

	result = fread(buffer, 1, (size_t) (Width * bpp), f);

	g_assert(result == (size_t) (Width * bpp));
	
	if (((Width * bpp) & 1) != 0)	/*  Not 16 bit aligned */
		(void) fread(&DummyByte, 1, 1, f);
	X = 0;
	while (X < Width) {
		pixels[X * 3]   = Map[buffer[X]*3];
		pixels[X * 3+1] = Map[buffer[X]*3];
		pixels[X * 3+2] = Map[buffer[X]*3];

		X++;
	}
	g_free(buffer);
}

static void OneLineMapped_buf(guchar *buffer, guint Width, guchar * pixels,
			    guint bpp,guchar *Map)
{
	size_t result;
	guint X;
	
	X = 0;
	while (X < Width) {
		pixels[X * 3]   = Map[buffer[X]*3];
		pixels[X * 3+1] = Map[buffer[X]*3+1];
		pixels[X * 3+2] = Map[buffer[X]*3+2];

		X++;
	}
}

/* Shared library entry point */
GdkPixbuf *image_load(FILE * f)<commit_msg>Now can handle indexing correctly. Patch from Arjan to do so.<commit_after>/* GdkPixbuf library - SUNRAS image loader
 *
 * Copyright (C) 1999 The Free Software Foundation
 *
 * Authors: <NAME> <<EMAIL>>
 *          <NAME> <<EMAIL>>
 *
 * Based on io-gif.c, io-tiff.c and io-png.c
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
 
/*

Known bugs:
	* "Indexed" (incl grayscale) sunras files don't work
	  ( 1999/11/10 - Fixed for non-progressive loading )
	* 1 bpp sunrasfiles don't work yet
	* Compressed rasterfiles don't work yet

*/

#include <config.h>
#include <stdio.h>
#include <glib.h>
#include <gdk/gdk.h>
#include <gtk/gtk.h>
#include "gdk-pixbuf.h"
#include "gdk-pixbuf-io.h"




/* 
   Header structure for sunras files.
   All values are in big-endian order on disk
 */

struct rasterfile {
	guint magic;
	guint width;
	guint height;
	guint depth;
	guint length;
	guint type;
	guint maptype;
	guint maplength;
};

/* 
	This does a byte-order swap. Does glib have something like
	be32_to_cpu() ??
*/

static unsigned int be32_to_cpu(guint i)
{
	unsigned int i2;
	i2 =
	    ((i & 255) << 24) | (((i >> 8) & 255) << 16) |
	    (((i >> 16) & 255) << 8) | ((i >> 24) & 255);
	return i2;
}

/* 
	Destroy notification function for the libart pixbuf 
*/

static void free_buffer(gpointer user_data, gpointer data)
{
	free(data);
}

/*

OneLineBGR_buf/file does what it says: Reads one line from file or buffer.
Note: It also changes BGR pixelorder to RGB as libart currently
doesn't support ART_PIX_BGR.

*/
static void OneLineBGR_buf(guchar * buffer, guint Width, guchar * pixels,
			   guint bpp)
{
	guint X;

	memcpy(pixels, buffer, (size_t) (Width * bpp));
	X = 0;
	while (X < Width) {
		guchar Blue;
		Blue = pixels[X * bpp];
		pixels[X * bpp] = pixels[X * bpp + 2];
		pixels[X * bpp + 2] = Blue;
		X++;
	}
}

static void OneLineBGR_file(FILE * f, guint Width, guchar * pixels,
			    guint bpp)
{
	size_t result;
	guint X;
	guchar DummyByte;

	result = fread(pixels, 1, (size_t) (Width * bpp), f);

	g_assert(result == (size_t) (Width * bpp));
	if (((Width * bpp) & 1) != 0)	/*  Not 16 bit aligned */
		(void) fread(&DummyByte, 1, 1, f);
	X = 0;
	while (X < Width) {
		guchar Blue;
		Blue = pixels[X * bpp];
		pixels[X * bpp] = pixels[X * bpp + 2];
		pixels[X * bpp + 2] = Blue;
		X++;
	}
}

static void OneLineMapped_file(FILE * f, guint Width, guchar * pixels,
			    guint bpp, guchar *Map)
{
	size_t result;
	guint X;
	guchar DummyByte;
	guchar *buffer;
	
	
	buffer =g_malloc((size_t) (Width * bpp));
	
	g_assert(buffer!=NULL);

	result = fread(buffer, 1, (size_t) (Width * bpp), f);

	g_assert(result == (size_t) (Width * bpp));
	
	if (((Width * bpp) & 1) != 0)	/*  Not 16 bit aligned */
		(void) fread(&DummyByte, 1, 1, f);
	X = 0;
	while (X < Width) {
		pixels[X * 3]   = Map[buffer[X]];
		pixels[X * 3+1] = Map[buffer[X]+256];
		pixels[X * 3+2] = Map[buffer[X]+512];

		X++;
	}
	g_free(buffer);
}

static void OneLineMapped_buf(guchar *buffer, guint Width, guchar * pixels,
			    guint bpp,guchar *Map)
{
	size_t result;
	guint X;
	
	X = 0;
	while (X < Width) {
		pixels[X * 3]   = Map[buffer[X]*3];
		pixels[X * 3+1] = Map[buffer[X]*3+1];
		pixels[X * 3+2] = Map[buffer[X]*3+2];

		X++;
	}
}

/* Shared library entry point */
GdkPixbuf *image_load(FILE * f)
{'fraction_non_alphanumeric': 0.1394902011015755, 'fraction_numerical': 0.024209043166389138, 'mean_word_length': 3.3329633740288567, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 37, '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': '724417', 'n_tokens_mistral': 3265, 'n_tokens_neox': 2953, 'n_words': 972}