<commit_before>//******************************************************************************
///
/// @file base/image/targa.cpp
///
/// Implementation of Targa (TGA) image file handling.
///
/// @copyright
/// @parblock
///
/// Persistence of Vision Ray Tracer ('POV-Ray') version 3.8.
/// Copyright 1991-2019 Persistence of Vision Raytracer Pty. Ltd.
///
/// POV-Ray is free software: you can redistribute it and/or modify
/// it under the terms of the GNU Affero General Public License as
/// published by the Free Software Foundation, either version 3 of the
/// License, or (at your option) any later version.
///
/// POV-Ray 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 Affero General Public License for more details.
///
/// You should have received a copy of the GNU Affero General Public License
/// along with this program.  If not, see <http://www.gnu.org/licenses/>.
///
/// ----------------------------------------------------------------------------
///
/// POV-Ray is based on the popular DKB raytracer version 2.12.
/// DKBTrace was originally written by <NAME>.
/// DKBTrace Ver 2.0-2.12 were written by <NAME> & <NAME>.
///
/// @endparblock
///
//******************************************************************************

/****************************************************************************
*
*  Explanation:
*
*  May 1994 : Support for 24-bit RLE Targa output files added: <NAME>
*             and <NAME>.
*
*  Jul 1994 : Resume trace support and minor algorithm fix (one more still
*             needed, see comments in Write_Targa_Line); resume will force
*             Targa format to match the original trace format -- the T or C
*             format flag is adjusted as necessary: <NAME>,
*
*  Jun 1995 : Added support for 32-bit Targa input and output files.
*             The alpha channel has a value of 0 for 100% transparency
*             and a value of 255 for 0% transparency. [DB]
*
*  Sep 2010 : Added metadata in Write, changing targa version from 1.0 to 2.0
*
*****************************************************************************/

// Unit header file must be the first file included within POV-Ray *.cpp files (pulls in config)
#include "base/image/targa.h"

// C++ variants of C standard header files
//  (none at the moment)

// C++ standard header files
#include <string>
#include <vector>

// POV-Ray header files (base module)
#include "base/fileinputoutput.h"
#include "base/types.h"
#include "base/image/colourspace.h"
#include "base/image/encoding.h"
#include "base/image/image.h"
#include "base/image/metadata.h"

// this must be the last file included
#include "base/povdebug.h"

