<commit_before>/* ############################################################################
Name           : alg3.c
Company        : S. Vagionitis
Project        : Finding Clones
Programmer     : <NAME>
Revisor        : <NAME>
Description    : Functions for Algorithm 3.

Procedure                     Description
============================= =================================================


Globals        Type           Description
============== ============== =================================================


Programmer     Date           Action
============== ============== =================================================
<NAME>  10/06/2010     Creation

############################################################################ */

#define __ALG3_C__
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

#include "alg3.h"
#include "alcon2009.h"/*For save_ppm function*/

RGB **data2D_RGB;
CIELab **data2D_CIELAB;
unsigned int **gradient_Map;

int transform_1D_to_2D_RGB(unsigned char *image_data, int width, int height)
{
unsigned int i = 0, j = 0;

/* Allocate memory for subimage data*/
data2D_RGB = (RGB **)malloc(height * sizeof(RGB *));
if (data2D_RGB == NULL){
	printf("transform_1D_to_2D_RGB: Could not allocate %d bytes.\n", (height * sizeof(RGB *)));
	return FALSE;
	}
else{
	for (i=0;i<height;i++){
		data2D_RGB[i] = (RGB *)malloc(width * sizeof(RGB));
		if (data2D_RGB[i] == NULL){
			printf("transform_1D_to_2D_RGB: Could not allocate %d bytes for i=%d index.\n", (width * sizeof(RGB)), i);
			return FALSE;
			}
		else{
			for(j=0;j<width;j++){
				data2D_RGB[i][j].r = 0;
				data2D_RGB[i][j].g = 0;
				data2D_RGB[i][j].b = 0;
				}/*for j*/
			}
		}/*for i*/
	printf("transform_1D_to_2D_RGB: Allocated %d bytes.\n", (width * height * sizeof(RGB)));
	}


for (i=0;i<height;i++){
	for(j=0;j<width;j++){
		register unsigned int idx = (j + i*width)*3;
		data2D_RGB[i][j].r = image_data[idx + 0];
		data2D_RGB[i][j].g = image_data[idx + 1];
		data2D_RGB[i][j].b = image_data[idx + 2];
		}
	}

return TRUE;
}

int allocate_mem_data_CIELAB(int width, int height)
{
unsigned int i = 0, j = 0;

/* Allocate memory for CIELAB data*/
data2D_CIELAB = (CIELab **)malloc(height * sizeof(CIELab *));
if (data2D_CIELAB == NULL){
	printf("allocate_mem_CIELAB: Could not allocate %d bytes.\n", (height * sizeof(CIELab *)));
	return FALSE;
	}
else{
	for (i=0;i<height;i++){
		data2D_CIELAB[i] = (CIELab *)malloc(width * sizeof(CIELab));
		if (data2D_CIELAB[i] == NULL){
			printf("allocate_mem_CIELAB: Could not allocate %d bytes for i=%d index.\n", (width * sizeof(CIELab)), i);
			return FALSE;
			}
		else{
			for(j=0;j<width;j++){
				data2D_CIELAB[i][j].C = 0;
				data2D_CIELAB[i][j].L = 0.0;
				data2D_CIELAB[i][j].a = 0.0;
				data2D_CIELAB[i][j].b = 0.0;
				}/*for j*/
			}
		}/*for i*/
	printf("allocate_mem_CIELAB: Allocated %d bytes.\n", (width * height * sizeof(CIELab)));
	}


return TRUE;
}

