год или пол года назад очень круто шарил за C++, писал змейку, строчный калькулятор без библиотек и т.д. сейчас хочу снова начать, пишите ваши задачи, можете под хайд, можете в лс, решу (не обещаю) бесплатно. или, если вам нечего не надо, можете просто накидать идей
leha_zabor, #include <iostream> #include <fstream> #include <vector> const int JPEG_MAGIC_NUMBER = 0xFFD8; const int BMP_MAGIC_NUMBER = 0x4D42; struct BMPHeader { int magicNumber; int fileSize; int reserved; int dataOffset; }; struct DIBHeader { int headerSize; int width; int height; short planes; short bitsPerPixel; int compression; int imageSize; int xPixelsPerMeter; int yPixelsPerMeter; int colorsUsed; int importantColors; }; struct Pixel { unsigned char blue; unsigned char green; unsigned char red; }; // Converts a BMP image to a JPEG image void BMPToJPEG(const std::string& bmpFileName, const std::string& jpegFileName) { // Open the BMP file for reading std::ifstream bmpFile(bmpFileName, std::ios::binary); if (!bmpFile.is_open()) { std::cerr << "Failed to open BMP file: " << bmpFileName << std::endl; return; } // Read the BMP header BMPHeader bmpHeader; bmpFile.read(reinterpret_cast<char*>(&bmpHeader), sizeof(BMPHeader)); // Check the magic number to confirm that this is a BMP file if (bmpHeader.magicNumber != BMP_MAGIC_NUMBER) { std::cerr << "Invalid BMP file: " << bmpFileName << std::endl; return; } // Read the DIB header DIBHeader dibHeader; bmpFile.read(reinterpret_cast<char*>(&dibHeader), sizeof(DIBHeader)); // Check that the BMP is uncompressed if (dibHeader.compression != 0) { std::cerr << "Compressed BMP is not supported: " << bmpFileName << std::endl; return; } // Check that the BMP is 24-bit (3 bytes per pixel) if (dibHeader.bitsPerPixel != 24) { std::cerr << "Only 24-bit BMP is supported: " << bmpFileName << std::endl; return; } // Calculate the number of padding bytes per row int rowSize = (dibHeader.width * 3 + 3) & ~3; int paddingSize = rowSize - dibHeader.width * 3; // Read the pixel data into a vector std::vector<Pixel> pixels(dibHeader.width * dibHeader.height); for (int y = 0; y < dibHeader.height; ++y) { for (int x = 0; x < dibHeader.width; ++x) { bmpFile.read(reinterpret_cast<char*>(&pixels[y * dibHeader.width + x]), 3); } // Skip the padding bytes bmpFile.seekg(paddingSize, std::ios::cur); } // Close the BMP file bmpFile.close(); // Open the JPEG file for writing std::ofstream jpegFile(jpegFileName, std::ios::binary); if (!jpegFile.is_open()) { std::cerr << "Failed to open JPEG file: " << jpegFileName << std::endl; return; } // Write the JPEG magic number to the file jpegFile.write(reinterpret_cast<char*>(&JPEG_MAGIC_NUMBER), 2); // Write the pixel data to the JPEG file for (int y = 0; y < dibHeader.height; ++y) { for (int x = 0; x < dibHeader.width; ++x) { jpegFile.write(reinterpret_cast<char*>(&pixels[y * dibHeader.width + x]), 3); } } // Close the JPEG file jpegFile.close(); std::cout << "Converted BMP file to JPEG: " << bmpFileName << " -> " << jpegFileName << std::endl; Code #include <iostream> #include <fstream> #include <vector> const int JPEG_MAGIC_NUMBER = 0xFFD8; const int BMP_MAGIC_NUMBER = 0x4D42; struct BMPHeader { int magicNumber; int fileSize; int reserved; int dataOffset; }; struct DIBHeader { int headerSize; int width; int height; short planes; short bitsPerPixel; int compression; int imageSize; int xPixelsPerMeter; int yPixelsPerMeter; int colorsUsed; int importantColors; }; struct Pixel { unsigned char blue; unsigned char green; unsigned char red; }; // Converts a BMP image to a JPEG image void BMPToJPEG(const std::string& bmpFileName, const std::string& jpegFileName) { // Open the BMP file for reading std::ifstream bmpFile(bmpFileName, std::ios::binary); if (!bmpFile.is_open()) { std::cerr << "Failed to open BMP file: " << bmpFileName << std::endl; return; } // Read the BMP header BMPHeader bmpHeader; bmpFile.read(reinterpret_cast<char*>(&bmpHeader), sizeof(BMPHeader)); // Check the magic number to confirm that this is a BMP file if (bmpHeader.magicNumber != BMP_MAGIC_NUMBER) { std::cerr << "Invalid BMP file: " << bmpFileName << std::endl; return; } // Read the DIB header DIBHeader dibHeader; bmpFile.read(reinterpret_cast<char*>(&dibHeader), sizeof(DIBHeader)); // Check that the BMP is uncompressed if (dibHeader.compression != 0) { std::cerr << "Compressed BMP is not supported: " << bmpFileName << std::endl; return; } // Check that the BMP is 24-bit (3 bytes per pixel) if (dibHeader.bitsPerPixel != 24) { std::cerr << "Only 24-bit BMP is supported: " << bmpFileName << std::endl; return; } // Calculate the number of padding bytes per row int rowSize = (dibHeader.width * 3 + 3) & ~3; int paddingSize = rowSize - dibHeader.width * 3; // Read the pixel data into a vector std::vector<Pixel> pixels(dibHeader.width * dibHeader.height); for (int y = 0; y < dibHeader.height; ++y) { for (int x = 0; x < dibHeader.width; ++x) { bmpFile.read(reinterpret_cast<char*>(&pixels[y * dibHeader.width + x]), 3); } // Skip the padding bytes bmpFile.seekg(paddingSize, std::ios::cur); } // Close the BMP file bmpFile.close(); // Open the JPEG file for writing std::ofstream jpegFile(jpegFileName, std::ios::binary); if (!jpegFile.is_open()) { std::cerr << "Failed to open JPEG file: " << jpegFileName << std::endl; return; } // Write the JPEG magic number to the file jpegFile.write(reinterpret_cast<char*>(&JPEG_MAGIC_NUMBER), 2); // Write the pixel data to the JPEG file for (int y = 0; y < dibHeader.height; ++y) { for (int x = 0; x < dibHeader.width; ++x) { jpegFile.write(reinterpret_cast<char*>(&pixels[y * dibHeader.width + x]), 3); } } // Close the JPEG file jpegFile.close(); std::cout << "Converted BMP file to JPEG: " << bmpFileName << " -> " << jpegFileName << std::endl;
leha_zabor, чтобы конвертировать JPEG в BMP, можно выполнить аналогичный процесс, но в обратном порядке. Нужно будет прочитать magic число JPEG, а затем прочитать данные пикселей из файла JPEG и записать данные пикселей в файл BMP вместе с необходимыми заголовками BMP и DIB.
Onlyinc, #include <complex> #include <iostream> #include <vector> // Cooley-Tukey FFT (in-place, divide-and-conquer) void fft(std::vector<std::complex<double>> &x) { const size_t N = x.size(); if (N <= 1) return; // divide std::vector<std::complex<double>> even(N / 2); std::vector<std::complex<double>> odd(N / 2); for (size_t k = 0; k < N / 2; ++k) { even[k] = x[2 * k]; odd[k] = x[2 * k + 1]; } // conquer fft(even); fft(odd); // combine for (size_t k = 0; k < N / 2; ++k) { std::complex<double> t = std::polar(1.0, -2 * M_PI * k / N) * odd[k]; x[k] = even[k] + t; x[k + N / 2] = even[k] - t; } } int main() { // test input (random real values) std::vector<std::complex<double>> x = {1, 2, 3, 4}; // perform DFT fft(x); // print results std::cout << "DFT of x: " << std::endl; for (size_t i = 0; i < x.size(); ++i) { std::cout << x[i] << std::endl; } return 0; } C #include <complex> #include <iostream> #include <vector> // Cooley-Tukey FFT (in-place, divide-and-conquer) void fft(std::vector<std::complex<double>> &x) { const size_t N = x.size(); if (N <= 1) return; // divide std::vector<std::complex<double>> even(N / 2); std::vector<std::complex<double>> odd(N / 2); for (size_t k = 0; k < N / 2; ++k) { even[k] = x[2 * k]; odd[k] = x[2 * k + 1]; } // conquer fft(even); fft(odd); // combine for (size_t k = 0; k < N / 2; ++k) { std::complex<double> t = std::polar(1.0, -2 * M_PI * k / N) * odd[k]; x[k] = even[k] + t; x[k + N / 2] = even[k] - t; } } int main() { // test input (random real values) std::vector<std::complex<double>> x = {1, 2, 3, 4}; // perform DFT fft(x); // print results std::cout << "DFT of x: " << std::endl; for (size_t i = 0; i < x.size(); ++i) { std::cout << x[i] << std::endl; } return 0; }