0

I have extracted a function that jpegtran used to flip a JPEG image verbatim (lossless). I have added my code to read file into a byte array, my goal is to write a function which takes a JPEG image in byte array and return flipped JPEG in byte array as well. The source code is as follows (this is the farthest I can go right now):

#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? 
    
}

I am currently having trouble with the function flip_vertical at the bottom, how to call do_flip_v and return the byte array of the flipped jpeg image? As a novice at libjpeg, I have been tortured for days. Thank you in advance!!!

tripleee
  • 175,061
  • 34
  • 275
  • 318
l.yang
  • 11
  • 2
  • Does this work? 1. Find the required size for the destination array, 2. allocate destination array, 3. call `do_flip_v()`, 4. return destination array – VLL Feb 02 '22 at 11:07
  • @vll I'm afraid not, because the four parameters that `do_flip_v` takes are specific to libjpeg which seems to be some data structures derived from JPEG decompression. – l.yang Feb 02 '22 at 15:55

0 Answers0