Problem: BMP Image Weighted Feature Calculation

The problem requires creating C++ functions to process BMP files—locating them across paths, parsing headers, skipping 8-bit palettes, and computing weighted metrics (effective dimensions, pixel count, dual Gaussian average) from intensity and spatial weights, returning rounded or normalized results.


Input/Output: The problem takes a string input representing the filename of a BMP file. It locates the file in predefined paths, parses and validates BMP file and info headers (checking "BM" signature), skips the 1024-byte color palette for 8-bit BMPs, and extracts pixel data. The problem outputs four results: an integer effective width (from weighted x-center using squared normalized intensity weights + 0.1, 2*absolute difference between weighted and image center + half width, rounded), an integer effective height (similar y-axis logic), an integer weighted pixel count (combining Gaussian spatial and intensity weights, scaled to original range, rounded), and a double dual Gaussian mixture average (convex combination of two Gaussians with distinct centers/sigmas, normalized). The input filename maps to these outputs via the respective computations.

Required dependencies:
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <cmath>
#include <iomanip>
#include <cstdlib>
#include <algorithm>
using namespace std;

Sub-steps:
step_number: 1
step_description_prompt: Please write a C++ function named problem_1_1 that calculates the weighted effective width of a BMP image based on pixel intensity distribution. The function takes a const string& filename as input and returns an int.

The function signature is: int problem_1_1(const string& filename);

Algorithm steps: 
1. Try to open the file using possible paths: "test_data/" + filename, "../test_data/" + filename, "../../test_data/" + filename, "substep/test_data/" + filename, and filename itself. 
2. Define BMPFileHeader and BMPInfoHeader structures with #pragma pack(push, 2) and #pragma pack(pop) for byte alignment. Read these headers from the file. 
3. Check if the file is a valid BMP (BMPFileHeader.bfty[0] == 'B' and bfty[1] == 'M'). 
4. For 8-bit BMP (BMPInfoHeader.bibitcount == 8), skip 1024 bytes of color palette. 
5. Calculate aligned_width as ((width + 3) / 4) * 4 (width from BMPInfoHeader.biwidth). Read pixel data into a buffer (note: BMP stores rows from bottom to top). 
6. Compute weighted sum for x-coordinates: for each pixel, weight = ((double)val / 255.0) * ((double)val / 255.0) + 0.1 (val is pixel intensity). Sum j * weight (weighted_sum_x) and weight (weight_sum) (j is x-coordinate, actual_row = height - 1 - i where height is abs(BMPInfoHeader.biheight)). 
7. Compute weighted_center_x = (weight_sum > 1e-10) ? (weighted_sum_x / weight_sum) : (width / 2.0). 
8. Calculate effective_width as 2.0 * abs(weighted_center_x - width / 2.0) + width * 0.5. 
9. Round effective_width to nearest integer (add 0.5 then cast to int) and return. Free the pixel buffer before returning.

Handle file reading by trying multiple paths. Follow BMP format rules (row order, palette for 8-bit BMP). Use included headers like <fstream>, <vector>, <cmath>, etc. (assume required headers are included).
function_header: int problem_1_1(const string& filename)
test_cases: // ==================== Main function: Testing ====================
int main() {
    srand(42);
    cout << fixed << setprecision(6);
    cout << "Problem 1.1" << endl;
    
    int correct_count = 0;
    
    // Test 1
    int correct1 = 1026;  // Expected value (updated from actual output)
    string x1 = "1.bmp";
    int result1 = problem_1_1(x1);
    if (result1 == correct1) {
        cout << "Correct" << endl;
        correct_count++;
    } else {
        cout << "Wrong" << endl;
        cout << "Expected: " << correct1 << ", Got: " << result1 << endl;
    }
    
    // Test 2
    int correct2 = 1040;  // Expected value (updated from actual output)
    string x2 = "a.bmp";
    int result2 = problem_1_1(x2);
    if (result2 == correct2) {
        cout << "Correct" << endl;
        correct_count++;
    } else {
        cout << "Wrong" << endl;
        cout << "Expected: " << correct2 << ", Got: " << result2 << endl;
    }
    
    // Test 3
    int correct3 = 1026;  // Expected value (updated from actual output)
    string x3 = "b.bmp";
    int result3 = problem_1_1(x3);
    if (result3 == correct3) {
        cout << "Correct" << endl;
        correct_count++;
    } else {
        cout << "Wrong" << endl;
        cout << "Expected: " << correct3 << ", Got: " << result3 << endl;
    }
    
    if (correct_count == 3) {
        cout << "Completely correct!" << endl;
    }
    
    return 0;
}

