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 56 57 58 59 60 61 62 63 64 65
| #include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include "turbojpeg.h"
int main(int argc, char** argv)
{
const int JPEG_QUALITY = 95;
const int COLOR_COMPONENTS = 3;
int width = 720;
int height = 560;
unsigned long size = 0;
unsigned char* compressedImage = NULL;
unsigned char buffer[width * height * COLOR_COMPONENTS];
for (int x = 0; x < 720; x++)
for (int y = 0; y < 560; y++)
{
switch ((x / 80) % 3)
{
case 0:
buffer[(y * 720 + x) * 3] = 255;
buffer[(y * 720 + x) * 3 + 1] = 0;
buffer[(y * 720 + x) * 3 + 2] = 0;
break;
case 1:
buffer[(y * 720 + x) * 3] = 0;
buffer[(y * 720 + x) * 3 + 1] = 255;
buffer[(y * 720 + x) * 3 + 2] = 0;
break;
case 2:
buffer[(y * 720 + x) * 3] = 0;
buffer[(y * 720 + x) * 3 + 1] = 0;
buffer[(y * 720 + x) * 3 + 2] = 255;
break;
}
}
tjhandle compressor = tjInitCompress();
compressedImage = tjAlloc(width * height * COLOR_COMPONENTS);
size = width * height * COLOR_COMPONENTS;
tjCompress2(compressor, buffer, width, 0, height, TJPF_RGB, &compressedImage, &size, TJSAMP_444, JPEG_QUALITY, TJFLAG_NOREALLOC | TJFLAG_ACCURATEDCT);
tjDestroy(compressor);
FILE * fichier = fopen("file.jpeg", "wb");
printf("LENGTH=%ld\n", size);
if (fwrite(compressedImage, size, 1,fichier) != size)
printf("Write Error.\n");
fclose(fichier);
tjFree(compressedImage);
return 0;
} |
Partager