<commit_before>/* Copyright (C)2004 Landmark Graphics Corporation
 * Copyright (C)2005, 2006 Sun Microsystems, Inc.
 * Copyright (C)2009-2011 <NAME>
 *
 * This library is free software and may be redistributed and/or modified under
 * the terms of the wxWindows Library License, Version 3.1 or (at your option)
 * any later version.  The full license is in the LICENSE.txt file included
 * with this distribution.
 *
 * 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
 * wxWindows Library License for more details.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <errno.h>
#include "./bmp.h"
#include "./rrutil.h"
#include "./rrtimer.h"
#include "./turbojpeg.h"

#define _throw(op, err) {  \
	printf("ERROR in line %d while %s:\n%s\n", __LINE__, op, err);  goto bailout;}
#define _throwunix(m) _throw(m, strerror(errno))
#define _throwtj(m) _throw(m, tjGetErrorStr())
#define _throwbmp(m) _throw(m, bmpgeterr())

#define PAD(v, p) ((v+(p)-1)&(~((p)-1)))

enum {YUVENCODE=1, YUVDECODE};
int forcemmx=0, forcesse=0, forcesse2=0, forcesse3=0, fastupsample=0,
	decomponly=0, yuv=0, quiet=0, dotile=0, pf=BMP_BGR, bu=0,  useppm=0;
const int _ps[BMPPIXELFORMATS]={3, 4, 3, 4, 4, 4};
const int _flags[BMPPIXELFORMATS]={0, 0, TJ_BGR, TJ_BGR,
	TJ_BGR|TJ_ALPHAFIRST, TJ_ALPHAFIRST};
const int _rindex[BMPPIXELFORMATS]={0, 0, 2, 2, 3, 1};
const int _gindex[BMPPIXELFORMATS]={1, 1, 1, 1, 2, 2};
const int _bindex[BMPPIXELFORMATS]={2, 2, 0, 0, 1, 3};
const char *_pfname[]={"RGB", "RGBA", "BGR", "BGRA", "ABGR", "ARGB"};
const char *_subnamel[NUMSUBOPT]={"4:4:4", "4:2:2", "4:2:0", "GRAY"};
const char *_subnames[NUMSUBOPT]={"444", "422", "420", "GRAY"};
const int _hsf[NUMSUBOPT]={1, 2, 2, 1};
const int _vsf[NUMSUBOPT]={1, 1, 2, 1};

void printsigfig(double val, int figs)
{
	char format[80];
	double _l=log10(val);  int l;
	if(_l<0.)
	{
		l=(int)fabs(_l);
		sprintf(format, "%%%d.%df", figs+l+2, figs+l);
	}
	else
	{
		l=(int)_l+1;
		if(figs<=l) sprintf(format, "%%.0f");
		else sprintf(format, "%%%d.%df", figs+1, figs-l);
	}	
	printf(format, val);
}

// Decompression test
int decomptest(unsigned char *srcbuf, unsigned char **jpegbuf,
	unsigned long *comptilesize, unsigned char *rgbbuf, int w, int h,
	int jpegsub, int qual, char *filename, int tilesizex, int tilesizey)
{
	char tempstr[1024], qualstr[5]="\0";
	FILE *outfile=NULL;  tjhandle hnd=NULL;
	int flags=(forcemmx?TJ_FORCEMMX:0)|(forcesse?TJ_FORCESSE:0)
		|(forcesse2?TJ_FORCESSE2:0)|(forcesse3?TJ_FORCESSE3:0)
		|(fastupsample?TJ_FASTUPSAMPLE:0);
	int i, j, ITER, rgbbufalloc=0;
	double start, elapsed;
	int ps=_ps[pf];
	int pitch=w*ps;
	int hsf=_hsf[jpegsub], vsf=_vsf[jpegsub];
	int pw=PAD(w, hsf), ph=PAD(h, vsf);
	int cw=pw/hsf, ch=ph/vsf;
	int ypitch=PAD(pw, 4), uvpitch=PAD(cw, 4);
	int yuvsize=ypitch*ph + (jpegsub==TJ_GRAYSCALE? 0:uvpitch*ch*2);

	if(qual>0)
	{
		snprintf(qualstr, 5, "Q%d", qual);
		qualstr[4]=0;
	}

	flags |= _flags[pf];
	if(bu) flags |= TJ_BOTTOMUP;
	if(yuv==YUVDECODE) flags |= TJ_YUV;
	if((hnd=tjInitDecompress())==NULL)
		_throwtj("executing tjInitDecompress()");

	if(rgbbuf==NULL)
	{
		if((rgbbuf=(unsigned char *)malloc(max(yuvsize, pitch*h))) == NULL)
			_throwunix("allocating image buffer");
		rgbbufalloc=1;
	}
	memset(rgbbuf, 127, max(yuvsize, pitch*h));  // Grey image means decompressor did nothing

	if(tjDecompress(hnd, jpegbuf[0], comptilesize[0], rgbbuf, tilesizex, pitch,
		tilesizey, ps, flags)==-1)
		_throwtj("executing tjDecompress()");
	ITER=0;
	start=rrtime();
	do
	{
		int tilen=0;
		for(i=0; i<h; i+=tilesizey)
		{
			for(j=0; j<w; j+=tilesizex)
			{
				int tempw=min(tilesizex, w-j), temph=min(tilesizey, h-i);
				if(tjDecompress(hnd, jpegbuf[tilen], comptilesize[tilen],
					&rgbbuf[pitch*i+ps*j], tempw, pitch, temph, ps, flags)==-1)
					_throwtj("executing tjDecompress()");
				tilen++;
			}
		}
		ITER++;
	}	while((elapsed=rrtime()-start)<5.);
	if(tjDestroy(hnd)==-1) _throwtj("executing tjDestroy()");
	hnd=NULL;
	if(quiet)
	{
		printsigfig((double)(w*h)/1000000.*(double)ITER/elapsed, 4);
		printf("\n");
	}
	else
	{
		printf("D--> Frame rate:           %f fps\n", (double)ITER/elapsed);
		printf("     Dest. throughput:     %f Megapixels/sec\n",
			(double)(w*h)/1000000.*(double)ITER/elapsed);
	}
	if(yuv==YUVDECODE)
	{
		sprintf(tempstr, "%s_%s%s.yuv", filename, _subnames[jpegsub], qualstr);
		if((outfile=fopen(tempstr, "wb"))==NULL)
			_throwunix("opening YUV image for output");
		if(fwrite(rgbbuf, yuvsize, 1, outfile)!=1)
			_throwunix("writing YUV image");
		fclose(outfile);  outfile=NULL;
	}
	else
	{
		if(tilesizex==w && tilesizey==h)
			sprintf(tempstr, "%s_%s%s_full.%s", filename, _subnames[jpegsub],
				qualstr, useppm?"ppm":"bmp");
		else sprintf(tempstr, "%s_%s%s_%dx%d.%s", filename, _subnames[jpegsub],
			qualstr, tilesizex, tilesizey, useppm?"ppm":"bmp");
		if(savebmp(tempstr, rgbbuf, w, h, pf, pitch, bu)==-1)
			_throwbmp("saving bitmap");
		sprintf(strrchr(tempstr, '.'), "-err.%s", useppm?"ppm":"bmp");
		if(srcbuf)
		{
			if(!quiet)
				printf("Computing compression error and saving to %s.\n", tempstr);
			if(jpegsub==TJ_GRAYSCALE)
			{
				for(j=0; j<h; j++)
				{
					for(i=0; i<w*ps; i+=ps)
					{
						int y=(int)((double)srcbuf[w*ps*j+i+_rindex[pf]]*0.299
							+ (double)srcbuf[w*ps*j+i+_gindex[pf]]*0.587
							+ (double)srcbuf[w*ps*j+i+_bindex[pf]]*0.114 + 0.5);
						if(y>255) y=255;  if(y<0) y=0;
						rgbbuf[pitch*j+i+_rindex[pf]]=abs(rgbbuf[pitch*j+i+_rindex[pf]]-y);
						rgbbuf[pitch*j+i+_gindex[pf]]=abs(rgbbuf[pitch*j+i+_gindex[pf]]-y);
						rgbbuf[pitch*j+i+_bindex[pf]]=abs(rgbbuf[pitch*j+i+_bindex[pf]]-y);
					}
				}
			}		
			else
			{<commit_msg>Use same naming scheme for output files as v1.1<commit_after>/* Copyright (C)2004 Landmark Graphics Corporation
 * Copyright (C)2005, 2006 Sun Microsystems, Inc.
 * Copyright (C)2009-2011 <NAME>
 *
 * This library is free software and may be redistributed and/or modified under
 * the terms of the wxWindows Library License, Version 3.1 or (at your option)
 * any later version.  The full license is in the LICENSE.txt file included
 * with this distribution.
 *
 * 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
 * wxWindows Library License for more details.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <errno.h>
#include "./bmp.h"
#include "./rrutil.h"
#include "./rrtimer.h"
#include "./turbojpeg.h"

#define _throw(op, err) {  \
	printf("ERROR in line %d while %s:\n%s\n", __LINE__, op, err);  goto bailout;}
#define _throwunix(m) _throw(m, strerror(errno))
#define _throwtj(m) _throw(m, tjGetErrorStr())
#define _throwbmp(m) _throw(m, bmpgeterr())

#define PAD(v, p) ((v+(p)-1)&(~((p)-1)))

enum {YUVENCODE=1, YUVDECODE};
int forcemmx=0, forcesse=0, forcesse2=0, forcesse3=0, fastupsample=0,
	decomponly=0, yuv=0, quiet=0, dotile=0, pf=BMP_BGR, bu=0,  useppm=0;
const int _ps[BMPPIXELFORMATS]={3, 4, 3, 4, 4, 4};
const int _flags[BMPPIXELFORMATS]={0, 0, TJ_BGR, TJ_BGR,
	TJ_BGR|TJ_ALPHAFIRST, TJ_ALPHAFIRST};
const int _rindex[BMPPIXELFORMATS]={0, 0, 2, 2, 3, 1};
const int _gindex[BMPPIXELFORMATS]={1, 1, 1, 1, 2, 2};
const int _bindex[BMPPIXELFORMATS]={2, 2, 0, 0, 1, 3};
const char *_pfname[]={"RGB", "RGBA", "BGR", "BGRA", "ABGR", "ARGB"};
const char *_subnamel[NUMSUBOPT]={"4:4:4", "4:2:2", "4:2:0", "GRAY"};
const char *_subnames[NUMSUBOPT]={"444", "422", "420", "GRAY"};
const int _hsf[NUMSUBOPT]={1, 2, 2, 1};
const int _vsf[NUMSUBOPT]={1, 1, 2, 1};

void printsigfig(double val, int figs)
{
	char format[80];
	double _l=log10(val);  int l;
	if(_l<0.)
	{
		l=(int)fabs(_l);
		sprintf(format, "%%%d.%df", figs+l+2, figs+l);
	}
	else
	{
		l=(int)_l+1;
		if(figs<=l) sprintf(format, "%%.0f");
		else sprintf(format, "%%%d.%df", figs+1, figs-l);
	}	
	printf(format, val);
}

// Decompression test
int decomptest(unsigned char *srcbuf, unsigned char **jpegbuf,
	unsigned long *comptilesize, unsigned char *rgbbuf, int w, int h,
	int jpegsub, int qual, char *filename, int tilesizex, int tilesizey)
{
	char tempstr[1024], qualstr[5]="\0";
	FILE *outfile=NULL;  tjhandle hnd=NULL;
	int flags=(forcemmx?TJ_FORCEMMX:0)|(forcesse?TJ_FORCESSE:0)
		|(forcesse2?TJ_FORCESSE2:0)|(forcesse3?TJ_FORCESSE3:0)
		|(fastupsample?TJ_FASTUPSAMPLE:0);
	int i, j, ITER, rgbbufalloc=0;
	double start, elapsed;
	int ps=_ps[pf];
	int pitch=w*ps;
	int hsf=_hsf[jpegsub], vsf=_vsf[jpegsub];
	int pw=PAD(w, hsf), ph=PAD(h, vsf);
	int cw=pw/hsf, ch=ph/vsf;
	int ypitch=PAD(pw, 4), uvpitch=PAD(cw, 4);
	int yuvsize=ypitch*ph + (jpegsub==TJ_GRAYSCALE? 0:uvpitch*ch*2);

	if(qual>0)
	{
		snprintf(qualstr, 5, "Q%d", qual);
		qualstr[4]=0;
	}

	flags |= _flags[pf];
	if(bu) flags |= TJ_BOTTOMUP;
	if(yuv==YUVDECODE) flags |= TJ_YUV;
	if((hnd=tjInitDecompress())==NULL)
		_throwtj("executing tjInitDecompress()");

	if(rgbbuf==NULL)
	{
		if((rgbbuf=(unsigned char *)malloc(max(yuvsize, pitch*h))) == NULL)
			_throwunix("allocating image buffer");
		rgbbufalloc=1;
	}
	memset(rgbbuf, 127, max(yuvsize, pitch*h));  // Grey image means decompressor did nothing

	if(tjDecompress(hnd, jpegbuf[0], comptilesize[0], rgbbuf, tilesizex, pitch,
		tilesizey, ps, flags)==-1)
		_throwtj("executing tjDecompress()");
	ITER=0;
	start=rrtime();
	do
	{
		int tilen=0;
		for(i=0; i<h; i+=tilesizey)
		{
			for(j=0; j<w; j+=tilesizex)
			{
				int tempw=min(tilesizex, w-j), temph=min(tilesizey, h-i);
				if(tjDecompress(hnd, jpegbuf[tilen], comptilesize[tilen],
					&rgbbuf[pitch*i+ps*j], tempw, pitch, temph, ps, flags)==-1)
					_throwtj("executing tjDecompress()");
				tilen++;
			}
		}
		ITER++;
	}	while((elapsed=rrtime()-start)<5.);
	if(tjDestroy(hnd)==-1) _throwtj("executing tjDestroy()");
	hnd=NULL;
	if(quiet)
	{
		printsigfig((double)(w*h)/1000000.*(double)ITER/elapsed, 4);
		printf("\n");
	}
	else
	{
		printf("D--> Frame rate:           %f fps\n", (double)ITER/elapsed);
		printf("     Dest. throughput:     %f Megapixels/sec\n",
			(double)(w*h)/1000000.*(double)ITER/elapsed);
	}
	if(yuv==YUVDECODE)
	{
		sprintf(tempstr, "%s_%s%s.yuv", filename, _subnames[jpegsub], qualstr);
		if((outfile=fopen(tempstr, "wb"))==NULL)
			_throwunix("opening YUV image for output");
		if(fwrite(rgbbuf, yuvsize, 1, outfile)!=1)
			_throwunix("writing YUV image");
		fclose(outfile);  outfile=NULL;
	}
	else
	{
		if(tilesizex==w && tilesizey==h)
		{
			if(decomponly)
				sprintf(tempstr, "%s_full.%s", filename, useppm?"ppm":"bmp");
			else
				sprintf(tempstr, "%s_%s%s_full.%s", filename, _subnames[jpegsub],
					qualstr, useppm?"ppm":"bmp");
		}
		else sprintf(tempstr, "%s_%s%s_%dx%d.%s", filename, _subnames[jpegsub],
			qualstr, tilesizex, tilesizey, useppm?"ppm":"bmp");
		if(savebmp(tempstr, rgbbuf, w, h, pf, pitch, bu)==-1)
			_throwbmp("saving bitmap");
		sprintf(strrchr(tempstr, '.'), "-err.%s", useppm?"ppm":"bmp");
		if(srcbuf)
		{
			if(!quiet)
				printf("Computing compression error and saving to %s.\n", tempstr);
			if(jpegsub==TJ_GRAYSCALE)
			{
				for(j=0; j<h; j++)
				{
					for(i=0; i<w*ps; i+=ps)
					{
						int y=(int)((double)srcbuf[w*ps*j+i+_rindex[pf]]*0.299
							+ (double)srcbuf[w*ps*j+i+_gindex[pf]]*0.587
							+ (double)srcbuf[w*ps*j+i+_bindex[pf]]*0.114 + 0.5);
						if(y>255) y=255;  if(y<0) y=0;
						rgbbuf[pitch*j+i+_rindex[pf]]=abs(rgbbuf[pitch*j+i+_rindex[pf]]-y);
						rgbbuf[pitch*j+i+_gindex[pf]]=abs(rgbbuf[pitch*j+i+_gindex[pf]]-y);
						rgbbuf[pitch*j+i+_bindex[pf]]=abs(rgbbuf[pitch*j+i+_bindex[pf]]-y);
					}
				}
			}		
			else
			{
{'fraction_non_alphanumeric': 0.19290202071659027, 'fraction_numerical': 0.030565461029037188, 'mean_word_length': 4.9340050377833755, 'pattern_counts': {'":': 7, '<': 31, '<?xml version=': 0, '>': 21, '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': '2668769', 'n_tokens_mistral': 5797, 'n_tokens_neox': 5203, 'n_words': 1034}