/*
Source: http://inkscape.modevia.com/doxygen/html/classorg_1_1siox_1_1CieLab.php
*/
int RGB_to_CIELAB(RGB in, CIELab *out)
{
out->C = 0;
out->L = 0.0;
out->a = 0.0;
out->b = 0.0;

float fr = ((float)in.r) / 255.0;
float fg = ((float)in.g) / 255.0;
float fb = ((float)in.b) / 255.0;

if (fr > 0.04045)
	fr = (float) pow((fr + 0.055) / 1.055, 2.4);
else
	fr = fr / 12.92;

if (fg > 0.04045)
	fg = (float) pow((fg + 0.055) / 1.055, 2.4);
else
	fg = fg / 12.92;

if (fb > 0.04045)
	fb = (float) pow((fb + 0.055) / 1.055, 2.4);
else
	fb = fb / 12.92;


/*Use white = D65*/
const float x = fr * 0.4124 + fg * 0.3576 + fb * 0.1805;
const float y = fr * 0.2126 + fg * 0.7152 + fb * 0.0722;
const float z = fr * 0.0193 + fg * 0.1192 + fb * 0.9505;

float vx = x / 0.95047;
float vy = y;
float vz = z / 1.08883;


if (vx > 0.008856)
	vx = (float) pow(vx, 0.3333);
else
	vx = (7.787 * vx) + (16.0 / 116.0);

if (vy > 0.008856)
	vy = (float) pow(vy, 0.3333);
else
	vy = (7.787 * vy) + (16.0 / 116.0);

if (vz > 0.008856)
	vz = (float) pow(vz, 0.3333);
else
	vz = (7.787 * vz) + (16.0 / 116.0);

out->C = 0;
out->L = 116.0 * vy - 16.0;
out->a = 500.0 * (vx - vy);
out->b = 200.0 * (vy - vz);


return TRUE;
}
<commit_msg>	modified:   alg3.c<commit_after>/* ############################################################################
Name           : alg3.c
Company        : S. Vagionitis
Project        : Finding Clones
Programmer     : <NAME>
Revisor        : <NAME>
Description    : Functions for Algorithm 3.

Procedure                          Description
================================== ============================================
transform_1D_to_2D_RGB             Transform 1D data to a 2D for better 
                                   handling
allocate_mem_data_CIELAB           Allocate memory for 2D data in CIE L*a*b* 
                                   color space.
RGB_to_CIELAB                      Transform RGB values to CIE L*a*b* values.
convert_RGB_to_CIELAB              Convert all image RGB data to CIE L*a*b* 
                                   data
first_derivative_CIELAB            Calculate gradient values using kernel 
                                   functions(Sobel, Prewitt).
calculate_histogram_of_gradient    Calculate the histogram of gradient values.


Globals        Type                Description
============== =================== ============================================
data2D_RGB     RGB **              Image data in RGB color space.
data2D_CIELAB  CIELab **           Image data in CIE L*a*b* color space.
gradient_Map   unsigned int **     Gradient values.


Programmer     Date           Action
============== ============== =================================================
<NAME>  10/06/2010     Creation

############################################################################ */

#define __ALG3_C__
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

#include "alg3.h"
#include "alcon2009.h"/*For save_ppm function*/

RGB **data2D_RGB;
CIELab **data2D_CIELAB;
unsigned int **gradient_Map;

/* ############################################################################
Name           : transform_1D_to_2D_RGB
Description    : Transform 1D data to 2D for better handling and maybe faster.

Arguments             Type                Description
===================== =================== =====================================
image_data(IN)        unsigned char *     Image data.
width(IN)             int                 Width of image.
height(IN)            int                 Height of image.

Return Values                             Description
========================================= =====================================
TRUE                                      If all goes well.
FALSE                                     If memory allocation fails.

Globals               Type                Description
===================== =================== =====================================
data2D_RGB(OUT)       RGB **              Image data in RGB color space.

Locals                Type                Description
===================== =================== =====================================
i, j                  unsigned int        General purpose indexes.

############################################################################ */
int transform_1D_to_2D_RGB(unsigned char *image_data, int width, int height)
{
unsigned int i = 0, j = 0;

/* Allocate memory for subimage data*/
data2D_RGB = (RGB **)malloc(height * sizeof(RGB *));
if (data2D_RGB == NULL){
	printf("transform_1D_to_2D_RGB: Could not allocate %d bytes.\n", (height * sizeof(RGB *)));
	return FALSE;
	}
else{
	for (i=0;i<height;i++){
		data2D_RGB[i] = (RGB *)malloc(width * sizeof(RGB));
		if (data2D_RGB[i] == NULL){
			printf("transform_1D_to_2D_RGB: Could not allocate %d bytes for i=%d index.\n", (width * sizeof(RGB)), i);
			return FALSE;
			}
		else{
			for(j=0;j<width;j++){
				data2D_RGB[i][j].r = 0;
				data2D_RGB[i][j].g = 0;
				data2D_RGB[i][j].b = 0;
				}/*for j*/
			}
		}/*for i*/
	printf("transform_1D_to_2D_RGB: Allocated %d bytes.\n", (width * height * sizeof(RGB)));
	}


for (i=0;i<height;i++){
	for(j=0;j<width;j++){
		register unsigned int idx = (j + i*width)*3;
		data2D_RGB[i][j].r = image_data[idx + 0];
		data2D_RGB[i][j].g = image_data[idx + 1];
		data2D_RGB[i][j].b = image_data[idx + 2];
		}
	}

return TRUE;
}


