static int b2026_barcode_ascii_equals_ignore_case(const char *left, const char *right) {
    unsigned char lch;
    unsigned char rch;
    if (left == NULL || right == NULL) return 0;
    while (*left != '\0' && *right != '\0') {
        lch = (unsigned char)*left++;
        rch = (unsigned char)*right++;
        if (lch >= 'A' && lch <= 'Z') lch = (unsigned char)(lch - 'A' + 'a');
        if (rch >= 'A' && rch <= 'Z') rch = (unsigned char)(rch - 'A' + 'a');
        if (lch != rch) return 0;
    }
    return *left == '\0' && *right == '\0';
}

static int b2026_createimage_barcode_fill_target(const char *target_name, int width, int height) {
    int fill_present[4] = {1, 1, 1, 1};
    int fill_types[4] = {0, 0, 0, 0};
    double fill_numbers[4] = {255.0, 255.0, 255.0, 255.0};
    if (width <= 0 || height <= 0) return 0;
    screen_array(target_name, width, height, NULL, 4, fill_present, fill_types, fill_numbers, NULL, 1, 255.0, 0, 0);
    return 1;
}

static void b2026_createimage_barcode_draw_rect(ArrayVariable *array_var, int width, int height, int x0, int y0, int x1, int y1, double r, double g, double b, double a) {
    int x;
    int y;
    if (array_var == NULL) return;
    if (x0 < 0) x0 = 0;
    if (y0 < 0) y0 = 0;
    if (x1 > width) x1 = width;
    if (y1 > height) y1 = height;
    for (y = y0; y < y1; ++y) {
        for (x = x0; x < x1; ++x) {
            long long pixel = ((long long)y * (long long)width + (long long)x) * 4LL;
            int base = option_base + (int)pixel;
            set_b2026_number(&array_var->data[base + 0], r);
            set_b2026_number(&array_var->data[base + 1], g);
            set_b2026_number(&array_var->data[base + 2], b);
            set_b2026_number(&array_var->data[base + 3], a);
        }
    }
}

static int b2026_createimage_barcode_code39_pattern(char ch, const char **pattern_out) {
    switch (ch) {
        case '0': *pattern_out = "nnnwwnwnn"; return 1;
        case '1': *pattern_out = "wnnwnnnnw"; return 1;
        case '2': *pattern_out = "nnwwnnnnw"; return 1;
        case '3': *pattern_out = "wnwwnnnnn"; return 1;
        case '4': *pattern_out = "nnnwwnnnw"; return 1;
        case '5': *pattern_out = "wnnwwnnnn"; return 1;
        case '6': *pattern_out = "nnwwwnnnn"; return 1;
        case '7': *pattern_out = "nnnwnnwnw"; return 1;
        case '8': *pattern_out = "wnnwnnwnn"; return 1;
        case '9': *pattern_out = "nnwwnnwnn"; return 1;
        case 'A': *pattern_out = "wnnnnwnnw"; return 1;
        case 'B': *pattern_out = "nnwnnwnnw"; return 1;
        case 'C': *pattern_out = "wnwnnwnnn"; return 1;
        case 'D': *pattern_out = "nnnnwwnnw"; return 1;
        case 'E': *pattern_out = "wnnnwwnnn"; return 1;
        case 'F': *pattern_out = "nnwnwwnnn"; return 1;
        case 'G': *pattern_out = "nnnnnwwnw"; return 1;
        case 'H': *pattern_out = "wnnnnwwnn"; return 1;
        case 'I': *pattern_out = "nnwnnwwnn"; return 1;
        case 'J': *pattern_out = "nnnnwwwnn"; return 1;
        case 'K': *pattern_out = "wnnnnnnww"; return 1;
        case 'L': *pattern_out = "nnwnnnnww"; return 1;
        case 'M': *pattern_out = "wnwnnnnwn"; return 1;
        case 'N': *pattern_out = "nnnnwnnww"; return 1;
        case 'O': *pattern_out = "wnnnwnnwn"; return 1;
        case 'P': *pattern_out = "nnwnwnnwn"; return 1;
        case 'Q': *pattern_out = "nnnnnnwww"; return 1;
        case 'R': *pattern_out = "wnnnnnwwn"; return 1;
        case 'S': *pattern_out = "nnwnnnwwn"; return 1;
        case 'T': *pattern_out = "nnnnwnwwn"; return 1;
        case 'U': *pattern_out = "wwnnnnnnw"; return 1;
        case 'V': *pattern_out = "nwwnnnnnw"; return 1;
        case 'W': *pattern_out = "wwwnnnnnn"; return 1;
        case 'X': *pattern_out = "nwnnwnnnw"; return 1;
        case 'Y': *pattern_out = "wwnnwnnnn"; return 1;
        case 'Z': *pattern_out = "nwwnwnnnn"; return 1;
        case '-': *pattern_out = "nwnnnnwnw"; return 1;
        case '.': *pattern_out = "wwnnnnwnn"; return 1;
        case ' ': *pattern_out = "nwwnnnwnn"; return 1;
        case '$': *pattern_out = "nwnwnwnnn"; return 1;
        case '/': *pattern_out = "nwnwnnnwn"; return 1;
        case '+': *pattern_out = "nwnnnwnwn"; return 1;
        case '%': *pattern_out = "nnnwnwnwn"; return 1;
        case '*': *pattern_out = "nwnnwnwnn"; return 1;
    }
    return 0;
}

static int b2026_createimage_barcode_code39_value(char ch) {
    static const char *alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%";
    const char *cursor;
    if (ch >= 'a' && ch <= 'z') ch = (char)(ch - 'a' + 'A');
    cursor = strchr(alphabet, ch);
    if (cursor == NULL) return -1;
    return (int)(cursor - alphabet);
}

static int b2026_createimage_barcode_emit_code39(const char *text, char **bits_out, int *module_count_out) {
    size_t len;
    size_t i;
    size_t capacity;
    size_t pos = 0U;
    char *bits;
    const char *pattern;
    char decorated[1024];
    size_t dlen;
    if (text == NULL) text = "";
    len = strlen(text);
    if (len + 2U >= sizeof(decorated)) return 0;
    decorated[0] = '*';
    memcpy(decorated + 1, text, len);
    decorated[len + 1U] = '*';
    decorated[len + 2U] = '\0';
    dlen = len + 2U;
    capacity = dlen * 32U + 1U;
    bits = (char *)malloc(capacity);
    if (bits == NULL) return 0;
    for (i = 0; i < dlen; ++i) {
        int elem;
        int is_bar = 1;
        char ch = decorated[i];
        if (ch >= 'a' && ch <= 'z') ch = (char)(ch - 'a' + 'A');
        if (!b2026_createimage_barcode_code39_pattern(ch, &pattern)) {
            free(bits);
            return 0;
        }
        for (elem = 0; elem < 9; ++elem) {
            int width_units = pattern[elem] == 'w' ? 3 : 1;
            int k;
            for (k = 0; k < width_units; ++k) bits[pos++] = is_bar ? '1' : '0';
            is_bar = !is_bar;
        }
        if (i + 1U < dlen) bits[pos++] = '0';
    }
    bits[pos] = '\0';
    *bits_out = bits;
    *module_count_out = (int)pos;
    return 1;
}

static int b2026_createimage_barcode_prepare_logmars_text(const char *text, char *buffer, size_t buffer_size, char *check_out) {
    static const char *alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%";
    size_t len;
    size_t i;
    int sum = 0;
    int value;
    char check_char;
    if (text == NULL) text = "";
    len = strlen(text);
    if (len == 0U || len + 2U > buffer_size) return 0;
    for (i = 0U; i < len; ++i) {
        char ch = text[i];
        if (ch >= 'a' && ch <= 'z') ch = (char)(ch - 'a' + 'A');
        value = b2026_createimage_barcode_code39_value(ch);
        if (value < 0) return 0;
        buffer[i] = ch;
        sum += value;
    }
    check_char = alphabet[sum % 43];
    buffer[len] = check_char;
    buffer[len + 1U] = '\0';
    if (check_out != NULL) {
        check_out[0] = check_char;
        check_out[1] = '\0';
    }
    return 1;
}

static int b2026_createimage_barcode_code93_index_for_char(char ch) {
    static const char *alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%";
    const char *cursor = strchr(alphabet, ch);
    if (cursor == NULL) return -1;
    return (int)(cursor - alphabet);
}

static int b2026_createimage_barcode_code93_shift_index(char ch) {
    switch (ch) {
        case '$': return 43;
        case '%': return 44;
        case '/': return 45;
        case '+': return 46;
    }
    return -1;
}

static int b2026_createimage_barcode_emit_code93(const char *text, char **bits_out, int *module_count_out) {
    static const char *codeset[] = {
        "131112", "111213", "111312", "111411", "121113",
        "121212", "121311", "111114", "131211", "141111",
        "211113", "211212", "211311", "221112", "221211",
        "231111", "112113", "112212", "112311", "122112",
        "132111", "111123", "111222", "111321", "121122",
        "131121", "212112", "212211", "211122", "211221",
        "221121", "222111", "112122", "112221", "122121",
        "123111", "121131", "311112", "311211", "321111",
        "112131", "113121", "211131", "121221", "312111",
        "311121", "122211", "111141"
    };
    static const char shiftset[] =
        "%$$$$$$$$$$$$$$$$$$$$$$$$$$%%%%% ///  ///// /             /%%%%%%                          %%%%%%++++++++++++++++++++++++++%%%%%";
    static const char shiftset2[] =
        "UABCDEFGHIJKLMNOPQRSTUVWXYZABCDE ABC  FGHIJ L             ZFGHIJV                          KLMNOWABCDEFGHIJLKMNOPQRSTUVWXYZPQRST";
    int symbols[2048];
    int symbol_count = 0;
    size_t i;
    size_t pos = 0U;
    size_t capacity;
    char *bits;
    int checksum_c = 0;
    int checksum_k = 0;
    int weight;
    if (text == NULL) text = "";
    if (*text == '\0') return 0;
    for (i = 0U; text[i] != '\0'; ++i) {
        unsigned char uch = (unsigned char)text[i];
        int code = b2026_createimage_barcode_code93_index_for_char((char)uch);
        if (uch > 127U) return 0;
        if (code >= 0) {
            if (symbol_count >= (int)(sizeof(symbols) / sizeof(symbols[0])) - 4) return 0;
            symbols[symbol_count++] = code;
        } else {
            int shift_code;
            int second_code;
            char shift_ch = shiftset[uch];
            char second_ch = shiftset2[uch];
            if (shift_ch == '\0' || second_ch == '\0' || shift_ch == ' ') return 0;
            shift_code = b2026_createimage_barcode_code93_shift_index(shift_ch);
            second_code = b2026_createimage_barcode_code93_index_for_char(second_ch);
            if (shift_code < 0 || second_code < 0) return 0;
            if (symbol_count >= (int)(sizeof(symbols) / sizeof(symbols[0])) - 4) return 0;
            symbols[symbol_count++] = shift_code;
            symbols[symbol_count++] = second_code;
        }
    }
    weight = 1;
    for (i = (size_t)symbol_count; i > 0U; --i) {
        checksum_c += symbols[i - 1U] * weight;
        ++weight;
        if (weight > 20) weight = 1;
    }
    checksum_c %= 47;
    symbols[symbol_count++] = checksum_c;
    weight = 1;
    for (i = (size_t)symbol_count; i > 0U; --i) {
        checksum_k += symbols[i - 1U] * weight;
        ++weight;
        if (weight > 15) weight = 1;
    }
    checksum_k %= 47;
    symbols[symbol_count++] = checksum_k;
    capacity = (size_t)(symbol_count + 2) * 10U + 2U;
    bits = (char *)malloc(capacity);
    if (bits == NULL) return 0;
    {
        int full_count = symbol_count + 2;
        int idx;
        for (idx = 0; idx < full_count; ++idx) {
            const char *pattern;
            int pattern_index;
            int is_bar = 1;
            int elem;
            if (idx == 0 || idx == full_count - 1) pattern_index = 47;
            else pattern_index = symbols[idx - 1];
            pattern = codeset[pattern_index];
            for (elem = 0; elem < 6; ++elem) {
                int width_units = pattern[elem] - '0';
                int k;
                if (width_units < 1 || width_units > 4) {
                    free(bits);
                    return 0;
                }
                for (k = 0; k < width_units; ++k) bits[pos++] = is_bar ? '1' : '0';
                is_bar = !is_bar;
            }
        }
    }
    bits[pos++] = '1';
    bits[pos] = '\0';
    *bits_out = bits;
    *module_count_out = (int)pos;
    return 1;
}