namespace pov_base
{

namespace Targa
{

using std::vector;

struct pix final
{
    unsigned int b;
    unsigned int g;
    unsigned int r;
    unsigned int a;
};
using Pixel = pix;

typedef char Targa_footer[26];
#define FOO_EXT_OFF 0
#define FOO_SIG_OFF 8
#define FOO_DOT_OFF 24
#define FOO_NUL_OFF 25

typedef char Targa_extension[495];
#define EXT_SIZE_OFF 0
#define EXT_SIZE_DIM 2
#define EXT_COMMENT_OFF 43
#define EXT_COMMENT_DIM 81
#define EXT_COMMENT_COUNT 4
#define EXT_DATE_OFF 367
#define EXT_DATE_DIM 12
#define EXT_SOFT_OFF 426
#define EXT_SOFT_DIM 41
#define EXT_VERSION_OFF 467
#define EXT_VERSION_DIM 3
#define EXT_SCAN_OFF 490
#define EXT_SCAN_DIM 4
#define EXT_ALPHA_OFF 494
#define EXT_ALPHA_DIM 1
#define EXT_GAMMA_OFF 478
#define EXT_PIXRATIO_OFF 474

static Pixel *GetPix (const Image *image, int x, int y, Pixel *pixel, const GammaCurvePtr& gamma, DitherStrategy& dither, bool premul)
{
    GetEncodedRGBAValue (image, x, y, gamma, 255, pixel->r, pixel->g, pixel->b, pixel->a, dither, premul);
    return (pixel);
}

static void PutPix (vector<unsigned char>& line, const pix *pixel, bool opaque)
{
    line.push_back (pixel->b);
    line.push_back (pixel->g);
    line.push_back (pixel->r);
    if (!opaque)
        line.push_back (pixel->a);
}

void Write (OStream *file, const Image *image, const ImageWriteOptions& options)
{
    pix                     current;
    pix                     next;
    bool                    opaque = options.AlphaIsEnabled();
    bool                    compress = (options.compression > 0);
    vector<unsigned char>   header;
    vector<unsigned char>   line;
    GammaCurvePtr           gamma;
    Metadata                meta;
    DitherStrategy&         dither = *options.ditherStrategy;

    // If the user does not specify gamma, we're defaulting to sRGB.
    gamma = options.GetTranscodingGammaCurve(SRGBGammaCurve::Get());

    // TODO ALPHA - check if TGA should really keep presuming non-premultiplied alpha
    // We presume non-premultiplied alpha, unless the user overrides
    // (e.g. to handle a non-compliant file).
    bool premul = options.AlphaIsPremultiplied(false);

    header.reserve (18);
    header.push_back (0);
    header.push_back (0);

    // byte 2 - targa file type (compressed/uncompressed)
    header.push_back (compress ? 10 : 2);

    // Byte 3 - Index of first color map entry LSB
    // Byte 4 - Index of first color map entry MSB
    // Byte 5 - Color map length LSB
    // Byte 6 - Color map legth MSB
    // Byte 7 - Color map size
    // x origin set to "First_Column" - Bytes 8, 9
    // y origin set to "First_Line"   - Bytes 10, 11
    for (int i = 0; i < 9; i++)<commit_msg>targa transparency was not working due to inversed logic.<commit_after>//******************************************************************************
///
/// @file base/image/targa.cpp
///
/// Implementation of Targa (TGA) image file handling.
///
/// @copyright
/// @parblock
///
/// Persistence of Vision Ray Tracer ('POV-Ray') version 3.8.
/// Copyright 1991-2019 Persistence of Vision Raytracer Pty. Ltd.
///
/// POV-Ray is free software: you can redistribute it and/or modify
/// it under the terms of the GNU Affero General Public License as
/// published by the Free Software Foundation, either version 3 of the
/// License, or (at your option) any later version.
///
/// POV-Ray 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 Affero General Public License for more details.
///
/// You should have received a copy of the GNU Affero General Public License
/// along with this program.  If not, see <http://www.gnu.org/licenses/>.
///
/// ----------------------------------------------------------------------------
///
/// POV-Ray is based on the popular DKB raytracer version 2.12.
/// DKBTrace was originally written by <NAME>.
/// DKBTrace Ver 2.0-2.12 were written by <NAME> & <NAME>.
///
/// @endparblock
///
//******************************************************************************

/****************************************************************************
*
*  Explanation:
*
*  May 1994 : Support for 24-bit RLE Targa output files added: <NAME>
*             and <NAME>.
*
*  Jul 1994 : Resume trace support and minor algorithm fix (one more still
*             needed, see comments in Write_Targa_Line); resume will force
*             Targa format to match the original trace format -- the T or C
*             format flag is adjusted as necessary: <NAME>,
*
*  Jun 1995 : Added support for 32-bit Targa input and output files.
*             The alpha channel has a value of 0 for 100% transparency
*             and a value of 255 for 0% transparency. [DB]
*
*  Sep 2010 : Added metadata in Write, changing targa version from 1.0 to 2.0
*
*****************************************************************************/

// Unit header file must be the first file included within POV-Ray *.cpp files (pulls in config)
#include "base/image/targa.h"

// C++ variants of C standard header files
//  (none at the moment)

// C++ standard header files
#include <string>
#include <vector>

// POV-Ray header files (base module)
#include "base/fileinputoutput.h"
#include "base/types.h"
#include "base/image/colourspace.h"
#include "base/image/encoding.h"
#include "base/image/image.h"
#include "base/image/metadata.h"

// this must be the last file included
#include "base/povdebug.h"

namespace pov_base
{

namespace Targa
{

using std::vector;

struct pix final
{
    unsigned int b;
    unsigned int g;
    unsigned int r;
    unsigned int a;
};
using Pixel = pix;

typedef char Targa_footer[26];
#define FOO_EXT_OFF 0
#define FOO_SIG_OFF 8
#define FOO_DOT_OFF 24
#define FOO_NUL_OFF 25

typedef char Targa_extension[495];
#define EXT_SIZE_OFF 0
#define EXT_SIZE_DIM 2
#define EXT_COMMENT_OFF 43
#define EXT_COMMENT_DIM 81
#define EXT_COMMENT_COUNT 4
#define EXT_DATE_OFF 367
#define EXT_DATE_DIM 12
#define EXT_SOFT_OFF 426
#define EXT_SOFT_DIM 41
#define EXT_VERSION_OFF 467
#define EXT_VERSION_DIM 3
#define EXT_SCAN_OFF 490
#define EXT_SCAN_DIM 4
#define EXT_ALPHA_OFF 494
#define EXT_ALPHA_DIM 1
#define EXT_GAMMA_OFF 478
#define EXT_PIXRATIO_OFF 474

static Pixel *GetPix (const Image *image, int x, int y, Pixel *pixel, const GammaCurvePtr& gamma, DitherStrategy& dither, bool premul)
{
    GetEncodedRGBAValue (image, x, y, gamma, 255, pixel->r, pixel->g, pixel->b, pixel->a, dither, premul);
    return (pixel);
}

static void PutPix (vector<unsigned char>& line, const pix *pixel, bool opaque)
{
    line.push_back (pixel->b);
    line.push_back (pixel->g);
    line.push_back (pixel->r);
    if (!opaque)
        line.push_back (pixel->a);
}

void Write (OStream *file, const Image *image, const ImageWriteOptions& options)
{
    pix                     current;
    pix                     next;
    bool                    opaque = !options.AlphaIsEnabled();
    bool                    compress = (options.compression > 0);
    vector<unsigned char>   header;
    vector<unsigned char>   line;
    GammaCurvePtr           gamma;
    Metadata                meta;
    DitherStrategy&         dither = *options.ditherStrategy;

    // If the user does not specify gamma, we're defaulting to sRGB.
    gamma = options.GetTranscodingGammaCurve(SRGBGammaCurve::Get());

    // TODO ALPHA - check if TGA should really keep presuming non-premultiplied alpha
    // We presume non-premultiplied alpha, unless the user overrides
    // (e.g. to handle a non-compliant file).
    bool premul = options.AlphaIsPremultiplied(false);

    header.reserve (18);
    header.push_back (0);
    header.push_back (0);

    // byte 2 - targa file type (compressed/uncompressed)
    header.push_back (compress ? 10 : 2);

    // Byte 3 - Index of first color map entry LSB
    // Byte 4 - Index of first color map entry MSB
    // Byte 5 - Color map length LSB
    // Byte 6 - Color map legth MSB
    // Byte 7 - Color map size
    // x origin set to "First_Column" - Bytes 8, 9
    // y origin set to "First_Line"   - Bytes 10, 11
    for (int i = 0; i < 9; i++)
{'fraction_non_alphanumeric': 0.16749931873921337, 'fraction_numerical': 0.02216368425833409, 'mean_word_length': 2.8959660297239913, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1753318', 'n_tokens_mistral': 3560, 'n_tokens_neox': 3240, 'n_words': 1225}