static int save_rgb_buffer_as_ppm(const char *fn, unsigned char* data, int width,
		int height, int stride, camera_frametype_t frameType)
{
	int rc=0;
	char header[256];
	uint8_t *rgb_scanline=NULL;

	int lenheader = snprintf(header, sizeof header, "P6\n%d %d\n255\n", width, height);

	int fd = open(fn, O_RDWR|O_CREAT|O_TRUNC, 0660);

	if (fd == -1) {
		perror("save_vf_buffer()/open():");
		rc = -1;
		goto cleanup;
	}

	write(fd, header, lenheader);

	rgb_scanline = (uint8_t*)malloc(3*width);
	if (rgb_scanline == NULL) {
		fprintf(stderr, "out of memory while allocating rgb_scanline\n");
		rc = -1;
		goto cleanup;
	}
	for (int i=0; i<height; i++) {
		if (frameType == CAMERA_FRAMETYPE_RGB8888) {
			bgra_to_rgb(rgb_scanline, data, width);
		}
		rc = write(fd, rgb_scanline, 3*width);
		if (rc != 3*width) {
			printf("%s(): write(%d) returns %d\n", __func__, width, rc);
			break;
		}
		data += stride;
	}

cleanup:
	close(fd);
	free(rgb_scanline);

	return rc;
}
Пример #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";
    }
};
Пример #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);
}