static int b2026_createimage_barcode_code11_value(char ch) {
    if (ch >= '0' && ch <= '9') return ch - '0';
    if (ch == '-') return 10;
    return -1;
}

static char b2026_createimage_barcode_code11_char_from_value(int value) {
    if (value >= 0 && value <= 9) return (char)('0' + value);
    if (value == 10) return '-';
    return '?';
}

static int b2026_createimage_barcode_code11_pattern(char ch, const char **pattern_out) {
    switch (ch) {
        case '0': *pattern_out = "nnnnw"; return 1;
        case '1': *pattern_out = "wnnnw"; return 1;
        case '2': *pattern_out = "nwnnw"; return 1;
        case '3': *pattern_out = "wwnnn"; return 1;
        case '4': *pattern_out = "nnwnw"; return 1;
        case '5': *pattern_out = "wnwnn"; return 1;
        case '6': *pattern_out = "nwwnn"; return 1;
        case '7': *pattern_out = "nnnww"; return 1;
        case '8': *pattern_out = "wnnwn"; return 1;
        case '9': *pattern_out = "wnnnn"; return 1;
        case '-': *pattern_out = "nnwnn"; return 1;
    }
    return 0;
}

static int b2026_createimage_barcode_emit_code11(const char *text, char **bits_out, int *module_count_out, char *normalized_out, size_t normalized_out_size, char *check_out, size_t check_out_size) {
    char normalized[512];
    size_t len = 0U;
    size_t i;
    int values[512];
    int value_count = 0;
    int checksum_c = 0;
    int checksum_k = -1;
    int weight;
    char encoded[520];
    size_t encoded_len;
    size_t capacity;
    char *bits;
    size_t pos = 0U;
    if (text == NULL) text = "";
    for (i = 0U; text[i] != '\0'; ++i) {
        char ch = text[i];
        if (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n') continue;
        if (b2026_createimage_barcode_code11_value(ch) < 0) return 0;
        if (len + 1U >= sizeof(normalized)) return 0;
        normalized[len++] = ch;
    }
    if (len == 0U) return 0;
    normalized[len] = '\0';
    for (i = 0U; i < len; ++i) values[value_count++] = b2026_createimage_barcode_code11_value(normalized[i]);
    weight = 1;
    for (i = (size_t)value_count; i > 0U; --i) {
        checksum_c += values[i - 1U] * weight;
        ++weight;
        if (weight > 10) weight = 1;
    }
    checksum_c %= 11;
    values[value_count++] = checksum_c;
    if (len >= 10U) {
        checksum_k = 0;
        weight = 1;
        for (i = (size_t)value_count; i > 0U; --i) {
            checksum_k += values[i - 1U] * weight;
            ++weight;
            if (weight > 9) weight = 1;
        }
        checksum_k %= 11;
        values[value_count++] = checksum_k;
    }
    memcpy(encoded, normalized, len);
    encoded_len = len;
    encoded[encoded_len++] = b2026_createimage_barcode_code11_char_from_value(checksum_c);
    if (checksum_k >= 0) encoded[encoded_len++] = b2026_createimage_barcode_code11_char_from_value(checksum_k);
    encoded[encoded_len] = '\0';
    capacity = (encoded_len + 2U) * 16U + 1U;
    bits = (char *)malloc(capacity);
    if (bits == NULL) return 0;
    {
        const char *pattern;
        int elem;
        int k;
        int is_bar = 1;
        if (!b2026_createimage_barcode_code11_pattern('-', &pattern)) {
            free(bits);
            return 0;
        }
        for (elem = 0; elem < 5; ++elem) {
            int width_units = pattern[elem] == 'w' ? 2 : 1;
            for (k = 0; k < width_units; ++k) bits[pos++] = is_bar ? '1' : '0';
            is_bar = !is_bar;
        }
        bits[pos++] = '0';
    }
    for (i = 0U; i < encoded_len; ++i) {
        const char *pattern;
        int elem;
        int k;
        int is_bar = 1;
        if (!b2026_createimage_barcode_code11_pattern(encoded[i], &pattern)) {
            free(bits);
            return 0;
        }
        for (elem = 0; elem < 5; ++elem) {
            int width_units = pattern[elem] == 'w' ? 2 : 1;
            for (k = 0; k < width_units; ++k) bits[pos++] = is_bar ? '1' : '0';
            is_bar = !is_bar;
        }
        bits[pos++] = '0';
    }
    {
        const char *pattern;
        int elem;
        int k;
        int is_bar = 1;
        if (!b2026_createimage_barcode_code11_pattern('-', &pattern)) {
            free(bits);
            return 0;
        }
        for (elem = 0; elem < 5; ++elem) {
            int width_units = pattern[elem] == 'w' ? 2 : 1;
            for (k = 0; k < width_units; ++k) bits[pos++] = is_bar ? '1' : '0';
            is_bar = !is_bar;
        }
        bits[pos++] = '1';
    }
    bits[pos] = '\0';
    if (normalized_out != NULL && normalized_out_size > 0U) {
        strncpy(normalized_out, normalized, normalized_out_size - 1U);
        normalized_out[normalized_out_size - 1U] = '\0';
    }
    if (check_out != NULL && check_out_size > 0U) {
        if (checksum_k >= 0 && check_out_size >= 3U) {
            check_out[0] = b2026_createimage_barcode_code11_char_from_value(checksum_c);
            check_out[1] = b2026_createimage_barcode_code11_char_from_value(checksum_k);
            check_out[2] = '\0';
        } else if (check_out_size >= 2U) {
            check_out[0] = b2026_createimage_barcode_code11_char_from_value(checksum_c);
            check_out[1] = '\0';
        } else {
            check_out[0] = '\0';
        }
    }
    *bits_out = bits;
    *module_count_out = (int)pos;
    return 1;
}

static int b2026_createimage_barcode_prepare_code32(const char *text, char *digits_out, size_t digits_out_size, char *code39_out, size_t code39_out_size) {
    static const char *alphabet = "0123456789BCDFGHJKLMNPQRSTUVWXYZ";
    char cleaned[32];
    size_t len = 0U;
    size_t i;
    unsigned long long value = 0ULL;
    char encoded[8];
    int check = 0;
    if (text == NULL) text = "";
    for (i = 0U; text[i] != '\0'; ++i) {
        char ch = text[i];
        if (ch == ' ' || ch == '-' || ch == '\t' || ch == '\r' || ch == '\n') continue;
        if (ch < '0' || ch > '9') return 0;
        if (len + 1U >= sizeof(cleaned)) return 0;
        cleaned[len++] = ch;
    }
    if (!(len == 8U || len == 9U)) return 0;
    cleaned[len] = '\0';
    {
        int sum = 0;
        for (i = 0U; i < 8U; ++i) {
            int digit = cleaned[i] - '0';
            if ((i % 2U) == 1U) {
                int doubled = digit * 2;
                if (doubled >= 10) doubled = (doubled / 10) + (doubled % 10);
                sum += doubled;
            } else {
                sum += digit;
            }
        }
        check = sum % 10;
    }
    if (len == 9U) {
        if (cleaned[8] != (char)('0' + check)) return 0;
    } else {
        cleaned[len++] = (char)('0' + check);
        cleaned[len] = '\0';
    }
    if (digits_out != NULL && digits_out_size > len) {
        memcpy(digits_out, cleaned, len + 1U);
    }
    for (i = 0U; i < len; ++i) value = value * 10ULL + (unsigned long long)(cleaned[i] - '0');
    for (i = 0U; i < 6U; ++i) {
        encoded[5U - i] = alphabet[value % 32ULL];
        value /= 32ULL;
    }
    encoded[6] = '\0';
    if (code39_out != NULL && code39_out_size >= 7U) {
        memcpy(code39_out, encoded, 7U);
    }
    return 1;
}

static const char *b2026_createimage_barcode_ean_l(int digit) {
    static const char *table[10] = {"0001101","0011001","0010011","0111101","0100011","0110001","0101111","0111011","0110111","0001011"};
    return (digit >= 0 && digit <= 9) ? table[digit] : NULL;
}

static const char *b2026_createimage_barcode_ean_g(int digit) {
    static const char *table[10] = {"0100111","0110011","0011011","0100001","0011101","0111001","0000101","0010001","0001001","0010111"};
    return (digit >= 0 && digit <= 9) ? table[digit] : NULL;
}

static const char *b2026_createimage_barcode_ean_r(int digit) {
    static const char *table[10] = {"1110010","1100110","1101100","1000010","1011100","1001110","1010000","1000100","1001000","1110100"};
    return (digit >= 0 && digit <= 9) ? table[digit] : NULL;
}

static int b2026_createimage_barcode_ean_mod10_digit(const char *digits, int length) {
    int sum = 0;
    int i;
    if (digits == NULL || length <= 0) return -1;
    for (i = length - 1; i >= 0; --i) {
        int d = digits[i] - '0';
        if (d < 0 || d > 9) return -1;
        if (((length - 1 - i) % 2) == 0) sum += d * 3;
        else sum += d;
    }
    return (10 - (sum % 10)) % 10;
}

static int b2026_createimage_barcode_isbn10_check_digit(const char *digits, int length) {
    int sum = 0;
    int i;
    if (digits == NULL || length != 9) return -1;
    for (i = 0; i < 9; ++i) {
        int d = digits[i] - '0';
        if (d < 0 || d > 9) return -1;
        sum += d * (10 - i);
    }
    sum = 11 - (sum % 11);
    if (sum == 11) return 0;
    if (sum == 10) return 10;
    return sum;
}

static int b2026_createimage_barcode_validate_isbn10(const char *digits) {
    int check;
    int i;
    if (digits == NULL || strlen(digits) != 10U) return 0;
    for (i = 0; i < 9; ++i) {
        if (digits[i] < '0' || digits[i] > '9') return 0;
    }
    check = b2026_createimage_barcode_isbn10_check_digit(digits, 9);
    if (check < 0) return 0;
    if (check == 10) return digits[9] == 'X' || digits[9] == 'x';
    return digits[9] == (char)('0' + check);
}

static int b2026_createimage_barcode_prepare_isbn_digits(const char *text, char *buffer, size_t buffer_size) {
    char cleaned[64];
    size_t len = 0U;
    size_t i;
    int check;
    if (text == NULL) text = "";
    for (i = 0U; text[i] != '\0'; ++i) {
        char ch = text[i];
        if (ch == '-' || ch == ' ' || ch == '	') continue;
        if (len + 1U >= sizeof(cleaned)) return 0;
        cleaned[len++] = ch;
    }
    cleaned[len] = '\0';
    if (buffer == NULL || buffer_size < 14U) return 0;
    if (len == 9U) {
        for (i = 0U; i < 9U; ++i) if (cleaned[i] < '0' || cleaned[i] > '9') return 0;
        memcpy(buffer, "978", 3U);
        memcpy(buffer + 3, cleaned, 9U);
        check = b2026_createimage_barcode_ean_mod10_digit(buffer, 12);
        if (check < 0) return 0;
        buffer[12] = (char)('0' + check);
        buffer[13] = '\0';
        return 1;
    }
    if (len == 10U) {
        if (!b2026_createimage_barcode_validate_isbn10(cleaned)) return 0;
        memcpy(buffer, "978", 3U);
        memcpy(buffer + 3, cleaned, 9U);
        check = b2026_createimage_barcode_ean_mod10_digit(buffer, 12);
        if (check < 0) return 0;
        buffer[12] = (char)('0' + check);
        buffer[13] = '\0';
        return 1;
    }
    if (len == 12U) {
        if (strncmp(cleaned, "978", 3U) != 0 && strncmp(cleaned, "979", 3U) != 0) return 0;
        for (i = 0U; i < 12U; ++i) if (cleaned[i] < '0' || cleaned[i] > '9') return 0;
        memcpy(buffer, cleaned, 12U);
        check = b2026_createimage_barcode_ean_mod10_digit(buffer, 12);
        if (check < 0) return 0;
        buffer[12] = (char)('0' + check);
        buffer[13] = '\0';
        return 1;
    }
    if (len == 13U) {
        if (strncmp(cleaned, "978", 3U) != 0 && strncmp(cleaned, "979", 3U) != 0) return 0;
        for (i = 0U; i < 13U; ++i) if (cleaned[i] < '0' || cleaned[i] > '9') return 0;
        check = b2026_createimage_barcode_ean_mod10_digit(cleaned, 12);
        if (check < 0 || cleaned[12] != (char)('0' + check)) return 0;
        memcpy(buffer, cleaned, 14U);
        return 1;
    }
    return 0;
}

static int b2026_createimage_barcode_normalize_digit_text(const char *text, char *buffer, size_t buffer_size) {
    size_t len = 0U;
    size_t i;
    if (buffer == NULL || buffer_size == 0U) return 0;
    if (text == NULL) text = "";
    for (i = 0U; text[i] != '\0'; ++i) {
        char ch = text[i];
        if (ch == '-' || ch == ' ' || ch == '	') continue;
        if (ch < '0' || ch > '9') return 0;
        if (len + 1U >= buffer_size) return 0;
        buffer[len++] = ch;
    }
    if (len == 0U) return 0;
    buffer[len] = '\0';
    return 1;
}

static int b2026_createimage_barcode_prepare_itf14_digits(const char *text, char *buffer, size_t buffer_size) {
    char cleaned[32];
    size_t len;
    int check;
    if (!b2026_createimage_barcode_normalize_digit_text(text, cleaned, sizeof(cleaned))) return 0;
    if (buffer == NULL || buffer_size < 15U) return 0;
    len = strlen(cleaned);
    if (len == 13U) {
        memcpy(buffer, cleaned, 13U);
        check = b2026_createimage_barcode_ean_mod10_digit(buffer, 13);
        if (check < 0) return 0;
        buffer[13] = (char)('0' + check);
        buffer[14] = '\0';
        return 1;
    }
    if (len == 14U) {
        memcpy(buffer, cleaned, 14U);
        buffer[14] = '\0';
        check = b2026_createimage_barcode_ean_mod10_digit(buffer, 13);
        if (check < 0) return 0;
        return buffer[13] == (char)('0' + check);
    }
    return 0;
}

static int b2026_createimage_barcode_mod10_check_digit(const char *digits, size_t length) {
    size_t i;
    int sum = 0;
    if (digits == NULL) return -1;
    for (i = 0U; i < length; ++i) {
        char ch = digits[i];
        if (ch < '0' || ch > '9') return -1;
        sum += ch - '0';
    }
    return (10 - (sum % 10)) % 10;
}

static int b2026_createimage_barcode_prepare_postnet_digits(const char *text, char *buffer, size_t buffer_size) {
    char cleaned[32];
    size_t len;
    int check;
    if (!b2026_createimage_barcode_normalize_digit_text(text, cleaned, sizeof(cleaned))) return 0;
    if (buffer == NULL || buffer_size < 13U) return 0;
    len = strlen(cleaned);
    if (len == 5U || len == 9U || len == 11U) {
        memcpy(buffer, cleaned, len);
        check = b2026_createimage_barcode_mod10_check_digit(buffer, len);
        if (check < 0) return 0;
        buffer[len] = (char)('0' + check);
        buffer[len + 1U] = '\0';
        return 1;
    }
    if (len == 6U || len == 10U || len == 12U) {
        memcpy(buffer, cleaned, len);
        buffer[len] = '\0';
        check = b2026_createimage_barcode_mod10_check_digit(buffer, len - 1U);
        if (check < 0) return 0;
        return buffer[len - 1U] == (char)('0' + check);
    }
    return 0;
}

static int b2026_createimage_barcode_emit_postnet_bars(const char *digits, char **bars_out, int *bar_count_out) {
    static const char *patterns[10] = {
        "11000", "00011", "00101", "00110", "01001",
        "01010", "01100", "10001", "10010", "10100"
    };
    size_t len;
    size_t i;
    size_t pos = 0U;
    char *bars;
    if (digits == NULL) return 0;
    len = strlen(digits);
    if (!(len == 6U || len == 10U || len == 12U)) return 0;
    for (i = 0U; i < len; ++i) {
        if (digits[i] < '0' || digits[i] > '9') return 0;
    }
    bars = (char *)malloc(2U + len * 5U + 1U);
    if (bars == NULL) return 0;
    bars[pos++] = '1';
    for (i = 0U; i < len; ++i) {
        const char *pattern = patterns[digits[i] - '0'];
        memcpy(bars + pos, pattern, 5U);
        pos += 5U;
    }
    bars[pos++] = '1';
    bars[pos] = '\0';
    *bars_out = bars;
    *bar_count_out = (int)pos;
    return 1;
}

static int b2026_createimage_barcode_parse_positive_int_text(const char *text, int *value_out) {
    char cleaned[64];
    size_t i;
    int value = 0;
    if (value_out != NULL) *value_out = 0;
    if (!b2026_createimage_barcode_normalize_digit_text(text, cleaned, sizeof(cleaned))) return 0;
    for (i = 0U; cleaned[i] != '\0'; ++i) {
        int digit = cleaned[i] - '0';
        if (value > 100000000) return 0;
        value = value * 10 + digit;
    }
    if (value_out != NULL) *value_out = value;
    return 1;
}

static int b2026_createimage_barcode_emit_pharmacode_bits(int value, char **bits_out, int *module_count_out) {
    char bars[64];
    int bar_count = 0;
    int temp = value;
    size_t pos = 0U;
    char *bits;
    int i;
    if (value < 3 || value > 131070) return 0;
    while (temp > 0) {
        if ((temp % 2) == 0) {
            bars[bar_count++] = 'W';
            temp = (temp - 2) / 2;
        } else {
            bars[bar_count++] = 'N';
            temp = (temp - 1) / 2;
        }
        if (bar_count >= (int)sizeof(bars)) return 0;
    }
    bits = (char *)malloc((size_t)bar_count * 4U + 1U);
    if (bits == NULL) return 0;
    for (i = bar_count - 1; i >= 0; --i) {
        int bar_units = (bars[i] == 'W') ? 3 : 1;
        int u;
        for (u = 0; u < bar_units; ++u) bits[pos++] = '1';
        if (i > 0) bits[pos++] = '0';
    }
    bits[pos] = '\0';
    *bits_out = bits;
    *module_count_out = (int)pos;
    return 1;
}

static int b2026_createimage_barcode_popcount8(unsigned char value) {
    int count = 0;
    while (value != 0U) {
        count += (int)(value & 1U);
        value >>= 1U;
    }
    return count;
}

static unsigned char b2026_createimage_barcode_telepen_even_parity(unsigned char value7) {
    unsigned char value = (unsigned char)(value7 & 0x7FU);
    if ((b2026_createimage_barcode_popcount8(value) % 2) != 0) value = (unsigned char)(value | 0x80U);
    return value;
}

static void b2026_createimage_barcode_append_modules_bits(char *dest, size_t *pos, int is_bar, int width_units) {
    int i;
    char ch = is_bar ? '1' : '0';
    for (i = 0; i < width_units; ++i) dest[(*pos)++] = ch;
}

static int b2026_createimage_barcode_emit_telepen_bits(const char *text, char **bits_out, int *module_count_out, int *check_raw_out, char **debug_bitstream_out, char **debug_bytes_out) {
    unsigned char bytes[2048];
    size_t text_len;
    size_t byte_count;
    size_t bit_count;
    char *bitstream;
    char *modules;
    char *bytes_text = NULL;
    size_t bit_pos = 0U;
    size_t i;
    size_t out_pos = 0U;
    int sum = 0;
    int check_raw;
    size_t bytes_text_len = 0U;
    if (text == NULL) text = "";
    if (check_raw_out != NULL) *check_raw_out = 0;
    if (debug_bitstream_out != NULL) *debug_bitstream_out = NULL;
    if (debug_bytes_out != NULL) *debug_bytes_out = NULL;
    text_len = strlen(text);
    if (text_len + 3U >= sizeof(bytes)) return 0;
    bytes[0] = 0x5FU;
    for (i = 0U; i < text_len; ++i) {
        unsigned char ch = (unsigned char)text[i];
        if (ch > 127U) return 0;
        sum += (int)ch;
        bytes[i + 1U] = b2026_createimage_barcode_telepen_even_parity(ch);
    }
    check_raw = (127 - (sum % 127)) % 127;
    bytes[text_len + 1U] = b2026_createimage_barcode_telepen_even_parity((unsigned char)check_raw);
    bytes[text_len + 2U] = 0xFAU;
    byte_count = text_len + 3U;
    if (check_raw_out != NULL) *check_raw_out = check_raw;
    bit_count = byte_count * 8U;
    bitstream = (char *)malloc(bit_count + 1U);
    if (bitstream == NULL) return 0;
    for (i = 0U; i < byte_count; ++i) {
        unsigned char value = bytes[i];
        int b;
        for (b = 0; b < 8; ++b) {
            bitstream[bit_pos++] = ((value >> b) & 1U) ? '1' : '0';
        }
    }
    bitstream[bit_pos] = '\0';
    if (debug_bytes_out != NULL) {
        bytes_text_len = byte_count * 3U;
        if (bytes_text_len > 0U) bytes_text_len -= 1U;
        bytes_text = (char *)malloc(bytes_text_len + 1U);
        if (bytes_text == NULL) {
            free(bitstream);
            return 0;
        }
        bytes_text[0] = '\0';
        for (i = 0U; i < byte_count; ++i) {
            char chunk[8];
            snprintf(chunk, sizeof(chunk), "%02X", (unsigned int)bytes[i]);
            strcat(bytes_text, chunk);
            if (i + 1U < byte_count) strcat(bytes_text, ",");
        }
    }
    modules = (char *)malloc(bit_count * 4U + 1U);
    if (modules == NULL) {
        free(bitstream);
        free(bytes_text);
        return 0;
    }
    i = 0U;
    while (i < bit_count) {
        if (bitstream[i] == '1') {
            if (i + 1U == bit_count) {
                b2026_createimage_barcode_append_modules_bits(modules, &out_pos, 1, 1);
                b2026_createimage_barcode_append_modules_bits(modules, &out_pos, 0, 1);
                i += 1U;
            } else if (bitstream[i + 1U] == '0' && (i + 2U >= bit_count || bitstream[i + 2U] == '0')) {
                b2026_createimage_barcode_append_modules_bits(modules, &out_pos, 1, 1);
                b2026_createimage_barcode_append_modules_bits(modules, &out_pos, 0, 3);
                i += 2U;
            } else {
                b2026_createimage_barcode_append_modules_bits(modules, &out_pos, 1, 1);
                b2026_createimage_barcode_append_modules_bits(modules, &out_pos, 0, 1);
                i += 1U;
            }
        } else {
            if (i + 1U < bit_count && bitstream[i + 1U] == '0') {
                b2026_createimage_barcode_append_modules_bits(modules, &out_pos, 1, 3);
                b2026_createimage_barcode_append_modules_bits(modules, &out_pos, 0, 1);
                i += 2U;
            } else {
                size_t j = i + 1U;
                while (j < bit_count && bitstream[j] == '1') j++;
                if (j >= bit_count) {
                    size_t ones = j - i - 1U;
                    size_t k;
                    if (ones < 1U) {
                        free(bitstream);
                        free(modules);
                        free(bytes_text);
                        return 0;
                    }
                    b2026_createimage_barcode_append_modules_bits(modules, &out_pos, 1, 1);
                    b2026_createimage_barcode_append_modules_bits(modules, &out_pos, 0, 3);
                    for (k = 0U; k + 1U < ones; ++k) {
                        b2026_createimage_barcode_append_modules_bits(modules, &out_pos, 1, 1);
                        b2026_createimage_barcode_append_modules_bits(modules, &out_pos, 0, 1);
                    }
                    i = j;
                } else if (bitstream[j] != '0') {
                    free(bitstream);
                    free(modules);
                    free(bytes_text);
                    return 0;
                } else if (j == i + 2U) {
                    b2026_createimage_barcode_append_modules_bits(modules, &out_pos, 1, 3);
                    b2026_createimage_barcode_append_modules_bits(modules, &out_pos, 0, 3);
                } else {
                    size_t ones = j - i - 1U;
                    size_t k;
                    b2026_createimage_barcode_append_modules_bits(modules, &out_pos, 1, 1);
                    b2026_createimage_barcode_append_modules_bits(modules, &out_pos, 0, 3);
                    for (k = 0U; k + 2U < ones; ++k) {
                        b2026_createimage_barcode_append_modules_bits(modules, &out_pos, 1, 1);
                        b2026_createimage_barcode_append_modules_bits(modules, &out_pos, 0, 1);
                    }
                    b2026_createimage_barcode_append_modules_bits(modules, &out_pos, 1, 1);
                    b2026_createimage_barcode_append_modules_bits(modules, &out_pos, 0, 3);
                }
                i = j + 1U;
            }
        }
    }
    modules[out_pos] = '\0';
    if (debug_bitstream_out != NULL) {
        *debug_bitstream_out = bitstream;
    } else {
        free(bitstream);
    }
    if (debug_bytes_out != NULL) {
        *debug_bytes_out = bytes_text;
    } else {
        free(bytes_text);
    }
    *bits_out = modules;
    *module_count_out = (int)out_pos;
    return 1;
}

static int b2026_createimage_barcode_emit_ean13_bits(const char *digits, char **bits_out, int *module_count_out) {
    static const char *parity[10] = {
        "LLLLLL","LLGLGG","LLGGLG","LLGGGL","LGLLGG","LGGLLG","LGGGLL","LGLGLG","LGLGGL","LGGLGL"
    };
    char *bits;
    int pos = 0;
    int first;
    int i;
    if (digits == NULL || strlen(digits) != 13U) return 0;
    for (i = 0; i < 13; ++i) if (digits[i] < '0' || digits[i] > '9') return 0;
    bits = (char *)malloc(96U);
    if (bits == NULL) return 0;
    first = digits[0] - '0';
    memcpy(bits + pos, "101", 3U); pos += 3;
    for (i = 1; i <= 6; ++i) {
        int digit = digits[i] - '0';
        const char *pat = parity[first][i - 1] == 'G' ? b2026_createimage_barcode_ean_g(digit) : b2026_createimage_barcode_ean_l(digit);
        memcpy(bits + pos, pat, 7U); pos += 7;
    }
    memcpy(bits + pos, "01010", 5U); pos += 5;
    for (i = 7; i <= 12; ++i) {
        int digit = digits[i] - '0';
        const char *pat = b2026_createimage_barcode_ean_r(digit);
        memcpy(bits + pos, pat, 7U); pos += 7;
    }
    memcpy(bits + pos, "101", 3U); pos += 3;
    bits[pos] = '\0';
    *bits_out = bits;
    *module_count_out = pos;
    return 1;
}

static int b2026_createimage_barcode_emit_upca_bits(const char *digits, char **bits_out, int *module_count_out) {
    char ean[14];
    if (digits == NULL || strlen(digits) != 12U) return 0;
    ean[0] = '0';
    memcpy(ean + 1, digits, 12U);
    ean[13] = '\0';
    return b2026_createimage_barcode_emit_ean13_bits(ean, bits_out, module_count_out);
}

static int b2026_createimage_barcode_emit_ean8_bits(const char *digits, char **bits_out, int *module_count_out) {
    char *bits;
    int pos = 0;
    int i;
    if (digits == NULL || strlen(digits) != 8U) return 0;
    for (i = 0; i < 8; ++i) if (digits[i] < '0' || digits[i] > '9') return 0;
    bits = (char *)malloc(68U);
    if (bits == NULL) return 0;
    memcpy(bits + pos, "101", 3U); pos += 3;
    for (i = 0; i < 4; ++i) {
        const char *pat = b2026_createimage_barcode_ean_l(digits[i] - '0');
        memcpy(bits + pos, pat, 7U); pos += 7;
    }
    memcpy(bits + pos, "01010", 5U); pos += 5;
    for (i = 4; i < 8; ++i) {
        const char *pat = b2026_createimage_barcode_ean_r(digits[i] - '0');
        memcpy(bits + pos, pat, 7U); pos += 7;
    }
    memcpy(bits + pos, "101", 3U); pos += 3;
    bits[pos] = '\0';
    *bits_out = bits;
    *module_count_out = pos;
    return 1;
}

static int b2026_createimage_barcode_emit_itf_bits(const char *digits, char **bits_out, int *module_count_out) {
    static const char *patterns[10] = {
        "nnwwn", "wnnnw", "nwnnw", "wwnnn", "nnwnw",
        "wnwnn", "nwwnn", "nnnww", "wnnwn", "nwnwn"
    };
    size_t len;
    size_t i;
    size_t capacity;
    char *bits;
    size_t pos = 0U;
    if (digits == NULL) return 0;
    len = strlen(digits);
    if (len == 0U || (len % 2U) != 0U) return 0;
    for (i = 0; i < len; ++i) {
        if (digits[i] < '0' || digits[i] > '9') return 0;
    }
    capacity = 4U + ((len / 2U) * 30U) + 4U + 1U;
    bits = (char *)malloc(capacity);
    if (bits == NULL) return 0;
    memcpy(bits + pos, "1010", 4U);
    pos += 4U;
    for (i = 0; i < len; i += 2U) {
        int d1 = digits[i] - '0';
        int d2 = digits[i + 1U] - '0';
        const char *p1 = patterns[d1];
        const char *p2 = patterns[d2];
        int k;
        for (k = 0; k < 5; ++k) {
            int bar_units = p1[k] == 'w' ? 3 : 1;
            int space_units = p2[k] == 'w' ? 3 : 1;
            int u;
            for (u = 0; u < bar_units; ++u) bits[pos++] = '1';
            for (u = 0; u < space_units; ++u) bits[pos++] = '0';
        }
    }
    memcpy(bits + pos, "1101", 4U);
    pos += 4U;
    bits[pos] = '\0';
    *bits_out = bits;
    *module_count_out = (int) pos;
    return 1;
}

static int b2026_createimage_barcode_codabar_pattern(char ch, const char **pattern_out) {
    switch (ch) {
        case '0': *pattern_out = "101010011"; return 1;
        case '1': *pattern_out = "101011001"; return 1;
        case '2': *pattern_out = "101001011"; return 1;
        case '3': *pattern_out = "110010101"; return 1;
        case '4': *pattern_out = "101101001"; return 1;
        case '5': *pattern_out = "110101001"; return 1;
        case '6': *pattern_out = "100101011"; return 1;
        case '7': *pattern_out = "100101101"; return 1;
        case '8': *pattern_out = "100110101"; return 1;
        case '9': *pattern_out = "110100101"; return 1;
        case '-': *pattern_out = "101001101"; return 1;
        case '$': *pattern_out = "101100101"; return 1;
        case ':': *pattern_out = "1101011011"; return 1;
        case '/': *pattern_out = "1101101011"; return 1;
        case '.': *pattern_out = "1101101101"; return 1;
        case '+': *pattern_out = "101100110011"; return 1;
        case 'A': *pattern_out = "1011001001"; return 1;
        case 'B': *pattern_out = "1001001011"; return 1;
        case 'C': *pattern_out = "1010010011"; return 1;
        case 'D': *pattern_out = "1010011001"; return 1;
    }
    return 0;
}

static int b2026_createimage_barcode_emit_codabar_bits(const char *text, char **bits_out, int *module_count_out) {
    size_t len;
    size_t i;
    size_t dlen;
    size_t capacity;
    char decorated[1024];
    char *bits;
    size_t pos = 0U;
    if (text == NULL) text = "";
    len = strlen(text);
    if (len == 0U) return 0;
    if (len + 3U >= sizeof(decorated)) return 0;
    memcpy(decorated, text, len + 1U);
    for (i = 0; i < len; ++i) {
        if (decorated[i] >= 'a' && decorated[i] <= 'z') decorated[i] = (char)(decorated[i] - 'a' + 'A');
    }
    if (!(decorated[0] == 'A' || decorated[0] == 'B' || decorated[0] == 'C' || decorated[0] == 'D')) {
        memmove(decorated + 1, decorated, len + 1U);
        decorated[0] = 'A';
        len++;
        decorated[len] = 'A';
        decorated[len + 1U] = '\0';
        len++;
    } else if (!(decorated[len - 1U] == 'A' || decorated[len - 1U] == 'B' || decorated[len - 1U] == 'C' || decorated[len - 1U] == 'D')) {
        decorated[len] = 'A';
        decorated[len + 1U] = '\0';
        len++;
    }
    dlen = len;
    capacity = dlen * 16U + 1U;
    bits = (char *)malloc(capacity);
    if (bits == NULL) return 0;
    for (i = 0; i < dlen; ++i) {
        const char *pattern = NULL;
        if (!b2026_createimage_barcode_codabar_pattern(decorated[i], &pattern)) {
            free(bits);
            return 0;
        }
        memcpy(bits + pos, pattern, strlen(pattern));
        pos += strlen(pattern);
        if (i + 1U < dlen) bits[pos++] = '0';
    }
    bits[pos] = '\0';
    *bits_out = bits;
    *module_count_out = (int) pos;
    return 1;
}

static void b2026_createimage_barcode_append_modules(char *bits, size_t *pos, const char *widths, int width_count) {
    int i;
    int is_bar = 1;
    for (i = 0; i < width_count; ++i) {
        int width_units = widths[i] - '0';
        int u;
        for (u = 0; u < width_units; ++u) bits[(*pos)++] = is_bar ? '1' : '0';
        is_bar = !is_bar;
    }
}

static int b2026_createimage_barcode_emit_code128_bits(const char *text, char **bits_out, int *module_count_out) {
    static const char *patterns[107] = {
        "212222","222122","222221","121223","121322","131222","122213","122312","132212","221213","221312","231212",
        "112232","122132","122231","113222","123122","123221","223211","221132","221231","213212","223112","312131",
        "311222","321122","321221","312212","322112","322211","212123","212321","232121","111323","131123","131321",
        "112313","132113","132311","211313","231113","231311","112133","112331","132131","113123","113321","133121",
        "313121","211331","231131","213113","213311","213131","311123","311321","331121","312113","312311","332111",
        "314111","221411","431111","111224","111422","121124","121421","141122","141221","112214","112412","122114",
        "122411","142112","142211","241211","221114","413111","241112","134111","111242","121142","121241","114212",
        "124112","124211","411212","421112","421211","212141","214121","412121","111143","111341","131141","114113",
        "114311","411113","411311","113141","114131","311141","411131","211412","211214","211232","211133"
    };
    int values[256];
    int value_count = 0;
    int checksum;
    int i;
    size_t len;
    size_t capacity;
    char *bits;
    size_t pos = 0U;
    int digits_only = 1;
    if (text == NULL) text = "";
    len = strlen(text);
    if (len == 0U) return 0;
    for (i = 0; i < (int) len; ++i) {
        unsigned char ch = (unsigned char) text[i];
        if (ch < 32U || ch > 126U) return 0;
        if (ch < '0' || ch > '9') digits_only = 0;
    }
    if (digits_only && (len % 2U) == 0U) {
        values[value_count++] = 105;
        for (i = 0; i < (int) len; i += 2) {
            values[value_count++] = (text[i] - '0') * 10 + (text[i + 1] - '0');
        }
    } else {
        values[value_count++] = 104;
        for (i = 0; i < (int) len; ++i) {
            values[value_count++] = ((unsigned char) text[i]) - 32;
        }
    }
    checksum = values[0];
    for (i = 1; i < value_count; ++i) {
        checksum += values[i] * i;
    }
    checksum %= 103;
    values[value_count++] = checksum;
    capacity = (size_t) value_count * 12U + 16U;
    bits = (char *)malloc(capacity);
    if (bits == NULL) return 0;
    for (i = 0; i < value_count; ++i) {
        b2026_createimage_barcode_append_modules(bits, &pos, patterns[values[i]], 6);
    }
    b2026_createimage_barcode_append_modules(bits, &pos, "2331112", 7);
    bits[pos] = '\0';
    *bits_out = bits;
    *module_count_out = (int) pos;
    return 1;
}

static int b2026_createimage_barcode_emit_code128_values(const int *values, int value_count, char **bits_out, int *module_count_out) {
    static const char *patterns[107] = {
        "212222","222122","222221","121223","121322","131222","122213","122312","132212","221213","221312","231212",
        "112232","122132","122231","113222","123122","123221","223211","221132","221231","213212","223112","312131",
        "311222","321122","321221","312212","322112","322211","212123","212321","232121","111323","131123","131321",
        "112313","132113","132311","211313","231113","231311","112133","112331","132131","113123","113321","133121",
        "313121","211331","231131","213113","213311","213131","311123","311321","331121","312113","312311","332111",
        "314111","221411","431111","111224","111422","121124","121421","141122","141221","112214","112412","122114",
        "122411","142112","142211","241211","221114","413111","241112","134111","111242","121142","121241","114212",
        "124112","124211","411212","421112","421211","212141","214121","412121","111143","111341","131141","114113",
        "114311","411113","411311","113141","114131","311141","411131","211412","211214","211232","211133"
    };
    size_t capacity;
    size_t pos = 0U;
    char *bits;
    int i;
    if (values == NULL || value_count <= 0) return 0;
    capacity = (size_t)value_count * 12U + 16U;
    bits = (char *)malloc(capacity);
    if (bits == NULL) return 0;
    for (i = 0; i < value_count; ++i) {
        if (values[i] < 0 || values[i] >= 107) {
            free(bits);
            return 0;
        }
        b2026_createimage_barcode_append_modules(bits, &pos, patterns[values[i]], 6);
    }
    b2026_createimage_barcode_append_modules(bits, &pos, "2331112", 7);
    bits[pos] = '\0';
    *bits_out = bits;
    *module_count_out = (int)pos;
    return 1;
}

static int b2026_createimage_barcode_gs1_fixed_length(const char *ai, int ai_len) {
    int ai_num = 0;
    int i;
    if (ai == NULL || ai_len < 2 || ai_len > 4) return -1;
    for (i = 0; i < ai_len; ++i) {
        if (ai[i] < '0' || ai[i] > '9') return -1;
        ai_num = ai_num * 10 + (ai[i] - '0');
    }
    if (ai_len == 2) {
        switch (ai_num) {
            case 0: return 18;
            case 1: return 14;
            case 2: return 14;
            case 11: case 12: case 13: case 15: case 16: case 17: return 6;
            case 20: return 2;
            default: return -1;
        }
    }
    if (ai_len == 3) {
        switch (ai_num) {
            case 402: return 17;
            case 410: case 411: case 412: case 413: case 414: case 415: case 416: return 13;
            case 422: case 424: case 425: case 426: return 3;
            case 255: return 13;
            default: return -1;
        }
    }
    if (ai_len == 4) {
        if (ai_num >= 3100 && ai_num <= 3699) return 6;
        switch (ai_num) {
            case 7001: return 13;
            case 7003: return 10;
            case 7006: return 6;
            case 7007: return 12;
            case 8001: return 14;
            case 8005: return 6;
            case 8006: return 18;
            case 8017: return 18;
            case 8018: return 18;
            case 8100: return 6;
            case 8101: return 10;
            case 8102: return 2;
            case 8111: return 4;
            default: return -1;
        }
    }
    return -1;
}

static int b2026_createimage_barcode_prepare_gs1_payload(const char *text, char *payload_out, size_t payload_size, char *normalized_out, size_t normalized_size) {
    size_t src = 0U;
    size_t dst = 0U;
    size_t norm = 0U;
    int has_paren = 0;
    if (text == NULL) text = "";
    if (payload_out == NULL || payload_size == 0U) return 0;
    payload_out[0] = '\0';
    if (normalized_out != NULL && normalized_size > 0U) normalized_out[0] = '\0';
    while (text[src] != '\0') {
        unsigned char ch = (unsigned char)text[src];
        if (ch == '(') { has_paren = 1; break; }
        ++src;
    }
    src = 0U;
    if (!has_paren) {
        while (text[src] != '\0') {
            unsigned char ch = (unsigned char)text[src];
            if ((ch < 32U || ch > 126U) && ch != 29U) return 0;
            if (dst + 2U >= payload_size) return 0;
            payload_out[dst++] = (char)ch;
            if (normalized_out != NULL && normalized_size > 0U) {
                if (norm + 8U >= normalized_size) return 0;
                if (ch == 29U) {
                    normalized_out[norm++] = '<';
                    normalized_out[norm++] = 'G';
                    normalized_out[norm++] = 'S';
                    normalized_out[norm++] = '>';
                } else {
                    normalized_out[norm++] = (char)ch;
                }
            }
            ++src;
        }
        payload_out[dst] = '\0';
        if (normalized_out != NULL && normalized_size > 0U) normalized_out[norm] = '\0';
        return dst > 0U;
    }
    while (text[src] != '\0') {
        char ai[8];
        int ai_len = 0;
        int fixed_len;
        size_t data_start;
        size_t data_len = 0U;
        if (text[src] != '(') return 0;
        ++src;
        while (text[src] != '\0' && text[src] != ')') {
            if (ai_len >= 4) return 0;
            if (text[src] < '0' || text[src] > '9') return 0;
            ai[ai_len++] = text[src++];
        }
        if (text[src] != ')' || ai_len < 2) return 0;
        ++src;
        ai[ai_len] = '\0';
        fixed_len = b2026_createimage_barcode_gs1_fixed_length(ai, ai_len);
        data_start = src;
        if (fixed_len >= 0) {
            int i;
            for (i = 0; i < fixed_len; ++i) {
                unsigned char ch = (unsigned char)text[src];
                if (ch == '\0' || ch == '(' || ch < 32U || ch > 126U) return 0;
                ++src;
            }
            data_len = (size_t)fixed_len;
        } else {
            while (text[src] != '\0' && text[src] != '(') {
                unsigned char ch = (unsigned char)text[src];
                if (ch < 32U || ch > 126U) return 0;
                ++src;
            }
            data_len = src - data_start;
            if (data_len == 0U) return 0;
        }
        if (dst + (size_t)ai_len + data_len + 2U >= payload_size) return 0;
        memcpy(payload_out + dst, ai, (size_t)ai_len);
        dst += (size_t)ai_len;
        memcpy(payload_out + dst, text + data_start, data_len);
        dst += data_len;
        if (normalized_out != NULL && normalized_size > 0U) {
            int wrote = snprintf(normalized_out + norm, normalized_size - norm, "(%s)%.*s", ai, (int)data_len, text + data_start);
            if (wrote < 0 || (size_t)wrote >= normalized_size - norm) return 0;
            norm += (size_t)wrote;
        }
        if (text[src] == '(' && fixed_len < 0) {
            payload_out[dst++] = 29;
            if (normalized_out != NULL && normalized_size > 0U) {
                if (norm + 4U >= normalized_size) return 0;
                normalized_out[norm++] = '<';
                normalized_out[norm++] = 'G';
                normalized_out[norm++] = 'S';
                normalized_out[norm++] = '>';
            }
        }
    }
    payload_out[dst] = '\0';
    if (normalized_out != NULL && normalized_size > 0U) normalized_out[norm] = '\0';
    return dst > 0U;
}

static int b2026_createimage_barcode_emit_gs1128_bits(const char *text, char **bits_out, int *module_count_out, char *normalized_out, size_t normalized_size) {
    char payload[2048];
    int values[1024];
    int value_count = 0;
    int set_mode = 0; /* 0=none,1=B,2=C */
    int checksum;
    int i;
    size_t pos = 0U;
    if (!b2026_createimage_barcode_prepare_gs1_payload(text, payload, sizeof(payload), normalized_out, normalized_size)) return 0;
    while (payload[pos] != '\0') {
        size_t digit_run = 0U;
        if ((unsigned char)payload[pos] == 29U) {
            if (set_mode == 0) {
                values[value_count++] = 104;
                set_mode = 1;
            }
            values[value_count++] = 102;
            ++pos;
            continue;
        }
        while (payload[pos + digit_run] >= '0' && payload[pos + digit_run] <= '9') digit_run++;
        if (set_mode == 0) {
            if (digit_run >= 4U || (digit_run == 2U && payload[pos + digit_run] == '\0')) {
                values[value_count++] = 105;
                set_mode = 2;
            } else {
                values[value_count++] = 104;
                set_mode = 1;
            }
            values[value_count++] = 102;
            continue;
        }
        if (set_mode == 2) {
            if (digit_run >= 2U) {
                while (payload[pos] >= '0' && payload[pos] <= '9' && payload[pos + 1U] >= '0' && payload[pos + 1U] <= '9') {
                    values[value_count++] = (payload[pos] - '0') * 10 + (payload[pos + 1U] - '0');
                    pos += 2U;
                }
                if (payload[pos] == '\0') break;
                if ((unsigned char)payload[pos] == 29U) continue;
            }
            values[value_count++] = 100;
            set_mode = 1;
            continue;
        }
        if (set_mode == 1) {
            if (digit_run >= 4U || (digit_run == 2U && (payload[pos + digit_run] == '\0' || (unsigned char)payload[pos + digit_run] == 29U))) {
                values[value_count++] = 99;
                set_mode = 2;
                continue;
            }
            if ((unsigned char)payload[pos] < 32U || (unsigned char)payload[pos] > 126U) return 0;
            values[value_count++] = ((unsigned char)payload[pos]) - 32;
            ++pos;
        }
        if (value_count >= (int)(sizeof(values) / sizeof(values[0])) - 4) return 0;
    }
    checksum = values[0];
    for (i = 1; i < value_count; ++i) checksum += values[i] * i;
    checksum %= 103;
    values[value_count++] = checksum;
    return b2026_createimage_barcode_emit_code128_values(values, value_count, bits_out, module_count_out);
}

static int b2026_createimage_barcode_prepare_code16k(const char *text, int *values_out, int *value_count_out, int *rows_out) {
    int value_count = 0;
    int rows;
    int pads_needed;
    int i;
    int first_sum = 0;
    int second_sum = 0;
    int first_check;
    int second_check;
    size_t len;
    if (text == NULL) text = "";
    len = strlen(text);
    if (len == 0U) return 0;
    for (i = 0; i < (int)len; ++i) {
        unsigned char ch = (unsigned char)text[i];
        if (ch < 32U || ch > 127U) return 0;
    }
    values_out[value_count++] = 1; /* mode character: 2 rows minimum, Code Set B */
    for (i = 0; i < (int)len; ++i) {
        values_out[value_count++] = ((unsigned char)text[i]) - 32;
        if (value_count >= 78) return 0;
    }
    pads_needed = 5 - ((value_count + 2) % 5);
    if (pads_needed == 5) pads_needed = 0;
    if ((value_count + pads_needed) < 8) pads_needed += 8 - (value_count + pads_needed);
    while (pads_needed-- > 0) values_out[value_count++] = 103; /* pad */
    for (i = 0; i < value_count; ++i) {
        first_sum += (i + 2) * values_out[i];
        second_sum += (i + 1) * values_out[i];
    }
    first_check = first_sum % 107;
    second_sum += first_check * (value_count + 1);
    second_check = second_sum % 107;
    values_out[value_count++] = first_check;
    values_out[value_count++] = second_check;
    rows = value_count / 5;
    if (rows < 2 || rows > 16) return 0;
    values_out[0] = 7 * (rows - 2) + 1; /* mode B */
    *value_count_out = value_count;
    *rows_out = rows;
    return 1;
}

static int b2026_createimage_barcode_emit_msi_bits(const char *digits, char **bits_out, int *module_count_out) {
    static const char *patterns[10] = {
        "100100100100",
        "100100100110",
        "100100110100",
        "100100110110",
        "100110100100",
        "100110100110",
        "100110110100",
        "100110110110",
        "110100100100",
        "110100100110"
    };
    size_t len;
    size_t i;
    size_t pos = 0U;
    size_t capacity;
    char *bits;
    if (digits == NULL) return 0;
    len = strlen(digits);
    if (len == 0U) return 0;
    for (i = 0; i < len; ++i) {
        if (digits[i] < '0' || digits[i] > '9') return 0;
    }
    capacity = 3U + (len * 12U) + 4U + 1U;
    bits = (char *)malloc(capacity);
    if (bits == NULL) return 0;
    memcpy(bits + pos, "110", 3U);
    pos += 3U;
    for (i = 0; i < len; ++i) {
        const char *pattern = patterns[digits[i] - '0'];
        memcpy(bits + pos, pattern, 12U);
        pos += 12U;
    }
    memcpy(bits + pos, "1001", 4U);
    pos += 4U;
    bits[pos] = '\0';
    *bits_out = bits;
    *module_count_out = (int) pos;
    return 1;
}

static int b2026_createimage_barcode_normalize_hex_text(const char *text, char *buffer, size_t buffer_size) {
    size_t len = 0U;
    size_t i;
    if (text == NULL) text = "";
    if (buffer == NULL || buffer_size == 0U) return 0;
    for (i = 0U; text[i] != '\0'; ++i) {
        unsigned char uch = (unsigned char)text[i];
        char ch = (char)uch;
        if (ch == ' ' || ch == '-' || ch == '\t' || ch == '\r' || ch == '\n') continue;
        if (ch >= 'a' && ch <= 'f') ch = (char)(ch - 'a' + 'A');
        if (!((ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'F'))) return 0;
        if (len + 1U >= buffer_size) return 0;
        buffer[len++] = ch;
    }
    if (len == 0U) return 0;
    buffer[len] = '\0';
    return 1;
}

static int b2026_createimage_barcode_emit_plessey_bits(const char *text, char **bits_out, int *module_count_out, char *normalized_out, size_t normalized_out_size, char *crc_out, size_t crc_out_size) {
    static const int poly[9] = {1, 1, 1, 1, 0, 1, 0, 0, 1}; /* 111101001 */
    char normalized[256];
    int data_bits[2048];
    int work_bits[2056];
    int crc_bits[8];
    size_t len;
    int data_bit_count = 0;
    int total_raw_bits;
    size_t capacity;
    char *modules;
    size_t pos = 0U;
    size_t i;
    if (!b2026_createimage_barcode_normalize_hex_text(text, normalized, sizeof(normalized))) return 0;
    len = strlen(normalized);
    for (i = 0U; i < len; ++i) {
        char ch = normalized[i];
        int value = (ch >= '0' && ch <= '9') ? (ch - '0') : (ch - 'A' + 10);
        int bit;
        for (bit = 0; bit < 4; ++bit) {
            data_bits[data_bit_count++] = (value >> bit) & 1;
        }
    }
    if (data_bit_count + 8 >= (int)(sizeof(work_bits) / sizeof(work_bits[0]))) return 0;
    for (i = 0U; i < (size_t)data_bit_count; ++i) work_bits[i] = data_bits[i];
    for (i = 0U; i < 8U; ++i) work_bits[data_bit_count + (int)i] = 0;
    for (i = 0U; i < (size_t)data_bit_count; ++i) {
        int j;
        if (work_bits[i] == 0) continue;
        for (j = 0; j < 9; ++j) work_bits[i + j] ^= poly[j];
    }
    for (i = 0U; i < 8U; ++i) crc_bits[i] = work_bits[data_bit_count + (int)i];
    total_raw_bits = 4 + data_bit_count + 8 + 1 + 4;
    capacity = (size_t)total_raw_bits * 3U + 1U;
    modules = (char *)malloc(capacity);
    if (modules == NULL) return 0;
    {
        static const int start_bits[4] = {1, 1, 0, 1};
        static const int stop_bits[4] = {0, 0, 1, 1};
        int bit_value;
        int k;
        #define B2026_APPEND_PLESSEY_BIT(value) \
            do { \
                if (value) { \
                    modules[pos++] = '1'; \
                    modules[pos++] = '1'; \
                    modules[pos++] = '0'; \
                } else { \
                    modules[pos++] = '1'; \
                    modules[pos++] = '0'; \
                    modules[pos++] = '0'; \
                } \
            } while (0)
        for (k = 0; k < 4; ++k) {
            bit_value = start_bits[k];
            B2026_APPEND_PLESSEY_BIT(bit_value);
        }
        for (k = 0; k < data_bit_count; ++k) {
            bit_value = data_bits[k];
            B2026_APPEND_PLESSEY_BIT(bit_value);
        }
        for (k = 0; k < 8; ++k) {
            bit_value = crc_bits[k];
            B2026_APPEND_PLESSEY_BIT(bit_value);
        }
        B2026_APPEND_PLESSEY_BIT(1);
        for (k = 0; k < 4; ++k) {
            bit_value = stop_bits[k];
            B2026_APPEND_PLESSEY_BIT(bit_value);
        }
        #undef B2026_APPEND_PLESSEY_BIT
    }
    modules[pos] = '\0';
    if (normalized_out != NULL && normalized_out_size > 0U) {
        strncpy(normalized_out, normalized, normalized_out_size - 1U);
        normalized_out[normalized_out_size - 1U] = '\0';
    }
    if (crc_out != NULL && crc_out_size >= 9U) {
        for (i = 0U; i < 8U; ++i) crc_out[i] = (char)('0' + crc_bits[i]);
        crc_out[8] = '\0';
    }
    *bits_out = modules;
    *module_count_out = (int)pos;
    return 1;
}

static int b2026_createimage_barcode_msi_mod10_digit(const char *digits) {
    int sum = 0;
    int len;
    int i;
    int use_double = 1;
    if (digits == NULL) return -1;
    len = (int)strlen(digits);
    if (len <= 0) return -1;
    for (i = len - 1; i >= 0; --i) {
        int d = digits[i] - '0';
        if (d < 0 || d > 9) return -1;
        if (use_double) {
            int value = d * 2;
            sum += (value / 10) + (value % 10);
        } else {
            sum += d;
        }
        use_double = !use_double;
    }
    return (10 - (sum % 10)) % 10;
}

static int b2026_createimage_barcode_msi_mod11_digit(const char *digits) {
    int sum = 0;
    int len;
    int i;
    int weight = 2;
    int result;
    if (digits == NULL) return -1;
    len = (int)strlen(digits);
    if (len <= 0) return -1;
    for (i = len - 1; i >= 0; --i) {
        int d = digits[i] - '0';
        if (d < 0 || d > 9) return -1;
        sum += d * weight;
        weight++;
        if (weight > 7) weight = 2;
    }
    result = 11 - (sum % 11);
    if (result == 11) result = 0;
    else if (result == 10) result = 0;
    return result;
}

static int b2026_createimage_barcode_prepare_msi_digits(const char *kind_name, const char *text, char *buffer, size_t buffer_size) {
    size_t len;
    int digit;
    if (text == NULL) text = "";
    len = strlen(text);
    if (len == 0U || len + 3U >= buffer_size) return 0;
    memcpy(buffer, text, len + 1U);
    {
        size_t i;
        for (i = 0; i < len; ++i) {
            if (buffer[i] < '0' || buffer[i] > '9') return 0;
        }
    }
    if (b2026_barcode_ascii_equals_ignore_case(kind_name, "MSI") || b2026_barcode_ascii_equals_ignore_case(kind_name, "MSI-PLESSEY")) {
        return 1;
    }
    if (b2026_barcode_ascii_equals_ignore_case(kind_name, "MSI10") || b2026_barcode_ascii_equals_ignore_case(kind_name, "MSI-10")) {
        digit = b2026_createimage_barcode_msi_mod10_digit(buffer);
        if (digit < 0) return 0;
        buffer[len++] = (char)('0' + digit);
        buffer[len] = '\0';
        return 1;
    }
    if (b2026_barcode_ascii_equals_ignore_case(kind_name, "MSI11") || b2026_barcode_ascii_equals_ignore_case(kind_name, "MSI-11")) {
        digit = b2026_createimage_barcode_msi_mod11_digit(buffer);
        if (digit < 0) return 0;
        buffer[len++] = (char)('0' + digit);
        buffer[len] = '\0';
        return 1;
    }
    if (b2026_barcode_ascii_equals_ignore_case(kind_name, "MSI1010") || b2026_barcode_ascii_equals_ignore_case(kind_name, "MSI-1010")) {
        digit = b2026_createimage_barcode_msi_mod10_digit(buffer);
        if (digit < 0) return 0;
        buffer[len++] = (char)('0' + digit);
        buffer[len] = '\0';
        digit = b2026_createimage_barcode_msi_mod10_digit(buffer);
        if (digit < 0) return 0;
        buffer[len++] = (char)('0' + digit);
        buffer[len] = '\0';
        return 1;
    }
    if (b2026_barcode_ascii_equals_ignore_case(kind_name, "MSI1110") || b2026_barcode_ascii_equals_ignore_case(kind_name, "MSI-1110")) {
        digit = b2026_createimage_barcode_msi_mod11_digit(buffer);
        if (digit < 0) return 0;
        buffer[len++] = (char)('0' + digit);
        buffer[len] = '\0';
        digit = b2026_createimage_barcode_msi_mod10_digit(buffer);
        if (digit < 0) return 0;
        buffer[len++] = (char)('0' + digit);
        buffer[len] = '\0';
        return 1;
    }
    return 0;
}

void createimage_barcode_on_array(const char *target_name, const char *kind_name, const char *text, double scale_value, double height_value, double margin_value) {
    ArrayVariable *target_var;
    char *bits = NULL;
    int module_count = 0;
    char msi_digits[1024];
    char isbn_digits[32];
    char postnet_digits[32];
    int pharmacode_value = 0;
    int telepen_check_raw = 0;
    char *telepen_bitstream = NULL;
    char *telepen_bytes = NULL;
    int scale;
    int bar_height;
    int margin;
    int width;
    int height;
    int draw_y_offset = 0;
    int bearer_thickness = 0;
    int use_itf14_bearer = 0;
    int use_postnet_style = 0;
    int use_planet_style = 0;
    int use_code16k_style = 0;
    int ok = 0;
    int i;
    char note_buffer[1024];
    char plessey_text[256];
    char plessey_crc[16];
    char code11_text[512];
    char code11_check[8];
    char code32_digits[32];
    char code32_code39[8];
    char logmars_text[1024];
    char logmars_check[8];
    int code16k_values[80];
    int code16k_value_count = 0;
    int code16k_rows = 0;
    if (target_name == NULL || *target_name == '\0') runtime_error("CREATEIMAGE(\"BARCODE\") failed: target is required / 【CREATEIMAGE】BARCODE の出力先が必要です。");
    if (kind_name == NULL || *kind_name == '\0') runtime_error("CREATEIMAGE(\"BARCODE\") failed: kind is required / 【CREATEIMAGE】BARCODE の種類が必要です。");
    if (text == NULL) text = "";
    scale = createimage_floor_int(scale_value + 0.5);
    bar_height = createimage_floor_int(height_value + 0.5);
    margin = createimage_floor_int(margin_value + 0.5);
    if (scale <= 0) runtime_error("CREATEIMAGE(\"BARCODE\") failed: scale must be >= 1 / 【CREATEIMAGE】BARCODE の scale は 1 以上である必要があります。");
    if (bar_height <= 0) runtime_error("CREATEIMAGE(\"BARCODE\") failed: height must be >= 1 / 【CREATEIMAGE】BARCODE の height は 1 以上である必要があります。");
    if (margin < 0) runtime_error("CREATEIMAGE(\"BARCODE\") failed: margin must be >= 0 / 【CREATEIMAGE】BARCODE の margin は 0 以上である必要があります。");

    if (b2026_barcode_ascii_equals_ignore_case(kind_name, "CODE39")) {
        ok = b2026_createimage_barcode_emit_code39(text, &bits, &module_count);
        if (!ok) runtime_error("CREATEIMAGE(\"BARCODE\") failed: invalid CODE39 text / 【CREATEIMAGE】CODE39 で使えない文字が含まれています。");
    } else if (b2026_barcode_ascii_equals_ignore_case(kind_name, "LOGMARS")) {
        ok = b2026_createimage_barcode_prepare_logmars_text(text, logmars_text, sizeof(logmars_text), logmars_check);
        if (!ok) runtime_error("CREATEIMAGE(\"BARCODE\") failed: LOGMARS accepts CODE39 characters and appends Mod43 / 【CREATEIMAGE】LOGMARS は CODE39 と同じ文字種に対応し、Mod43 チェック文字を自動付加します。");
        ok = b2026_createimage_barcode_emit_code39(logmars_text, &bits, &module_count);
        if (!ok) runtime_error("CREATEIMAGE(\"BARCODE\") failed: LOGMARS encode failed / 【CREATEIMAGE】LOGMARS バーコードの生成に失敗しました。");
        snprintf(note_buffer, sizeof(note_buffer), "[NOTE] LOGMARS normalized=%s", logmars_text);
        append_basic_note_log(note_buffer);
        snprintf(note_buffer, sizeof(note_buffer), "[NOTE] LOGMARS check=%s", logmars_check);
        append_basic_note_log(note_buffer);
    } else if (b2026_barcode_ascii_equals_ignore_case(kind_name, "CODE16K")
            || b2026_barcode_ascii_equals_ignore_case(kind_name, "CODE-16K")
            || b2026_barcode_ascii_equals_ignore_case(kind_name, "CODE 16K")) {
        ok = b2026_createimage_barcode_prepare_code16k(text, code16k_values, &code16k_value_count, &code16k_rows);
        if (!ok) runtime_error("CREATEIMAGE(\"BARCODE\") failed: CODE16K supports ASCII 32-127 and up to 77 characters / 【CREATEIMAGE】CODE16K は ASCII 32〜127、最大77文字まで対応します。");
        use_code16k_style = 1;
        snprintf(note_buffer, sizeof(note_buffer), "[NOTE] CODE16K rows=%d", code16k_rows);
        append_basic_note_log(note_buffer);
        {
            char values_buf[1024];
            size_t off = 0U;
            values_buf[0] = '\0';
            for (i = 0; i < code16k_value_count; ++i) {
                int wrote = snprintf(values_buf + off, sizeof(values_buf) - off, "%s%d", i == 0 ? "" : ",", code16k_values[i]);
                if (wrote < 0 || (size_t)wrote >= sizeof(values_buf) - off) break;
                off += (size_t)wrote;
            }
            snprintf(note_buffer, sizeof(note_buffer), "[NOTE] CODE16K values=%s", values_buf);
            append_basic_note_log(note_buffer);
        }
    } else if (b2026_barcode_ascii_equals_ignore_case(kind_name, "CODE32")
            || b2026_barcode_ascii_equals_ignore_case(kind_name, "CODE-32")
            || b2026_barcode_ascii_equals_ignore_case(kind_name, "CODE 32")) {
        ok = b2026_createimage_barcode_prepare_code32(text, code32_digits, sizeof(code32_digits), code32_code39, sizeof(code32_code39));
        if (!ok) runtime_error("CREATEIMAGE(\"BARCODE\") failed: CODE32 requires 8 digits or 9 digits with a valid check digit / 【CREATEIMAGE】CODE32 は 8桁、または正しいチェックディジット付きの9桁数字列を指定してください。");
        ok = b2026_createimage_barcode_emit_code39(code32_code39, &bits, &module_count);
        if (!ok) runtime_error("CREATEIMAGE(\"BARCODE\") failed: CODE32 encode failed / 【CREATEIMAGE】CODE32 バーコードの生成に失敗しました。");
        snprintf(note_buffer, sizeof(note_buffer), "[NOTE] CODE32 digits=%s", code32_digits);
        append_basic_note_log(note_buffer);
        snprintf(note_buffer, sizeof(note_buffer), "[NOTE] CODE32 code39=%s", code32_code39);
        append_basic_note_log(note_buffer);
    } else if (b2026_barcode_ascii_equals_ignore_case(kind_name, "CODE11")
            || b2026_barcode_ascii_equals_ignore_case(kind_name, "CODE-11")
            || b2026_barcode_ascii_equals_ignore_case(kind_name, "CODE 11")) {
        ok = b2026_createimage_barcode_emit_code11(text, &bits, &module_count, code11_text, sizeof(code11_text), code11_check, sizeof(code11_check));
        if (!ok) runtime_error("CREATEIMAGE(\"BARCODE\") failed: CODE11 requires digits and '-' / 【CREATEIMAGE】CODE11 は数字とハイフンだけを指定してください。");
        snprintf(note_buffer, sizeof(note_buffer), "[NOTE] CODE11 normalized=%s", code11_text);
        append_basic_note_log(note_buffer);
        snprintf(note_buffer, sizeof(note_buffer), "[NOTE] CODE11 check=%s", code11_check);
        append_basic_note_log(note_buffer);
    } else if (b2026_barcode_ascii_equals_ignore_case(kind_name, "CODE93")
            || b2026_barcode_ascii_equals_ignore_case(kind_name, "CODE-93")
            || b2026_barcode_ascii_equals_ignore_case(kind_name, "CODE 93")) {
        ok = b2026_createimage_barcode_emit_code93(text, &bits, &module_count);
        if (!ok) runtime_error("CREATEIMAGE(\"BARCODE\") failed: invalid CODE93 text / 【CREATEIMAGE】CODE93 では ASCII 0-127 の文字列を指定してください。");
    } else if (b2026_barcode_ascii_equals_ignore_case(kind_name, "GS1-128")
            || b2026_barcode_ascii_equals_ignore_case(kind_name, "GS1128")
            || b2026_barcode_ascii_equals_ignore_case(kind_name, "EAN128")
            || b2026_barcode_ascii_equals_ignore_case(kind_name, "EAN-128")
            || b2026_barcode_ascii_equals_ignore_case(kind_name, "UCC/EAN-128")) {
        char gs1_normalized[2048];
        ok = b2026_createimage_barcode_emit_gs1128_bits(text, &bits, &module_count, gs1_normalized, sizeof(gs1_normalized));
        if (!ok) runtime_error("CREATEIMAGE(\"BARCODE\") failed: invalid GS1-128 text / 【CREATEIMAGE】GS1-128 は (01)...(17)...(10)... のような AI 付き文字列、または GS 区切りの生データを指定してください。");
        snprintf(note_buffer, sizeof(note_buffer), "[NOTE] GS1-128 normalized=%s", gs1_normalized);
        append_basic_note_log(note_buffer);
    } else if (b2026_barcode_ascii_equals_ignore_case(kind_name, "PLESSEY")
            || b2026_barcode_ascii_equals_ignore_case(kind_name, "UKPLESSEY")
            || b2026_barcode_ascii_equals_ignore_case(kind_name, "UK-PLESSEY")) {
        ok = b2026_createimage_barcode_emit_plessey_bits(text, &bits, &module_count, plessey_text, sizeof(plessey_text), plessey_crc, sizeof(plessey_crc));
        if (!ok) runtime_error("CREATEIMAGE(\"BARCODE\") failed: PLESSEY requires hexadecimal text / 【CREATEIMAGE】PLESSEY は 0-9 / A-F の16進文字列を指定してください。");
        snprintf(note_buffer, sizeof(note_buffer), "[NOTE] PLESSEY normalized=%s", plessey_text);
        append_basic_note_log(note_buffer);
        snprintf(note_buffer, sizeof(note_buffer), "[NOTE] PLESSEY crc=%s", plessey_crc);
        append_basic_note_log(note_buffer);
    } else if (b2026_barcode_ascii_equals_ignore_case(kind_name, "CODE128")) {
        ok = b2026_createimage_barcode_emit_code128_bits(text, &bits, &module_count);
        if (!ok) runtime_error("CREATEIMAGE(\"BARCODE\") failed: invalid CODE128 text / 【CREATEIMAGE】CODE128 では ASCII 32-126 の文字列、または偶数桁の数字列を指定してください。");
    } else if (b2026_barcode_ascii_equals_ignore_case(kind_name, "MSI")
            || b2026_barcode_ascii_equals_ignore_case(kind_name, "MSI-PLESSEY")
            || b2026_barcode_ascii_equals_ignore_case(kind_name, "MSI10")
            || b2026_barcode_ascii_equals_ignore_case(kind_name, "MSI-10")
            || b2026_barcode_ascii_equals_ignore_case(kind_name, "MSI11")
            || b2026_barcode_ascii_equals_ignore_case(kind_name, "MSI-11")
            || b2026_barcode_ascii_equals_ignore_case(kind_name, "MSI1010")
            || b2026_barcode_ascii_equals_ignore_case(kind_name, "MSI-1010")
            || b2026_barcode_ascii_equals_ignore_case(kind_name, "MSI1110")
            || b2026_barcode_ascii_equals_ignore_case(kind_name, "MSI-1110")) {
        ok = b2026_createimage_barcode_prepare_msi_digits(kind_name, text, msi_digits, sizeof(msi_digits));
        if (!ok) runtime_error("CREATEIMAGE(\"BARCODE\") failed: MSI text is invalid / 【CREATEIMAGE】MSI 系バーコードには数字列を指定してください。");
        ok = b2026_createimage_barcode_emit_msi_bits(msi_digits, &bits, &module_count);
        if (!ok) runtime_error("CREATEIMAGE(\"BARCODE\") failed: MSI encode failed / 【CREATEIMAGE】MSI 系バーコードの生成に失敗しました。");
    } else if (b2026_barcode_ascii_equals_ignore_case(kind_name, "ISBN") ||
               b2026_barcode_ascii_equals_ignore_case(kind_name, "ISBN13") ||
               b2026_barcode_ascii_equals_ignore_case(kind_name, "ISBN-13")) {
        ok = b2026_createimage_barcode_prepare_isbn_digits(text, isbn_digits, sizeof(isbn_digits));
        if (!ok) runtime_error("CREATEIMAGE(\"BARCODE\") failed: ISBN must be 9/10/12/13 digits (hyphens allowed) / 【CREATEIMAGE】ISBN は 9桁/10桁/12桁/13桁の数字列（ハイフン可）を指定してください。");
        ok = b2026_createimage_barcode_emit_ean13_bits(isbn_digits, &bits, &module_count);
        if (!ok) runtime_error("CREATEIMAGE(\"BARCODE\") failed: ISBN encode failed / 【CREATEIMAGE】ISBN バーコードの生成に失敗しました。");
    } else if (b2026_barcode_ascii_equals_ignore_case(kind_name, "JAN") || b2026_barcode_ascii_equals_ignore_case(kind_name, "EAN13") || b2026_barcode_ascii_equals_ignore_case(kind_name, "EAN-13")) {
        ok = b2026_createimage_barcode_emit_ean13_bits(text, &bits, &module_count);
        if (!ok) runtime_error("CREATEIMAGE(\"BARCODE\") failed: JAN/EAN13 requires 13 digits / 【CREATEIMAGE】JAN/EAN13 は 13 桁の数字が必要です。");
    } else if (b2026_barcode_ascii_equals_ignore_case(kind_name, "EAN8") || b2026_barcode_ascii_equals_ignore_case(kind_name, "EAN-8")) {
        ok = b2026_createimage_barcode_emit_ean8_bits(text, &bits, &module_count);
        if (!ok) runtime_error("CREATEIMAGE(\"BARCODE\") failed: EAN-8 requires 8 digits / 【CREATEIMAGE】EAN-8 は 8 桁の数字が必要です。");
    } else if (b2026_barcode_ascii_equals_ignore_case(kind_name, "UPCA") || b2026_barcode_ascii_equals_ignore_case(kind_name, "UPC-A")) {
        ok = b2026_createimage_barcode_emit_upca_bits(text, &bits, &module_count);
        if (!ok) runtime_error("CREATEIMAGE(\"BARCODE\") failed: UPC-A requires 12 digits / 【CREATEIMAGE】UPC-A は 12 桁の数字が必要です。");
    } else if (b2026_barcode_ascii_equals_ignore_case(kind_name, "POSTNET")) {
        use_postnet_style = 1;
        ok = b2026_createimage_barcode_prepare_postnet_digits(text, postnet_digits, sizeof(postnet_digits));
        if (!ok) runtime_error("CREATEIMAGE(\"BARCODE\") failed: POSTNET requires 5/9/11 digits or 6/10/12 digits with a valid check digit / 【CREATEIMAGE】POSTNET は 5桁/9桁/11桁、または正しいチェックディジット付きの 6桁/10桁/12桁の数字列が必要です。");
        ok = b2026_createimage_barcode_emit_postnet_bars(postnet_digits, &bits, &module_count);
        if (!ok) runtime_error("CREATEIMAGE(\"BARCODE\") failed: POSTNET encode failed / 【CREATEIMAGE】POSTNET バーコードの生成に失敗しました。");
    } else if (b2026_barcode_ascii_equals_ignore_case(kind_name, "PLANET")) {
        use_planet_style = 1;
        ok = b2026_createimage_barcode_prepare_postnet_digits(text, postnet_digits, sizeof(postnet_digits));
        if (!ok) runtime_error("CREATEIMAGE(\"BARCODE\") failed: PLANET requires 5/9/11 digits or 6/10/12 digits with a valid check digit / 【CREATEIMAGE】PLANET は 5桁/9桁/11桁、または正しいチェックディジット付きの 6桁/10桁/12桁の数字列が必要です。");
        ok = b2026_createimage_barcode_emit_postnet_bars(postnet_digits, &bits, &module_count);
        if (!ok) runtime_error("CREATEIMAGE(\"BARCODE\") failed: PLANET encode failed / 【CREATEIMAGE】PLANET バーコードの生成に失敗しました。");
    } else if (b2026_barcode_ascii_equals_ignore_case(kind_name, "PHARMACODE")) {
        ok = b2026_createimage_barcode_parse_positive_int_text(text, &pharmacode_value);
        if (!ok || pharmacode_value < 3 || pharmacode_value > 131070) runtime_error("CREATEIMAGE(\"BARCODE\") failed: PHARMACODE requires an integer from 3 to 131070 / 【CREATEIMAGE】PHARMACODE は 3〜131070 の整数が必要です。");
        ok = b2026_createimage_barcode_emit_pharmacode_bits(pharmacode_value, &bits, &module_count);
        if (!ok) runtime_error("CREATEIMAGE(\"BARCODE\") failed: PHARMACODE encode failed / 【CREATEIMAGE】PHARMACODE バーコードの生成に失敗しました。");
    } else if (b2026_barcode_ascii_equals_ignore_case(kind_name, "TELEPEN")) {
        ok = b2026_createimage_barcode_emit_telepen_bits(text, &bits, &module_count, &telepen_check_raw, &telepen_bitstream, &telepen_bytes);
        if (!ok) runtime_error("CREATEIMAGE(\"BARCODE\") failed: TELEPEN requires ASCII 0-127 text / 【CREATEIMAGE】TELEPEN は ASCII 0〜127 の文字列を指定してください。");
        snprintf(note_buffer, sizeof(note_buffer), "[NOTE] TELEPEN checksumRaw=%d", telepen_check_raw);
        append_basic_note_log(note_buffer);
        if (telepen_bytes != NULL) {
            snprintf(note_buffer, sizeof(note_buffer), "[NOTE] TELEPEN parityBytes=%s", telepen_bytes);
            append_basic_note_log(note_buffer);
        }
        if (telepen_bitstream != NULL) {
            append_basic_note_log("[NOTE] TELEPEN bitOrder=LSB-first");
            snprintf(note_buffer, sizeof(note_buffer), "[NOTE] TELEPEN bitstream=%s", telepen_bitstream);
            append_basic_note_log(note_buffer);
        }
        if (bits != NULL) {
            snprintf(note_buffer, sizeof(note_buffer), "[NOTE] TELEPEN modules=%s", bits);
            append_basic_note_log(note_buffer);
        }
    } else if (b2026_barcode_ascii_equals_ignore_case(kind_name, "ITF")
            || b2026_barcode_ascii_equals_ignore_case(kind_name, "INTERLEAVED2OF5")
            || b2026_barcode_ascii_equals_ignore_case(kind_name, "INTERLEAVED-2-OF-5")) {
        char itf_digits[128];
        ok = b2026_createimage_barcode_normalize_digit_text(text, itf_digits, sizeof(itf_digits));
        if (!ok || (strlen(itf_digits) % 2U) != 0U) runtime_error("CREATEIMAGE(\"BARCODE\") failed: ITF requires an even number of digits / 【CREATEIMAGE】ITF は偶数桁の数字列が必要です。");
        ok = b2026_createimage_barcode_emit_itf_bits(itf_digits, &bits, &module_count);
        if (!ok) runtime_error("CREATEIMAGE(\"BARCODE\") failed: ITF encode failed / 【CREATEIMAGE】ITF バーコードの生成に失敗しました。");
    } else if (b2026_barcode_ascii_equals_ignore_case(kind_name, "ITF14")
            || b2026_barcode_ascii_equals_ignore_case(kind_name, "ITF-14")
            || b2026_barcode_ascii_equals_ignore_case(kind_name, "ITF14B")
            || b2026_barcode_ascii_equals_ignore_case(kind_name, "ITF-14-B")
            || b2026_barcode_ascii_equals_ignore_case(kind_name, "ITF14BEARER")
            || b2026_barcode_ascii_equals_ignore_case(kind_name, "ITF-14-BEARER")) {
        char itf14_digits[32];
        use_itf14_bearer = (b2026_barcode_ascii_equals_ignore_case(kind_name, "ITF14B")
                || b2026_barcode_ascii_equals_ignore_case(kind_name, "ITF-14-B")
                || b2026_barcode_ascii_equals_ignore_case(kind_name, "ITF14BEARER")
                || b2026_barcode_ascii_equals_ignore_case(kind_name, "ITF-14-BEARER"));
        ok = b2026_createimage_barcode_prepare_itf14_digits(text, itf14_digits, sizeof(itf14_digits));
        if (!ok) runtime_error("CREATEIMAGE(\"BARCODE\") failed: ITF-14 requires 13 or 14 digits with a valid check digit / 【CREATEIMAGE】ITF-14 は 13桁または14桁の数字列（14桁時は正しいチェックディジット付き）が必要です。");
        ok = b2026_createimage_barcode_emit_itf_bits(itf14_digits, &bits, &module_count);
        if (!ok) runtime_error("CREATEIMAGE(\"BARCODE\") failed: ITF-14 encode failed / 【CREATEIMAGE】ITF-14 バーコードの生成に失敗しました。");
    } else if (b2026_barcode_ascii_equals_ignore_case(kind_name, "NW7") || b2026_barcode_ascii_equals_ignore_case(kind_name, "CODABAR")) {
        ok = b2026_createimage_barcode_emit_codabar_bits(text, &bits, &module_count);
        if (!ok) runtime_error("CREATEIMAGE(\"BARCODE\") failed: invalid NW7/CODABAR text / 【CREATEIMAGE】NW7/CODABAR で使えない文字が含まれています。");
    } else {
        runtime_error("CREATEIMAGE(\"BARCODE\") failed: unsupported kind / 【CREATEIMAGE】対応していないバーコード種類です。");
    }

    if (use_code16k_style) width = 70 * scale + margin * 2;
    else if (use_postnet_style || use_planet_style) width = module_count * scale + (module_count - 1) * scale + margin * 2;
    else width = module_count * scale + margin * 2;
    if (use_code16k_style) height = code16k_rows * bar_height + (code16k_rows + 1) * scale + margin * 2;
    else height = bar_height + margin * 2;
    if (use_itf14_bearer) {
        bearer_thickness = scale >= 2 ? scale : 2;
        draw_y_offset = bearer_thickness;
        height += bearer_thickness * 2;
    }
    if (!b2026_createimage_barcode_fill_target(target_name, width, height)) {
        free(bits);
        runtime_error("CREATEIMAGE(\"BARCODE\") failed: could not allocate target image / 【CREATEIMAGE】出力画像を確保できませんでした。");
    }
    target_var = require_array_variable(target_name);
    ensure_array_unique(target_var);
    if (use_code16k_style) {
        static const char *patterns[107] = {
            "212222","222122","222221","121223","121322","131222","122213","122312","132212","221213","221312","231212",
            "112232","122132","122231","113222","123122","123221","223211","221132","221231","213212","223112","312131",
            "311222","321122","321221","312212","322112","322211","212123","212321","232121","111323","131123","131321",
            "112313","132113","132311","211313","231113","231311","112133","112331","132131","113123","113321","133121",
            "313121","211331","231131","213113","213311","213131","311123","311321","331121","312113","312311","332111",
            "314111","221411","431111","111224","111422","121124","121421","141122","141221","112214","112412","122114",
            "122411","142112","142211","241211","221114","413111","241112","134111","111242","121142","121241","114212",
            "124112","124211","411212","421112","421211","212141","214121","412121","111143","111341","131141","114113",
            "114311","411113","411311","113141","114131","311141","411131","211412","211214","211232","211133"
        };
        static const char *startstop[8] = {"3211","2221","2122","1411","1132","1231","1114","3112"};
        static const int start_values[16] = {0,1,2,3,4,5,6,7,0,1,2,3,4,5,6,7};
        static const int stop_values[16] = {0,1,2,3,4,5,6,7,4,5,6,7,0,1,2,3};
        int row;
        b2026_createimage_barcode_draw_rect(target_var, width, height, 0, margin, width, margin + scale, 0.0, 0.0, 0.0, 255.0);
        for (row = 0; row < code16k_rows; ++row) {
            char row_bits[1024];
            size_t row_pos = 0U;
            int writer = margin;
            int y0 = margin + scale + row * (bar_height + scale);
            int y1 = y0 + bar_height;
            int j;
            row_bits[0] = '\0';
            b2026_createimage_barcode_append_modules(row_bits, &row_pos, startstop[start_values[row]], 4);
            row_bits[row_pos++] = '1';
            for (j = 0; j < 5; ++j) {
                b2026_createimage_barcode_append_modules(row_bits, &row_pos, patterns[code16k_values[row * 5 + j]], 6);
            }
            b2026_createimage_barcode_append_modules(row_bits, &row_pos, startstop[stop_values[row]], 4);
            row_bits[row_pos] = '\0';
            for (j = 0; j < (int)row_pos; ++j) {
                if (row_bits[j] == '1') {
                    b2026_createimage_barcode_draw_rect(target_var, width, height, writer, y0, writer + scale, y1, 0.0, 0.0, 0.0, 255.0);
                }
                writer += scale;
            }
            b2026_createimage_barcode_draw_rect(target_var, width, height, 0, y1, width, y1 + scale, 0.0, 0.0, 0.0, 255.0);
        }
        free(bits);
        return;
    }
    if (use_itf14_bearer) {
        int x0 = margin;
        int x1 = margin + module_count * scale;
        b2026_createimage_barcode_draw_rect(target_var, width, height, x0, margin, x1, margin + bearer_thickness, 0.0, 0.0, 0.0, 255.0);
        b2026_createimage_barcode_draw_rect(target_var, width, height, x0, margin + bearer_thickness + bar_height, x1, margin + bearer_thickness + bar_height + bearer_thickness, 0.0, 0.0, 0.0, 255.0);
    }
    for (i = 0; i < module_count; ++i) {
        if (use_postnet_style || use_planet_style) {
            int x0 = margin + i * scale * 2;
            int x1 = x0 + scale;
            int short_height = createimage_floor_int(bar_height * 0.4 + 0.5);
            int tall = bits[i] == '1';
            int y0;
            if (short_height < 1) short_height = 1;
            if (use_planet_style) tall = !tall;
            y0 = margin + draw_y_offset + (tall ? 0 : (bar_height - short_height));
            b2026_createimage_barcode_draw_rect(target_var, width, height, x0, y0, x1, margin + draw_y_offset + bar_height, 0.0, 0.0, 0.0, 255.0);
        } else if (bits[i] == '1') {
            int x0 = margin + i * scale;
            int x1 = x0 + scale;
            b2026_createimage_barcode_draw_rect(target_var, width, height, x0, margin + draw_y_offset, x1, margin + draw_y_offset + bar_height, 0.0, 0.0, 0.0, 255.0);
        }
    }
    target_var->read_count = width * height * 4;
    free(bits);
    free(telepen_bitstream);
    free(telepen_bytes);
}