/* ############################################################################
Name           : allocate_mem_data_CIELAB
Description    : Allocate memory for 2D data in CIE L*a*b* color space.

Arguments             Type                Description
===================== =================== =====================================
width(IN)             int                 Width of image.
height(IN)            int                 Height of image.

Return Values                             Description
========================================= =====================================
TRUE                                      If all goes well.
FALSE                                     If memory allocation fails.

Globals               Type                Description
===================== =================== =====================================
data2D_CIELAB(OUT)    CIELab **           Image data in CIE L*a*b* color space.

Locals                Type                Description
===================== =================== =====================================
i, j                  unsigned int        General purpose indexes.

############################################################################ */
int allocate_mem_data_CIELAB(int width, int height)
{
unsigned int i = 0, j = 0;

/* Allocate memory for CIELAB data*/
data2D_CIELAB = (CIELab **)malloc(height * sizeof(CIELab *));
if (data2D_CIELAB == NULL){
	printf("allocate_mem_CIELAB: Could not allocate %d bytes.\n", (height * sizeof(CIELab *)));
	return FALSE;
	}
else{
	for (i=0;i<height;i++){
		data2D_CIELAB[i] = (CIELab *)malloc(width * sizeof(CIELab));
		if (data2D_CIELAB[i] == NULL){
			printf("allocate_mem_CIELAB: Could not allocate %d bytes for i=%d index.\n", (width * sizeof(CIELab)), i);
			return FALSE;
			}
		else{
			for(j=0;j<width;j++){
				data2D_CIELAB[i][j].C = 0;
				data2D_CIELAB[i][j].L = 0.0;
				data2D_CIELAB[i][j].a = 0.0;
				data2D_CIELAB[i][j].b = 0.0;
				}/*for j*/
			}
		}/*for i*/
	printf("allocate_mem_CIELAB: Allocated %d bytes.\n", (width * height * sizeof(CIELab)));
	}


return TRUE;
}


/* ############################################################################
Name           : RGB_to_CIELAB
Description    : Transform RGB values to CIE L*a*b* values.

(Source: http://inkscape.modevia.com/doxygen/html/classorg_1_1siox_1_1CieLab.php)

Arguments             Type                Description
===================== =================== =====================================
in(IN)                RGB                 RGB value.
out(OUT)              CIELab *            CIE L*a*b* value.

Return Values                             Description
========================================= =====================================
TRUE                                      If all goes well.

Globals               Type                Description
===================== =================== =====================================

Locals                Type                Description
===================== =================== =====================================

############################################################################ */
int RGB_to_CIELAB(RGB in, CIELab *out)
{
out->C = 0;
out->L = 0.0;
out->a = 0.0;
out->b = 0.0;

float fr = ((float)in.r) / 255.0;
float fg = ((float)in.g) / 255.0;
float fb = ((float)in.b) / 255.0;

if (fr > 0.04045)
	fr = (float) pow((fr + 0.055) / 1.055, 2.4);
else
	fr = fr / 12.92;

if (fg > 0.04045)
	fg = (float) pow((fg + 0.055) / 1.055, 2.4);
else
	fg = fg / 12.92;

if (fb > 0.04045)
	fb = (float) pow((fb + 0.055) / 1.055, 2.4);
else
	fb = fb / 12.92;


/*Use white = D65*/
const float x = fr * 0.4124 + fg * 0.3576 + fb * 0.1805;
const float y = fr * 0.2126 + fg * 0.7152 + fb * 0.0722;
const float z = fr * 0.0193 + fg * 0.1192 + fb * 0.9505;

float vx = x / 0.95047;
float vy = y;
float vz = z / 1.08883;


if (vx > 0.008856)
	vx = (float) pow(vx, 0.3333);/*cube root*/
else
	vx = (7.787 * vx) + (16.0 / 116.0);

if (vy > 0.008856)
	vy = (float) pow(vy, 0.3333);
else
	vy = (7.787 * vy) + (16.0 / 116.0);

if (vz > 0.008856)
	vz = (float) pow(vz, 0.3333);
else
	vz = (7.787 * vz) + (16.0 / 116.0);

out->C = 0;
out->L = 116.0 * vy - 16.0;
out->a = 500.0 * (vx - vy);
out->b = 200.0 * (vy - vz);


return TRUE;
}

{'fraction_non_alphanumeric': 0.2934937470318189, 'fraction_numerical': 0.04788665505778059, 'mean_word_length': 2.814915458937198, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 15, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6967657', 'n_tokens_mistral': 4845, 'n_tokens_neox': 4165, 'n_words': 1028}