step_number: 2
step_description_prompt: Please write a C++ function named problem_1_2 that calculates the weighted effective height of a BMP image based on pixel intensity distribution. The function takes a const string& filename and returns an int.

Function definition: int problem_1_2(const string& filename);

Algorithm flow:
1. Check multiple possible paths (test_data/filename, ../test_data/filename, ../../test_data/filename, substep/test_data/filename, filename) to find the BMP file; if no path is valid, output an error and return -1.
2. Open the file in binary mode, read BMPFileHeader and BMPInfoHeader. Validate the file is a BMP by checking if bfty[0] is 'B' and bfty[1] is 'M'; return 0 if invalid.
3. Get image width (biwidth) and height (absolute value of biheight). For 8-bit BMP (bibitcount == 8), skip 1024 bytes (palette) using seekg.
4. Calculate aligned_width as ((width + 3) / 4) * 4, allocate an unsigned char array of size aligned_width * height, then read pixel data into this array.
5. Compute weighted sum: iterate i from 0 to height-1 and j from 0 to width-1. Set actual_row = height - 1 - i (adjust for BMP's bottom-to-top storage). Get pixel value val = pixels[actual_row * aligned_width + j]. Compute weight as ((double)val / 255.0) * ((double)val / 255.0) + 0.1. Accumulate weighted_sum_y += i * weight and weight_sum += weight.
6. Calculate weighted_center_y as weight_sum > 1e-10 ? weighted_sum_y / weight_sum : height / 2.0. Compute effective_height = 2.0 * abs(weighted_center_y - height / 2.0) + height * 0.5.
7. Delete the pixel array. Round effective_height to the nearest integer (add 0.5 then cast to int) and return the result.

Note: The function uses binary mode for file operations. Assume BMPFileHeader, BMPInfoHeader, vector, fstream, string, cmath, and cstdlib are available. Handle memory allocation/deallocation for the pixel array. Ensure pixel indices are valid during iteration.</think>Please write a C++ function named problem_1_2 that calculates the weighted effective height of a BMP image based on its pixel intensity distribution. The function takes a const string& filename and returns an int.

Function definition: int problem_1_2(const string& filename);

Algorithm flow:
1. Check multiple possible paths (test_data/filename, ../test_data/filename, ../../test_data/filename, substep/test_data/filename, filename) to find the BMP file; if no valid path exists, output an error and return -1.
2. Open the file in binary mode, read BMPFileHeader and BMPInfoHeader. Validate the file is a BMP by checking if bfty[0] == 'B' and bfty[1] == 'M'; return 0 if invalid.
3. Get image width (biwidth) and height (absolute value of biheight). For 8-bit BMP (bibitcount == 8), skip 1024 bytes (palette) using seekg.
4. Calculate aligned_width as ((width + 3) / 4) * 4, allocate an unsigned char array of size aligned_width * height, then read pixel data into this array.
5. Compute weighted sums: iterate i from 0 to height-1 and j from 0 to width-1. Set actual_row = height - 1 - i (adjust for BMP's bottom-to-top storage). Get pixel value val = pixels[actual_row * aligned_width + j]. Compute weight as ((double)val / 255.0) * ((double)val / 255.0) + 0.1. Accumulate weighted_sum_y += i * weight and weight_sum += weight.
6. Calculate weighted_center_y as weight_sum > 1e-10 ? weighted_sum_y / weight_sum : height / 2.0. Compute effective_height = 2.0 * abs(weighted_center_y - height / 2.0) + height * 0.5.
7. Delete the pixel array. Round effective_height to the nearest integer (add 0.5 then cast to int) and return the result.

Note: The function uses binary mode for file operations. Assume BMPFileHeader, BMPInfoHeader, vector, fstream, string, cmath, and cstdlib are available. Handle memory allocation/deallocation for the pixel array. Ensure pixel indices are valid during iteration.
function_header: int problem_1_2(const string& filename)
test_cases: // ==================== Main function: Testing ====================
int main() {
    srand(42);
    cout << fixed << setprecision(6);
    cout << "Problem 1.2" << endl;
    
    int correct_count = 0;
    
    // Test 1
    int correct1 = 1764;  // Expected value (updated from actual output)
    string x1 = "1.bmp";
    int result1 = problem_1_2(x1);
    if (result1 == correct1) {
        cout << "Correct" << endl;
        correct_count++;
    } else {
        cout << "Wrong" << endl;
        cout << "Expected: " << correct1 << ", Got: " << result1 << endl;
    }
    
    // Test 2
    int correct2 = 1729;  // Expected value (updated from actual output)
    string x2 = "a.bmp";
    int result2 = problem_1_2(x2);
    if (result2 == correct2) {
        cout << "Correct" << endl;
        correct_count++;
    } else {
        cout << "Wrong" << endl;
        cout << "Expected: " << correct2 << ", Got: " << result2 << endl;
    }
    
    // Test 3
    int correct3 = 1764;  // Expected value (updated from actual output)
    string x3 = "b.bmp";
    int result3 = problem_1_2(x3);
    if (result3 == correct3) {
        cout << "Correct" << endl;
        correct_count++;
    } else {
        cout << "Wrong" << endl;
        cout << "Expected: " << correct3 << ", Got: " << result3 << endl;
    }
    
    if (correct_count == 3) {
        cout << "Completely correct!" << endl;
    }
    
    return 0;
}

step_number: 3
step_description_prompt: Please write a C++ function named problem_1_3 that takes a const string& filename and returns an int; it calculates a weighted pixel count for a BMP image using spatial Gaussian weighting and intensity-based weighting, then scales the result to the original pixel count range and rounds to the nearest integer.
The function is defined as: int problem_1_3(const string& filename);
Algorithm flow: 1. Check possible paths (test_data/ + filename, ../test_data/ + filename, ../../test_data/ + filename, substep/test_data/ + filename, filename) to find the BMP file; open the file in binary mode. If no path is valid, output error and return -1. 2. Read BMPFileHeader and BMPInfoHeader structures (use #pragma pack(push,2) and #pragma pack(pop) to ensure correct struct packing). 3. Validate BMP signature (fileHeader.bfty must be 'B' and 'M'). 4. For 8-bit BMP, skip the 1024-byte color palette. 5. Calculate aligned width ((width +3)/4 *4), read pixel data into a dynamically allocated unsigned char array (note: BMP stores pixels from bottom to top). 6. Compute weighted_count: iterate over each pixel (i from 0 to height-1, j from 0 to width-1), get actual_row = height-1 -i. Calculate spatial weight using Gaussian formula with sigma_spatial = max(width, height)/4.0 (dist_sq = (i - center_i)^2 + (j - center_j)^2, center_i = height/2.0, center_j = width/2.0). Calculate intensity weight as ((val/255.0)*(val/255.0) + 0.1). Combine weights (product) and sum to weighted_count. 7. Scale weighted_count by (width*height)/(weighted_count + 1e-10). 8. Round to nearest integer (add 0.5 then cast to int). 9. Delete the pixel array and return the result.
Handle BMP file reading in binary mode, use the specified path checks, and follow BMP's pixel storage order (bottom-to-top). Assume any called functions are already defined.
function_header: int problem_1_3(const string& filename)
test_cases: // ==================== Main function: Testing ====================
int main() {
    srand(42);
    cout << fixed << setprecision(6);
    cout << "Problem 1.3" << endl;
    
    int correct_count = 0;
    
    // Test 1
    int correct1 = 6717440;  // Expected value
    string x1 = "1.bmp";
    int result1 = problem_1_3(x1);
    if (result1 == correct1) {
        cout << "Correct" << endl;
        correct_count++;
    } else {
        cout << "Wrong" << endl;
    }
    
    // Test 2
    int correct2 = 6717440;  // Expected value
    string x2 = "a.bmp";
    int result2 = problem_1_3(x2);
    if (result2 == correct2) {
        cout << "Correct" << endl;
        correct_count++;
    } else {
        cout << "Wrong" << endl;
    }
    
    // Test 3
    int correct3 = 6717440;  // Expected value
    string x3 = "b.bmp";
    int result3 = problem_1_3(x3);
    if (result3 == correct3) {
        cout << "Correct" << endl;
        correct_count++;
    } else {
        cout << "Wrong" << endl;
    }
    
    if (correct_count == 3) {
        cout << "Completely correct!" << endl;
    }
    
    return 0;
}

step_number: 4
step_description_prompt: Please write a C++ function named problem_1_4 that takes a const string& filename as input and returns a double, which computes the dual Gaussian mixture weighted average of a BMP image. The function signature is double problem_1_4(const string& filename);. 

The algorithm flow: 1. Check multiple possible paths (test_data/ + filename, ../test_data/ + filename, ../../test_data/ + filename, substep/test_data/ + filename, filename) to find the existing BMP file. 2. Open the file in binary mode, read BMPFileHeader and BMPInfoHeader, verify if the file is a valid BMP (check if bfty[0] is 'B' and bfty[1] is 'M'). 3. Get image width (biwidth) and height (absolute value of biheight). 4. If the BMP is 8-bit, skip the 1024-byte color palette. 5. Calculate aligned width as ((width + 3)/4)*4, read pixel data (aligned_width * height bytes), note that BMP stores pixels from bottom to top (actual_row = height-1 -i). 6. For dual Gaussian mixture: first Gaussian center at (height/3.0, width/3.0) with sigma = max(width, height)/5.0 and mix weight 0.6; second Gaussian center at (2.0*height/3.0, 2.0*width/3.0) with sigma = max(width, height)/3.0 and mix weight 0.4. For each pixel (i,j), compute combined weight as 0.6*exp(-((i-center1_i)^2 + (j-center1_j)^2)/(2.0*sigma1*sigma1)) + 0.4*exp(-((i-center2_i)^2 + (j-center2_j)^2)/(2.0*sigma2*sigma2)). 7. Compute weighted sum of pixel values and total weight, return sum/weight_sum if weight_sum >1e-10, else return 0.0. 

Other functions used are assumed to be defined. Handle file reading and opening errors.
function_header: double problem_1_4(const string& filename)
test_cases: // ==================== Main function: Testing ====================
int main() {
    srand(42);
    cout << fixed << setprecision(6);
    cout << "Problem 1.4" << endl;
    
    int correct_count = 0;
    const double tolerance = 1e-5;
    
    // Test 1
    double correct1 = 205.748102;  // Expected value (updated from actual output)
    string x1 = "1.bmp";
    double result1 = problem_1_4(x1);
    if (abs(result1 - correct1) < tolerance) {
        cout << "Correct" << endl;
        correct_count++;
    } else {
        cout << "Wrong" << endl;
        cout << "Expected: " << fixed << setprecision(6) << correct1 << ", Got: " << result1 << endl;
    }
    
    // Test 2
    double correct2 = 240.819421;  // Expected value (updated from actual output)
    string x2 = "a.bmp";
    double result2 = problem_1_4(x2);
    if (abs(result2 - correct2) < tolerance) {
        cout << "Correct" << endl;
        correct_count++;
    } else {
        cout << "Wrong" << endl;
        cout << "Expected: " << fixed << setprecision(6) << correct2 << ", Got: " << result2 << endl;
    }
    
    // Test 3
    double correct3 = 205.750294;  // Expected value (updated from actual output)
    string x3 = "b.bmp";
    double result3 = problem_1_4(x3);
    if (abs(result3 - correct3) < tolerance) {
        cout << "Correct" << endl;
        correct_count++;
    } else {
        cout << "Wrong" << endl;
        cout << "Expected: " << fixed << setprecision(6) << correct3 << ", Got: " << result3 << endl;
    }
    
    if (correct_count == 3) {
        cout << "Completely correct!" << endl;
    }
    
    return 0;
}

General solution:

General tests: