Esempio n. 1
0
int main(int argc, char **argv) {
    if (argc < 5) {
        printf("Usage: rgba2rgb <input file> <output file> <width> <height>\n");
        exit(1);
    }
    char *inputf = argv[1];
    char *outputf = argv[2];
    int width = atoi(argv[3]);
    int height = atoi(argv[4]);

    FILE *input = fopen(inputf, "r");
    if (!input) {
        printf("Could not open %s input file\n", inputf);
        exit(1);
    }
    FILE *output = fopen(outputf, "w+");
    if (!output) {
        printf("Could not open %s output file\n", outputf);
        exit(1);
    }

    size_t rgba_size = width*height*4;
    size_t rgb_size = width*height*3;

    unsigned char *rgba_buf = malloc(sizeof(unsigned char)*rgba_size);
    if (!rgba_buf) {
        printf("Could not malloc mem for rgba_buf\n");
        exit(1);
    }

    unsigned char *rgb_buf = malloc(sizeof(unsigned char)*rgb_size);
    if (!rgb_buf) {
        printf("Could not malloc rgb_buf\n");
        exit(1);
    }

    size_t n = fread(rgba_buf, sizeof(unsigned char), rgba_size, input);
    if (n != rgba_size) {
        printf("Didn't read %d bytes (width*height*4), read %d\n", rgba_size, n);
        exit(1);
    }

    rgba_to_rgb(rgba_buf, rgb_buf, width, height);

    if (fwrite(rgb_buf, sizeof(unsigned char), rgb_size, output) != rgb_size) {
        printf("Failed writing file output file %s, %s\n", outputf,
            strerror(errno));
        exit(1);
    }

    free(rgba_buf);
    free(rgb_buf);
    fclose(input);
    fclose(output);

    return 0;
}
Esempio n. 2
0
RGBator::RGBator(unsigned char *data, int width, int height, buffer_type buf_type) {
    memory = (GifByteType *)malloc(sizeof(GifFileType)*width*height*3);
    if (!memory) throw "malloc in RGBator::RGBator failed";
    red = memory;
    green = memory + width*height;
    blue = memory + width*height*2;

    switch (buf_type) {
    case BUF_RGB:
        rgb_to_rgb(data, width, height);
        break;
    case BUF_BGR:
        bgr_to_rgb(data, width, height);
        break;
    case BUF_RGBA:
        rgba_to_rgb(data, width, height);
        break;
    case BUF_BGRA:
        bgra_to_rgb(data, width, height);
        break;
    default:
        throw "Unexpected buf_type in RGBator::RGBator";
    }
};
Esempio n. 3
0
void
JpegEncoder::encode()
{
    struct jpeg_compress_struct cinfo;
    struct jpeg_error_mgr jerr;

    cinfo.err = jpeg_std_error(&jerr);

    jpeg_create_compress(&cinfo);
    jpeg_mem_dest(&cinfo, &jpeg, &jpeg_len);

    if (offset.isNull()) {
        cinfo.image_width = width;
        cinfo.image_height = height;
    }
    else {
        cinfo.image_width = offset.w;
        cinfo.image_height = offset.h;
    }
    cinfo.input_components = 3;
    cinfo.in_color_space = JCS_RGB;

    jpeg_set_defaults(&cinfo);
    jpeg_set_quality(&cinfo, quality, TRUE);
    cinfo.smoothing_factor = smoothing;
    jpeg_start_compress(&cinfo, TRUE);

    unsigned char *rgb_data;
    switch (buf_type) {
    case BUF_RGBA:
        rgb_data = rgba_to_rgb(data, width*height*4);
        if (!rgb_data) throw "malloc failed in JpegEncoder::encode/rgba_to_rgb.";
        break;

    case BUF_BGRA:
        rgb_data = bgra_to_rgb(data, width*height*4);
        if (!rgb_data) throw "malloc failed in JpegEncoder::encode/bgra_to_rgb.";
        break;

    case BUF_BGR:
        rgb_data = bgr_to_rgb(data, width*height*3);
        if (!rgb_data) throw "malloc failed in JpegEncoder::encode/bgr_to_rgb.";
        break;

    case BUF_RGB:
        rgb_data = data;
        break;

    default:
        throw "Unexpected buf_type in JpegEncoder::encode";
    }

    JSAMPROW row_pointer;
    int start = 0;
    if (!offset.isNull()) {
        start = offset.y*width*3 + offset.x*3;
    }
    while (cinfo.next_scanline < cinfo.image_height) {
        row_pointer = &rgb_data[start + cinfo.next_scanline*3*width];
        jpeg_write_scanlines(&cinfo, &row_pointer, 1);
    }

    jpeg_finish_compress(&cinfo);
    jpeg_destroy_compress(&cinfo);

    if (buf_type == BUF_BGR || buf_type == BUF_RGBA || buf_type == BUF_BGRA)
        free(rgb_data);
}