Blame view

JpegUtils/pyjpeg/test.c 1.48 KB
1f1943eb   qijun   initial commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "jpeg.h"
#include <string.h>

void flip_in_place(JCOEFPTR ptr1) {
  for (int k = 0; k < DCTSIZE2; k+=2) {
    ptr1[k+1] = -ptr1[k+1];
  }
}

int main(int argc, char **argv) {
  if (argc < 3) {
    fprintf(stderr, "no filename supplied\n");
    return 1;
  }
  FILE *infile;
  const char *filename = argv[1];
  if ((infile = fopen(filename, "rb")) == NULL) {
    fprintf(stderr, "can't open %s\n", filename);
    return 1;
  }
  struct jpeg_stuff stuff;
  readjpeg(infile, &stuff);

  int comps = stuff.cinfo.num_components;
  for (int comp = 0; comp < comps; comp++) {
    int maxy = stuff.cinfo.comp_info[comp].height_in_blocks;
    int maxx = stuff.cinfo.comp_info[comp].width_in_blocks;
    fprintf(stdout,"component %d width: %d height: %d\n", comp, maxx, maxy);
    for (int y = 0; y < maxy; y ++) {
      for (int x = 0; 2 * x < maxx; x++) {
        JCOEF tmp[DCTSIZE2];
        JCOEF tmp2[DCTSIZE2];
        JCOEFPTR ptr;
        ptr = get_coeff_block(&stuff, x, y, comp);
        flip_in_place(ptr);
        memcpy(tmp, ptr, BLOCKSIZE);
        ptr = get_coeff_block(&stuff, maxx - x - 1, y, comp);
        flip_in_place(ptr);
        memcpy(tmp2, ptr, BLOCKSIZE);
        memcpy(ptr, tmp, BLOCKSIZE);
        ptr = get_coeff_block(&stuff, x, y, comp);
        memcpy(ptr, tmp2, BLOCKSIZE);
      }
    }
  }
      
  fprintf(stdout, "done writing\n");

  FILE *outfile = fopen(argv[2], "wb");
  writejpeg(outfile, &stuff);
  fclose(outfile);
  closejpeg(&stuff);
  fclose(infile);
  return 0;
}