<commit_before>/*
 * Copyright 2015 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "SkCodecPriv.h"
#include "SkColorPriv.h"
#include "SkSwizzler.h"
#include "SkTemplates.h"
#include "SkUtils.h"

SkSwizzler::ResultAlpha SkSwizzler::GetResult(uint8_t zeroAlpha,
                                              uint8_t maxAlpha) {
    // In the transparent case, this returns 0x0000
    // In the opaque case, this returns 0xFFFF
    // If the row is neither transparent nor opaque, returns something else
    return (((uint16_t) maxAlpha) << 8) | zeroAlpha;
}

// kBit
// These routines exclusively choose between white and black

#define GRAYSCALE_BLACK 0
#define GRAYSCALE_WHITE 0xFF

static SkSwizzler::ResultAlpha swizzle_bit_to_grayscale(
        void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int width,
        int /*bitsPerPixel*/, const SkPMColor* /*ctable*/) {
    uint8_t* SK_RESTRICT dst = (uint8_t*) dstRow;

    // Determine how many full bytes are in the row
    int bytesInRow = width >> 3;
    int i;
    for (i = 0; i < bytesInRow; i++) {
        U8CPU currByte = src[i];
        for (int j = 0; j < 8; j++) {
            dst[j] = ((currByte >> (7 - j)) & 1) ? GRAYSCALE_WHITE : GRAYSCALE_BLACK;
        }
        dst += 8;
    }

    // Finish the remaining bits
    width &= 7;
    U8CPU currByte = src[i];
    for (int j = 0; j < width; j++) {
        dst[j] = ((currByte >> 7) & 1) ? GRAYSCALE_WHITE : GRAYSCALE_BLACK;
        currByte <<= 1;
    }
    return SkSwizzler::kOpaque_ResultAlpha;
}

#undef GRAYSCALE_BLACK
#undef GRAYSCALE_WHITE

static SkSwizzler::ResultAlpha swizzle_bit_to_index(
        void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int width,
        int /*bitsPerPixel*/, const SkPMColor* /*ctable*/) {
    uint8_t* SK_RESTRICT dst = (uint8_t*) dstRow;

    // Determine how many full bytes are in the row
    int bytesInRow = width >> 3;
    int i;
    for (i = 0; i < bytesInRow; i++) {
        U8CPU currByte = src[i];
        for (int j = 0; j < 8; j++) {
            dst[j] = (currByte >> (7 - j)) & 1;
        }
        dst += 8;
    }

    // Finish the remaining bits
    width &= 7;
    U8CPU currByte = src[i];
    for (int j = 0; j < width; j++) {
        dst[j] = ((currByte >> 7) & 1);
        currByte <<= 1;
    }
    return SkSwizzler::kOpaque_ResultAlpha;
}

static SkSwizzler::ResultAlpha swizzle_bit_to_n32(
        void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int width,
        int /*bitsPerPixel*/, const SkPMColor* /*ctable*/) {
    SkPMColor* SK_RESTRICT dst = (SkPMColor*) dstRow;

    // Determine how many full bytes are in the row
    int bytesInRow = width >> 3;
    int i;
    for (i = 0; i < bytesInRow; i++) {
        U8CPU currByte = src[i];
        for (int j = 0; j < 8; j++) {
            dst[j] = ((currByte >> (7 - j)) & 1) ? SK_ColorWHITE : SK_ColorBLACK;
        }
        dst += 8;
    }

    // Finish the remaining bits
    width &= 7;
    U8CPU currByte = src[i];
    for (int j = 0; j < width; j++) {
        dst[j] = ((currByte >> 7) & 1) ? SK_ColorWHITE : SK_ColorBLACK;
        currByte <<= 1;
    }
    return SkSwizzler::kOpaque_ResultAlpha;
}

// kIndex1, kIndex2, kIndex4

