0

我提取了一个jpegtran用于逐字翻转 JPEG 图像(无损)的函数。我已经添加了将文件读入字节数组的代码,我的目标是编写一个函数,该函数在字节数组中获取 JPEG 图像,并在字节数组中返回翻转的 JPEG。源码如下(这是我目前能走的最远的):

#include "libjpeg.h"
#include <stdio.h>
#include <stdlib.h>

void do_flip_v(j_decompress_ptr srcinfo, j_compress_ptr dstinfo,
    jvirt_barray_ptr *src_coef_arrays, jvirt_barray_ptr *dst_coef_arrays)
{
    /* Vertical flip */
    JDIMENSION MCU_rows, comp_height, dst_blk_x, dst_blk_y;
    int ci, i, j, offset_y;
    JBLOCKARRAY src_buffer, dst_buffer;
    JBLOCKROW src_row_ptr, dst_row_ptr;
    JCOEFPTR src_ptr, dst_ptr;
    jpeg_component_info *compptr;

    MCU_rows = dstinfo->image_height / (dstinfo->max_v_samp_factor * DCTSIZE);

    for (ci = 0; ci < dstinfo->num_components; ci++) {
        compptr = dstinfo->comp_info + ci;
        comp_height = MCU_rows * compptr->v_samp_factor;
        for (dst_blk_y = 0; dst_blk_y < compptr->height_in_blocks;
        dst_blk_y += compptr->v_samp_factor) {
            dst_buffer = (*srcinfo->mem->access_virt_barray)
                ((j_common_ptr) srcinfo, dst_coef_arrays[ci], dst_blk_y,
                (JDIMENSION) compptr->v_samp_factor, TRUE);
            if (dst_blk_y < comp_height) {
                /* Row is within the mirrorable area. */
                src_buffer = (*srcinfo->mem->access_virt_barray)
                    ((j_common_ptr) srcinfo, src_coef_arrays[ci],
                    comp_height - dst_blk_y - (JDIMENSION)
                    compptr->v_samp_factor,
                    (JDIMENSION) compptr->v_samp_factor, FALSE);
            }
            else {
                /* Bottom-edge blocks will be copied verbatim. */
                src_buffer = (*srcinfo->mem->access_virt_barray)
                    ((j_common_ptr) srcinfo, src_coef_arrays[ci], dst_blk_y,
                    (JDIMENSION) compptr->v_samp_factor, FALSE);
            }
            for (offset_y = 0; offset_y < compptr->v_samp_factor; offset_y++) {
                if (dst_blk_y < comp_height) {
                    /* Row is within the mirrorable area. */
                    dst_row_ptr = dst_buffer[offset_y];
                    src_row_ptr =
                        src_buffer[compptr->v_samp_factor - offset_y - 1];
                    for (dst_blk_x = 0; dst_blk_x < compptr->width_in_blocks;
                    dst_blk_x++) {
                        dst_ptr = dst_row_ptr[dst_blk_x];
                        src_ptr = src_row_ptr[dst_blk_x];
                        for (i = 0; i < DCTSIZE; i += 2) {
                            /* copy even row */
                            for (j = 0; j < DCTSIZE; j++)
                                * dst_ptr++ = *src_ptr++;
                            /* copy odd row with sign change */
                            for (j = 0; j < DCTSIZE; j++)
                                * dst_ptr++ = - *src_ptr++;
                        }
                    }
                }
                else {
                    /* Just copy row verbatim. */
                    jcopy_block_row(src_buffer[offset_y], dst_buffer[offset_y],
                        compptr->width_in_blocks);
                }
            }
        }
    }
}

int main(int argc, char *argv){
    FILE *fp = fopen("test.jpg", "r");
    fseek(fp, 0, SEEK_END);
    int len = ftell(fp);
    char *buf = malloc(len);
    fseek(fp, 0, SEEK_SET);
    fread(buf, 1, len, fp);
    fclose(fp);
    
    // calling the flip function.
    char *flipped = flip_vertical(buf);
}

(char *) flip_vertical(char *in_stream){
    // How to call do_flip_v and return the byte array of the flipped jpeg image? 
    
}

我目前在使用底部的函数flip_vertical时遇到问题,如何调用do_flip_v并返回翻转后的jpeg图像的字节数组?作为 libjpeg 的新手,我已经被折磨了好几天。先感谢您!!!

4

0 回答 0