Alien-XGBoost
view release on metacpan or search on metacpan
xgboost/cub/experimental/histogram_compare.cu view on Meta::CPAN
bitsperpixel = fgetc(fptr);
imagedescriptor = fgetc(fptr);
}
void Display (FILE *fptr)
{
fprintf(fptr, "ID length: %d\n", idlength);
fprintf(fptr, "Color map type: %d\n", colormaptype);
fprintf(fptr, "Image type: %d\n", datatypecode);
fprintf(fptr, "Color map offset: %d\n", colormaporigin);
fprintf(fptr, "Color map length: %d\n", colormaplength);
fprintf(fptr, "Color map depth: %d\n", colormapdepth);
fprintf(fptr, "X origin: %d\n", x_origin);
fprintf(fptr, "Y origin: %d\n", y_origin);
fprintf(fptr, "Width: %d\n", width);
fprintf(fptr, "Height: %d\n", height);
fprintf(fptr, "Bits per pixel: %d\n", bitsperpixel);
fprintf(fptr, "Descriptor: %d\n", imagedescriptor);
}
};
/**
* Decode image byte data into pixel
*/
void ParseTgaPixel(uchar4 &pixel, unsigned char *tga_pixel, int bytes)
{
if (bytes == 4)
{
pixel.x = tga_pixel[2];
pixel.y = tga_pixel[1];
pixel.z = tga_pixel[0];
pixel.w = tga_pixel[3];
}
else if (bytes == 3)
{
pixel.x = tga_pixel[2];
pixel.y = tga_pixel[1];
pixel.z = tga_pixel[0];
pixel.w = 0;
}
else if (bytes == 2)
{
pixel.x = (tga_pixel[1] & 0x7c) << 1;
pixel.y = ((tga_pixel[1] & 0x03) << 6) | ((tga_pixel[0] & 0xe0) >> 2);
pixel.z = (tga_pixel[0] & 0x1f) << 3;
pixel.w = (tga_pixel[1] & 0x80);
}
}
/**
* Reads a .tga image file
*/
void ReadTga(uchar4* &pixels, int &width, int &height, const char *filename)
{
// Open the file
FILE *fptr;
if ((fptr = fopen(filename, "rb")) == NULL)
{
fprintf(stderr, "File open failed\n");
exit(-1);
}
// Parse header
TgaHeader header;
header.Parse(fptr);
// header.Display(stdout);
width = header.width;
height = header.height;
// Verify compatibility
if (header.datatypecode != 2 && header.datatypecode != 10)
{
fprintf(stderr, "Can only handle image type 2 and 10\n");
exit(-1);
}
if (header.bitsperpixel != 16 && header.bitsperpixel != 24 && header.bitsperpixel != 32)
{
fprintf(stderr, "Can only handle pixel depths of 16, 24, and 32\n");
exit(-1);
}
if (header.colormaptype != 0 && header.colormaptype != 1)
{
fprintf(stderr, "Can only handle color map types of 0 and 1\n");
exit(-1);
}
// Skip unnecessary header info
int skip_bytes = header.idlength + (header.colormaptype * header.colormaplength);
fseek(fptr, skip_bytes, SEEK_CUR);
// Read the image
int pixel_bytes = header.bitsperpixel / 8;
// Allocate and initialize pixel data
size_t image_bytes = width * height * sizeof(uchar4);
if ((pixels == NULL) && ((pixels = (uchar4*) malloc(image_bytes)) == NULL))
{
fprintf(stderr, "malloc of image failed\n");
exit(-1);
}
memset(pixels, 0, image_bytes);
// Parse pixels
unsigned char tga_pixel[5];
int current_pixel = 0;
while (current_pixel < header.width * header.height)
{
if (header.datatypecode == 2)
{
// Uncompressed
if (fread(tga_pixel, 1, pixel_bytes, fptr) != pixel_bytes)
{
fprintf(stderr, "Unexpected end of file at pixel %d (uncompressed)\n", current_pixel);
exit(-1);
}
ParseTgaPixel(pixels[current_pixel], tga_pixel, pixel_bytes);
current_pixel++;
}
else if (header.datatypecode == 10)
{
// Compressed
if (fread(tga_pixel, 1, pixel_bytes + 1, fptr) != pixel_bytes + 1)
{
fprintf(stderr, "Unexpected end of file at pixel %d (compressed)\n", current_pixel);
exit(-1);
}
int run_length = tga_pixel[0] & 0x7f;
ParseTgaPixel(pixels[current_pixel], &(tga_pixel[1]), pixel_bytes);
current_pixel++;
if (tga_pixel[0] & 0x80)
{
// RLE chunk
for (int i = 0; i < run_length; i++)
{
ParseTgaPixel(pixels[current_pixel], &(tga_pixel[1]), pixel_bytes);
current_pixel++;
}
}
else
{
// Normal chunk
for (int i = 0; i < run_length; i++)
{
if (fread(tga_pixel, 1, pixel_bytes, fptr) != pixel_bytes)
{
fprintf(stderr, "Unexpected end of file at pixel %d (normal)\n", current_pixel);
exit(-1);
}
ParseTgaPixel(pixels[current_pixel], tga_pixel, pixel_bytes);
current_pixel++;
}
}
}
}
// Close file
fclose(fptr);
}
//---------------------------------------------------------------------
// Random image generation
//---------------------------------------------------------------------
/**
* Generate a random image with specified entropy
*/
void GenerateRandomImage(uchar4* &pixels, int width, int height, int entropy_reduction)
{
int num_pixels = width * height;
size_t image_bytes = num_pixels * sizeof(uchar4);
if ((pixels == NULL) && ((pixels = (uchar4*) malloc(image_bytes)) == NULL))
{
fprintf(stderr, "malloc of image failed\n");
exit(-1);
}
for (int i = 0; i < num_pixels; ++i)
{
RandomBits(pixels[i].x, entropy_reduction);
RandomBits(pixels[i].y, entropy_reduction);
RandomBits(pixels[i].z, entropy_reduction);
RandomBits(pixels[i].w, entropy_reduction);
}
}
//---------------------------------------------------------------------
// Histogram verification
//---------------------------------------------------------------------
// Decode float4 pixel into bins
template <int NUM_BINS, int ACTIVE_CHANNELS>
void DecodePixelGold(float4 pixel, unsigned int (&bins)[ACTIVE_CHANNELS])
{
float* samples = reinterpret_cast<float*>(&pixel);
for (int CHANNEL = 0; CHANNEL < ACTIVE_CHANNELS; ++CHANNEL)
bins[CHANNEL] = (unsigned int) (samples[CHANNEL] * float(NUM_BINS));
}
// Decode uchar4 pixel into bins
template <int NUM_BINS, int ACTIVE_CHANNELS>
void DecodePixelGold(uchar4 pixel, unsigned int (&bins)[ACTIVE_CHANNELS])
{
unsigned char* samples = reinterpret_cast<unsigned char*>(&pixel);
for (int CHANNEL = 0; CHANNEL < ACTIVE_CHANNELS; ++CHANNEL)
bins[CHANNEL] = (unsigned int) (samples[CHANNEL]);
}
// Decode uchar1 pixel into bins
template <int NUM_BINS, int ACTIVE_CHANNELS>
void DecodePixelGold(uchar1 pixel, unsigned int (&bins)[ACTIVE_CHANNELS])
{
bins[0] = (unsigned int) pixel.x;
}
// Compute reference histogram. Specialized for uchar4
template <
int ACTIVE_CHANNELS,
int NUM_BINS,
typename PixelType>
void HistogramGold(PixelType *image, int width, int height, unsigned int* hist)
{
memset(hist, 0, ACTIVE_CHANNELS * NUM_BINS * sizeof(unsigned int));
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
PixelType pixel = image[i + j * width];
( run in 0.672 second using v1.01-cache-2.11-cpan-e1769b4cff6 )