Загрузка данных


#include <iostream>
#include <vector>
#include <windows.h>

// Подключаем чтение картинок
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"

// Подключаем СОХРАНЕНИЕ картинок
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"

int main() {
    SetConsoleCP(1251);
    SetConsoleOutputCP(1251);

    std::cout << "==================================================\n";
    std::cout << "     C++ Board Cropper Engine (v2.0)              \n";
    std::cout << "==================================================\n\n";

    int width, height, channels;

    // 1. Открываем исходный скриншот
    std::cout << "[...] Otkryvaem board.png...\n";
    unsigned char* img = stbi_load("board.png", &width, &height, &channels, 0);

    if (img == NULL) {
        std::cout << "\n[ERROR] Ne udalos naitii board.png!\n";
        std::cout << "Nazhmi Enter dlya vyhoda...";
        std::cin.get();
        return 1;
    }

    std::cout << "[OK] Kartinka uspeshno zagruzhena (" << width << "x" << height << " px)\n";

    // 2. Поиск границ доски (наши координаты)
    int minX = width, maxX = 0;
    int minY = height, maxY = 0;

    for (int y = 0; y < height; y += 5) {
        for (int x = 0; x < width; x += 5) {
            int index = (y * width + x) * channels;
            int brightness = (img[index] + img[index + 1] + img[index + 2]) / 3;

            if (brightness > 50 && brightness < 200) {
                if (x < minX) minX = x;
                if (x > maxX) maxX = x;
                if (y < minY) minY = y;
                if (y > maxY) maxY = y;
            }
        }
    }

    // Размеры вырезаемой области
    int cropWidth = maxX - minX;
    int cropHeight = maxY - minY;

    std::cout << "[...] Vyrezaem oblast: " << cropWidth << "x" << cropHeight << " px...\n";

    // 3. Создаем новый массив пикселей под вырезанный кусок
    std::vector<unsigned char> cropData(cropWidth * cropHeight * channels);

    // Копируем пиксели из оригинала в новый массив
    for (int cy = 0; cy < cropHeight; ++cy) {
        for (int cx = 0; cx < cropWidth; ++cx) {
            int origX = minX + cx;
            int origY = minY + cy;

            int origIndex = (origY * width + origX) * channels;
            int cropIndex = (cy * cropWidth + cx) * channels;

            for (int c = 0; c < channels; ++c) {
                cropData[cropIndex + c] = img[origIndex + c];
            }
        }
    }

    // 4. Сохраняем вырезанную доску в файл crop_board.png!
    int result = stbi_write_png("crop_board.png", cropWidth, cropHeight, channels, cropData.data(), cropWidth * channels);

    if (result != 0) {
        std::cout << "\n[УСПЕХ] Файл 'crop_board.png' успешно сохранен в папку с программой!\n";
    } else {
        std::cout << "\n[ОШИБКА] Не удалось сохранить файл 'crop_board.png'\n";
    }

    // Освобождаем оперативку
    stbi_image_free(img);

    std::cout << "\nNazhmi Enter dlya vyhoda...";
    std::cin.get();

    return 0;
}