static SkSwizzler::ResultAlpha swizzle_small_index_to_index(
        void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int width,
        int bitsPerPixel, const SkPMColor ctable[]) {

    uint8_t* SK_RESTRICT dst = (uint8_t*) dstRow;
    INIT_RESULT_ALPHA;
    const uint32_t pixelsPerByte = 8 / bitsPerPixel;
    const size_t rowBytes = compute_row_bytes_ppb(width, pixelsPerByte);<commit_msg>Fix ASAN error for wbmp swizzles<commit_after>/*
 * Copyright 2015 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "SkCodecPriv.h"
#include "SkColorPriv.h"
#include "SkSwizzler.h"
#include "SkTemplates.h"
#include "SkUtils.h"

SkSwizzler::ResultAlpha SkSwizzler::GetResult(uint8_t zeroAlpha,
                                              uint8_t maxAlpha) {
    // In the transparent case, this returns 0x0000
    // In the opaque case, this returns 0xFFFF
    // If the row is neither transparent nor opaque, returns something else
    return (((uint16_t) maxAlpha) << 8) | zeroAlpha;
}

// kBit
// These routines exclusively choose between white and black

#define GRAYSCALE_BLACK 0
#define GRAYSCALE_WHITE 0xFF

static SkSwizzler::ResultAlpha swizzle_bit_to_grayscale(
        void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int width,
        int /*bitsPerPixel*/, const SkPMColor* /*ctable*/) {
    uint8_t* SK_RESTRICT dst = (uint8_t*) dstRow;

    // Determine how many full bytes are in the row
    int bytesInRow = width >> 3;
    int i;
    for (i = 0; i < bytesInRow; i++) {
        U8CPU currByte = src[i];
        for (int j = 0; j < 8; j++) {
            dst[j] = ((currByte >> (7 - j)) & 1) ? GRAYSCALE_WHITE : GRAYSCALE_BLACK;
        }
        dst += 8;
    }

    // Finish the remaining bits
    width &= 7;
    if (width > 0) {
        U8CPU currByte = src[i];
        for (int j = 0; j < width; j++) {
            dst[j] = ((currByte >> 7) & 1) ? GRAYSCALE_WHITE : GRAYSCALE_BLACK;
            currByte <<= 1;
        }
    }
    return SkSwizzler::kOpaque_ResultAlpha;
}

#undef GRAYSCALE_BLACK
#undef GRAYSCALE_WHITE

static SkSwizzler::ResultAlpha swizzle_bit_to_index(
        void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int width,
        int /*bitsPerPixel*/, const SkPMColor* /*ctable*/) {
    uint8_t* SK_RESTRICT dst = (uint8_t*) dstRow;

    // Determine how many full bytes are in the row
    int bytesInRow = width >> 3;
    int i;
    for (i = 0; i < bytesInRow; i++) {
        U8CPU currByte = src[i];
        for (int j = 0; j < 8; j++) {
            dst[j] = (currByte >> (7 - j)) & 1;
        }
        dst += 8;
    }

    // Finish the remaining bits
    width &= 7;
    if (width > 0) {
        U8CPU currByte = src[i];
        for (int j = 0; j < width; j++) {
            dst[j] = ((currByte >> 7) & 1);
            currByte <<= 1;
        }
    }
    return SkSwizzler::kOpaque_ResultAlpha;
}

static SkSwizzler::ResultAlpha swizzle_bit_to_n32(
        void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int width,
        int /*bitsPerPixel*/, const SkPMColor* /*ctable*/) {
    SkPMColor* SK_RESTRICT dst = (SkPMColor*) dstRow;

    // Determine how many full bytes are in the row
    int bytesInRow = width >> 3;
    int i;
    for (i = 0; i < bytesInRow; i++) {
        U8CPU currByte = src[i];
        for (int j = 0; j < 8; j++) {
            dst[j] = ((currByte >> (7 - j)) & 1) ? SK_ColorWHITE : SK_ColorBLACK;
        }
        dst += 8;
    }

    // Finish the remaining bits
    width &= 7;
    if (width > 0) {
        U8CPU currByte = src[i];
        for (int j = 0; j < width; j++) {
            dst[j] = ((currByte >> 7) & 1) ? SK_ColorWHITE : SK_ColorBLACK;
            currByte <<= 1;
        }
    }
    return SkSwizzler::kOpaque_ResultAlpha;
}

// kIndex1, kIndex2, kIndex4

static SkSwizzler::ResultAlpha swizzle_small_index_to_index(
        void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int width,
        int bitsPerPixel, const SkPMColor ctable[]) {

    uint8_t* SK_RESTRICT dst = (uint8_t*) dstRow;
    INIT_RESULT_ALPHA;
    const uint32_t pixelsPerByte = 8 / bitsPerPixel;
    const size_t rowBytes = compute_row_bytes_ppb(width, pixelsPerByte);
{'fraction_non_alphanumeric': 0.11985664985399522, 'fraction_numerical': 0.020838863817361296, 'mean_word_length': 2.4375, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4117518', 'n_tokens_mistral': 2802, 'n_tokens_neox': 2658, 'n_words': 781}