use crate::jpeg::data::Block8x8;

pub fn write_block_1x1<T: Copy>(
    output: &mut [T],
    output_stride: usize,
    dx: usize,
    dy: usize,
    block: &Block8x8<T>,
) {
    for by in 0..8 {
        let block_row = &block.data[by * 8..by * 8 + 8];
        let output_offset = (dy + by) * output_stride + dx;
        if output_offset + 8 > output.len() {
            break;
        }
        output[output_offset..output_offset + 8].copy_from_slice(block_row);
    }
}

pub fn write_block_1x2<T: Copy>(
    output: &mut [T],
    output_stride: usize,
    dx: usize,
    dy: usize,
    block: &Block8x8<T>,
) {
    for by in 0..16 {
        let block_row = &block.data[by / 2 * 8..by / 2 * 8 + 8];
        let output_offset = (dy + by) * output_stride + dx;
        if output_offset + 8 > output.len() {
            break;
        }
        output[output_offset..output_offset + 8].copy_from_slice(block_row);
    }
}

pub fn write_block_2x1<T: Copy>(
    output: &mut [T],
    output_stride: usize,
    dx: usize,
    dy: usize,
    block: &Block8x8<T>,
) {
    for by in 0..8 {
        let block_row = &block.data[by * 8..by * 8 + 8];
        for bx in 0..16 {
            let output_offset = (dy + by) * output_stride + (dx + bx);
            if output_offset >= output.len() {
                break;
            }
            output[output_offset] = block_row[bx / 2];
        }
    }
}

pub fn write_block_2x2<T: Copy>(
    output: &mut [T],
    output_stride: usize,
    dx: usize,
    dy: usize,
    block: &Block8x8<T>,
) {
    for by in 0..16 {
        let block_row = &block.data[by / 2 * 8..by / 2 * 8 + 8];
        for bx in 0..16 {
            let output_offset = (dy + by) * output_stride + (dx + bx);
            if output_offset >= output.len() {
                break;
            }
            output[output_offset] = block_row[bx / 2];
        }
    }
}
