Example #1
0
bool R9_ImgWriteJPG( F9FILE file, r9Img* img )
{
	if(file==NULL || img==NULL) return false;
	if(!R9_ImgIsValid(img)) return false;
	if(R9_PFSpp(img->m_pf)!=3) return false; // 24bpp / 3bytes

	R9_ImgFlipRGB(img); // jpegs are BGR

	JFWRITE = R9_ImgJPG_WriteData;
	JFFLUSH = R9_ImgJPG_Flush;
	JFERROR = R9_ImgJPG_Error;

	jpeg_compress_struct cinfo;
	jpeg_error_mgr jerr;

	cinfo.err = jpeg_std_error(&jerr);	// set up the normal JPEG error routines.
	cinfo.client_data = file;			// set the file into the client_data

	// Now we can initialize the JPEG decompression object.
	jpeg_create_compress(&cinfo); // !!!

	jpeg_stdio_dest(&cinfo);

	// Params for compression
	cinfo.image_width = img->m_width;
	cinfo.image_height = img->m_height;
	cinfo.input_components = 3;
	cinfo.in_color_space = JCS_RGB;
	
	jpeg_set_defaults(&cinfo);
	jpeg_set_quality(&cinfo, r9_imgjpg_quality, TRUE); // limit to baseline-JPEG values

	// Start compressor
	jpeg_start_compress(&cinfo, TRUE);

	JSAMPROW row_pointer[1];
	int row_stride;	// physical row width in image buffer
	row_stride = img->m_width * 3;

	while (cinfo.next_scanline < cinfo.image_height) 
	{
		row_pointer[0] = & img->m_data[cinfo.next_scanline * row_stride];
		(void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
	}

	// Finish compression
	jpeg_finish_compress(&cinfo);
	jpeg_destroy_compress(&cinfo);

	R9_ImgFlipRGB(img); // back to RGB
	return true;
}
Example #2
0
bool ImageLoader::writeJpg(const std::string& filename, const std::vector<Uint8>& pixels, unsigned int width, unsigned int height)
{
#ifdef EMULATION
    // Open the file to write in
    FILE* file = fopen(filename.c_str(), "wb");
    if (!file)
        return false;

    // Initialize the error handler
    jpeg_compress_struct compressInfos;
    jpeg_error_mgr errorManager;
    compressInfos.err = jpeg_std_error(&errorManager);

    // Initialize all the writing and compression infos
    jpeg_create_compress(&compressInfos);
    compressInfos.image_width      = width;
    compressInfos.image_height     = height;
    compressInfos.input_components = 3;
    compressInfos.in_color_space   = JCS_RGB;
    jpeg_stdio_dest(&compressInfos, file);
    jpeg_set_defaults(&compressInfos);
    jpeg_set_quality(&compressInfos, 90, TRUE);

    // Get rid of the aplha channel
    std::vector<Uint8> buffer(width * height * 3);
    for (std::size_t i = 0; i < width * height; ++i)
    {
        buffer[i * 3 + 0] = pixels[i * 4 + 0];
        buffer[i * 3 + 1] = pixels[i * 4 + 1];
        buffer[i * 3 + 2] = pixels[i * 4 + 2];
    }
    Uint8* ptr = &buffer[0];

    // Start compression
    jpeg_start_compress(&compressInfos, TRUE);

    // Write each row of the image
    while (compressInfos.next_scanline < compressInfos.image_height)
    {
        JSAMPROW rawPointer = ptr + (compressInfos.next_scanline * width * 3);
        jpeg_write_scanlines(&compressInfos, &rawPointer, 1);
    }

    // Finish compression
    jpeg_finish_compress(&compressInfos);
    jpeg_destroy_compress(&compressInfos);

    // Close the file
    fclose(file);
#endif
    return true;
}
Example #3
0
static bool write_jpeg(struct image_writer_ctx *ctx, mp_image_t *image, FILE *fp)
{
    struct jpeg_compress_struct cinfo;
    struct jpeg_error_mgr jerr;

    cinfo.err = jpeg_std_error(&jerr);
    jerr.error_exit = write_jpeg_error_exit;

    jmp_buf error_return_jmpbuf;
    cinfo.client_data = &error_return_jmpbuf;
    if (setjmp(cinfo.client_data)) {
        jpeg_destroy_compress(&cinfo);
        return false;
    }

    jpeg_create_compress(&cinfo);
    jpeg_stdio_dest(&cinfo, fp);

    cinfo.image_width = image->w;
    cinfo.image_height = image->h;
    cinfo.input_components = 3;
    cinfo.in_color_space = JCS_RGB;

    cinfo.write_JFIF_header = TRUE;
    cinfo.JFIF_major_version = 1;
    cinfo.JFIF_minor_version = 2;

    jpeg_set_defaults(&cinfo);
    jpeg_set_quality(&cinfo, ctx->opts->jpeg_quality, ctx->opts->jpeg_baseline);
    cinfo.optimize_coding = ctx->opts->jpeg_optimize;
    cinfo.smoothing_factor = ctx->opts->jpeg_smooth;

    if (ctx->opts->jpeg_source_chroma) {
        cinfo.comp_info[0].h_samp_factor = 1 << ctx->original_format.chroma_xs;
        cinfo.comp_info[0].v_samp_factor = 1 << ctx->original_format.chroma_ys;
    }

    jpeg_start_compress(&cinfo, TRUE);

    while (cinfo.next_scanline < cinfo.image_height) {
        JSAMPROW row_pointer[1];
        row_pointer[0] = image->planes[0] +
                         cinfo.next_scanline * image->stride[0];
        jpeg_write_scanlines(&cinfo, row_pointer,1);
    }

    jpeg_finish_compress(&cinfo);

    jpeg_destroy_compress(&cinfo);

    return true;
}
Example #4
0
/**
  * @brief  Jpeg Encode
  * @param  file:          pointer to the bmp file
  * @param  file1:         pointer to the jpg file  
  * @param  width:         image width
  * @param  height:        image height
  * @param  image_quality: image quality
  * @param  buff:          pointer to the image line
  * @retval None
  */
void jpeg_encode(FIL *file, FIL *file1, uint32_t width, uint32_t height, uint32_t image_quality, uint8_t * buff)
{ 
    
  /* Encode BMP Image to JPEG */  
  JSAMPROW row_pointer;    /* Pointer to a single row */
  uint32_t bytesread;
            
  /* Step 1: allocate and initialize JPEG compression object */
  /* Set up the error handler */
  cinfo.err = jpeg_std_error(&jerr);
  
  /* Initialize the JPEG compression object */  
  jpeg_create_compress(&cinfo);
  
  /* Step 2: specify data destination */
  jpeg_stdio_dest(&cinfo, file1);
  
  /* Step 3: set parameters for compression */
  cinfo.image_width = width;
  cinfo.image_height = height;
  cinfo.input_components = 3;
  cinfo.in_color_space = JCS_RGB;
  
  /* Set default compression parameters */
  jpeg_set_defaults(&cinfo);
  
  cinfo.dct_method  = JDCT_FLOAT;    
  
  jpeg_set_quality(&cinfo, image_quality, TRUE);
  
  /* Step 4: start compressor */
  jpeg_start_compress(&cinfo, TRUE);
  
  f_read(file, buff, 54, (UINT*)&bytesread);
  
  while (cinfo.next_scanline < cinfo.image_height)
  {          
    
    if(f_read(file, buff, width*3, (UINT*)&bytesread) == FR_OK)
    {
      row_pointer = (JSAMPROW)buff;
      jpeg_write_scanlines(&cinfo, &row_pointer, 1);          
    }
  }
  /* Step 5: finish compression */
  jpeg_finish_compress(&cinfo);
  
  /* Step 6: release JPEG compression object */
  jpeg_destroy_compress(&cinfo);
    
}
Example #5
0
void image_write_jpg(const char *name, int w, int h, int c, int b, void *p)
{
    FILE *fp;

    assert(name);
    assert(p);

    if ((fp = fopen(name, "wb")))
    {
        struct jpeg_compress_struct cinfo;
        struct jpeg_error_mgr       jerr;

        unsigned char *s[1];

        /* Initialize the JPG compressor. */

        cinfo.err = jpeg_std_error(&jerr);

        jpeg_create_compress(&cinfo);
        jpeg_stdio_dest(&cinfo, fp);

        /* Set the JPG header info. */

        cinfo.image_width      = w;
        cinfo.image_height     = h;
        cinfo.input_components = c;

        if (c == 1) cinfo.in_color_space = JCS_GRAYSCALE;
        if (c == 3) cinfo.in_color_space = JCS_RGB;

        jpeg_set_defaults  (&cinfo);
        jpeg_set_quality   (&cinfo, 75, TRUE);
        jpeg_start_compress(&cinfo,     TRUE);

        /* Write each scanline in turn. */

        while (cinfo.next_scanline < cinfo.image_height)
        {
            s[0] = (unsigned char *) p + w * c * cinfo.next_scanline;
            jpeg_write_scanlines(&cinfo, s, 1);
        }

        /* Finalize the compression. */

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

        fclose(fp);
    }
    else fail(name, strerror(errno));
}
Example #6
0
/**
  * @brief  JPEG compression routine.
  * @param  file: JPEG file 
  * @param  in_params : input parameter structure.
  * @retval Status
  */
int32_t jpeg_compress(FIL *file, InParams_Typedef  *in_params)
{
  static struct jpeg_compress_struct cinfo; /* This struct contains the JPEG compression parameters */
  struct jpeg_error_mgr jerr; /* This struct represents a JPEG error handler */
  JSAMPROW row_pointer; /* Pointer to a single row */
  uint32_t row_stride = 0; /* Physical row width in image buffer */

  /* Step 1: allocate and initialize JPEG compression object */
  /* Set up the error handler */
  cinfo.err = jpeg_std_error(&jerr);

  /* Initialize the JPEG compression object */  
  jpeg_create_compress(&cinfo);

  /* Step 2: specify data destination */
  jpeg_stdio_dest(&cinfo, file);

  /* Step 3: set parameters for compression */
  cinfo.image_width = in_params->in_width;
  cinfo.image_height = in_params->in_height;
  cinfo.input_components = in_params->in_bpp;
  cinfo.in_color_space = in_params->in_colorspace;
 
  /* Set default compression parameters */
  jpeg_set_defaults(&cinfo);

#ifdef USE_STM324xG_EVAL
  cinfo.dct_method  = JDCT_FLOAT;    
#elif defined (USE_STM322xG_EVAL)
  cinfo.dct_method  = JDCT_IFAST;
#endif  
  jpeg_set_quality(&cinfo, in_params->in_imagequality, TRUE);

  /* Step 4: start compressor */
  jpeg_start_compress(&cinfo, TRUE);

  row_stride = cinfo.image_width * cinfo.input_components;
  while (cinfo.next_scanline < cinfo.image_height)
  {
    row_pointer = (JSAMPROW)&in_params->in_buff[cinfo.next_scanline * row_stride];
    jpeg_write_scanlines(&cinfo, &row_pointer, 1);
  }

  /* Step 5: finish compression */
  jpeg_finish_compress(&cinfo);

  /* Step 6: release JPEG compression object */
  jpeg_destroy_compress(&cinfo);

  return 0;
}
Example #7
0
bool ImageMemory::_SaveJpgImage(const std::string &filename) const
{
    FILE *fp = fopen(filename.c_str(), "wb");
    if(fp == NULL) {
        IF_PRINT_WARNING(VIDEO_DEBUG) << "could not open file: " << filename << std::endl;
        return false;
    }

    // we don't support RGBA because JPEGs don't support alpha
    if(rgb_format == false) {
        IF_PRINT_WARNING(VIDEO_DEBUG) << "attempting to save non-RGB format pixel data as a RGB format JPG image" << std::endl;
    }

    // compression object and error handling
    jpeg_compress_struct cinfo;
    jpeg_error_mgr jerr;

    cinfo.err = jpeg_std_error(&jerr);
    jpeg_create_compress(&cinfo);

    // tell libjpeg how we do things in this town
    cinfo.in_color_space = JCS_RGB;
    cinfo.image_width = width;
    cinfo.image_height = height;
    cinfo.input_components = 3;

    // everything else can be default
    jpeg_set_defaults(&cinfo);
    // tell it where to look
    jpeg_stdio_dest(&cinfo, fp);
    // compress it
    jpeg_start_compress(&cinfo, TRUE);

    JSAMPROW row_pointer; // A pointer to a single row
    uint32 row_stride = width * 3; // The physical row width in the buffer (RGB)

    // Note that the lines have to be stored from top to bottom
    while(cinfo.next_scanline < cinfo.image_height) {
        row_pointer = (uint8 *)pixels + cinfo.next_scanline * row_stride;
        jpeg_write_scanlines(&cinfo, &row_pointer, 1);
    }

    // compression is DONE, we are HAPPY
    // now finish it and clean up
    jpeg_finish_compress(&cinfo);
    jpeg_destroy_compress(&cinfo);

    fclose(fp);

    return true;
} // bool ImageMemory::_SaveJpgImage(const std::string& file_name) const
Example #8
0
int WriteJpgStream(FILE *file,
                   const std::vector<unsigned char> & array,
                   int w,
                   int h,
                   int depth,
                   int quality) {
  if (quality < 0 || quality > 100)
    std::cerr << "Error: The quality parameter should be between 0 and 100";

  struct jpeg_compress_struct cinfo;
  struct jpeg_error_mgr jerr;

  cinfo.err = jpeg_std_error(&jerr);
  jpeg_create_compress(&cinfo);
  jpeg_stdio_dest(&cinfo, file);

  cinfo.image_width = w;
  cinfo.image_height = h;
  cinfo.input_components = depth;

  if (cinfo.input_components==3) {
    cinfo.in_color_space = JCS_RGB;
  } else if (cinfo.input_components==1) {
    cinfo.in_color_space = JCS_GRAYSCALE;
  } else {
    std::cerr << "Error: Unsupported number of channels in file";
    jpeg_destroy_compress(&cinfo);
    return 0;
  }

  jpeg_set_defaults(&cinfo);
  jpeg_set_quality(&cinfo, quality, TRUE);
  jpeg_start_compress(&cinfo, TRUE);

  const unsigned char *ptr = &array[0];
  int row_bytes = cinfo.image_width*cinfo.input_components;

  JSAMPLE *row = new JSAMPLE[row_bytes];

  while (cinfo.next_scanline < cinfo.image_height) {
    std::memcpy(&row[0], &ptr[0], row_bytes * sizeof(unsigned char));
    jpeg_write_scanlines(&cinfo, &row, 1);
    ptr += row_bytes;
  }

  delete [] row;

  jpeg_finish_compress(&cinfo);
  jpeg_destroy_compress(&cinfo);
  return 1;
}
Example #9
0
int write_jpeg(struct image image, char *filename) {
	int rc = 0, width = 0, height = 0, row_stride = 0;
	unsigned char *data = NULL;
	FILE *file = NULL;
	struct jpeg_compress_struct cinfo;
	struct jpeg_error_mgr jerr;
	JSAMPROW row_pointer[1];

	file = fopen(filename, "wb");

	if (!file) {
		printf("Could not open %s\n", filename);

		rc = 1;
		goto cleanup;
	}

	cinfo.err = jpeg_std_error(&jerr);
	jpeg_create_compress(&cinfo);
	jpeg_stdio_dest(&cinfo, file);

	data = image.data;
	width = image.width;
	height = image.height;

	cinfo.image_width = width;
	cinfo.image_height = height;
	cinfo.input_components = 3;
	cinfo.in_color_space = JCS_RGB;
	jpeg_set_defaults(&cinfo);
	jpeg_set_quality(&cinfo, 100, TRUE);
	jpeg_start_compress(&cinfo, TRUE);

	row_stride = cinfo.input_components * cinfo.image_width;

	while (cinfo.next_scanline < cinfo.image_height) {
		row_pointer[0] = (JSAMPROW)&data[cinfo.next_scanline * row_stride];
		jpeg_write_scanlines(&cinfo, (JSAMPARRAY)&row_pointer, 1);
	}

	jpeg_finish_compress(&cinfo);

cleanup:
	if (file) {
		fclose(file);
	}

	jpeg_destroy_compress(&cinfo);

	return rc;
}
Example #10
0
int	open_output_file(char *output_name, FILE **outfile,
			 struct jpeg_compress_struct *cinfo)
{
  if ((*outfile = fopen(output_name, "wb")) == NULL)
    {
      my_printf("can't open %s\n", output_name);
      return (-1);
    }
  jpeg_create_compress(cinfo);
  jpeg_stdio_dest(cinfo, *outfile);
  cinfo->input_components = 3;
  cinfo->in_color_space = JCS_RGB;
  return (0);
}
Example #11
0
static FILE *setup_output(const char *outputname, j_compress_ptr outputinfo)
{
    FILE * output_file = fopen(outputname, "wb");
    if (output_file == NULL) {
        perror(outputname);
        exit(EXIT_FAILURE);
    }
    
    outputinfo->err = jpeg_std_error(&jerr);
    jpeg_create_compress(outputinfo);
    jpeg_stdio_dest(outputinfo, output_file);
    
    return output_file;
}
Example #12
0
void save_jpeg_buffer(buffer_t &buf, std::string filename, int quality) {
  FILE *f; // target file

  struct jpeg_compress_struct cinfo;
  struct jpeg_error_mgr jerr;
  
  // allocate and initialize JPEG compression object
  cinfo.err = jpeg_std_error(&jerr);
  jpeg_create_compress(&cinfo);

  // specify data destination
  if((f = fopen(filename.c_str(), "wb")) == NULL) {
    fprintf(stderr, "can't open %s\n", filename.c_str());
    exit(1);
  }
  jpeg_stdio_dest(&cinfo, f);

  // set parameters
  cinfo.image_width = buf.extent[0];
  cinfo.image_height = buf.extent[1];
  if((buf.extent[2] != 1) || (buf.extent[2] != 3)) {
    fprintf(stderr, "the buffer does not have a valid number of channels\n");
    exit(1);
  }
  cinfo.input_components = buf.extent[2];
  cinfo.in_color_space = ((buf.extent[2] == 1) ? JCS_GRAYSCALE : JCS_RGB);
  jpeg_set_defaults(&cinfo);
  jpeg_set_quality(&cinfo, quality, TRUE);

  // start compression
  jpeg_start_compress(&cinfo, TRUE);
  JSAMPROW row_pointer[buf.extent[0]*buf.extent[2]]; // points to JSAMPLE row
  while(cinfo.next_scanline < cinfo.image_height) {
    // copy pixel pointers to row_pointer
    for(int j = 0; j < buf.extent[0]; j++) {
      for(int i = 0; i < buf.extent[2]; i++)
	row_pointer[j*buf.extent[2] + i] = 
	  (JSAMPLE *) (buf.host + cinfo.next_scanline*buf.stride[1]
		    + j + i*buf.stride[2]);
    }
    (void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
  }

  // finish compression
  jpeg_finish_compress(&cinfo);
  fclose(f);
  jpeg_destroy_compress(&cinfo);

  return;
}
    /** \brief Constructor.
     *
     * \param filename File to open.
     */
    JpegWriter(const std::string &filename)
    {
      m_fd = fopen(filename.c_str(), "wb");
      if(NULL == m_fd)
      {
        std::stringstream sstr;
        sstr << "could not open '" << filename << "' for writing";
        BOOST_THROW_EXCEPTION(std::runtime_error(sstr.str()));
      }

      m_compress.err = jpeg_std_error(&m_err);
      jpeg_create_compress(&m_compress);
      jpeg_stdio_dest(&m_compress, m_fd);
    }
Example #14
0
void saveToJpeg( char* filename, TDIBImage* FBitmap){
    struct jpeg_compress_struct cinfo;
	struct jpeg_error_mgr jerr;
	cinfo.err = jpeg_std_error(&jerr);
	jpeg_create_compress(&cinfo);

    FILE * outfile;
	if ((outfile = fopen(filename, "wb")) == NULL) {
	    fprintf(stderr, "can't open %s\n", filename);
	    exit(1);
	}
	jpeg_stdio_dest(&cinfo, outfile);

    cinfo.image_width = FBitmap->Width; 	/* image width and height, in pixels */
	cinfo.image_height = FBitmap->Height;
    JSAMPROW row_pointer[1];	/* pointer to a single row */
	int row_stride;			/* physical row width in buffer */

    if ( FBitmap->DIBInfo.bmiHeader.biBitCount == 8 ){
	    cinfo.input_components = 1;	/* # of color components per pixel */
        cinfo.in_color_space = JCS_GRAYSCALE;
        row_stride = cinfo.image_width;
    }
    else if ( FBitmap->DIBInfo.bmiHeader.biBitCount == 24 ){
        cinfo.input_components = 3;
        cinfo.in_color_space = JCS_RGB;  /* colorspace of input image */
        row_stride = cinfo.image_width * 3;    /* JSAMPLEs per row in image_buffer */
    }
    else
        throw Exception ( "Format is not supported" );

	jpeg_set_defaults(&cinfo);
	/* Make optional parameter settings here */

    FBitmap->OpenPixels();
    jpeg_start_compress(&cinfo, TRUE);
    unsigned char* ScanBuffer = new unsigned char [ row_stride ];
	while (cinfo.next_scanline < cinfo.image_height) {
	    row_pointer[0] = (JSAMPROW)FBitmap->ScanLine[cinfo.next_scanline];
        memcpy( ScanBuffer, row_pointer[0], row_stride );
        rgb_to_bgr( ScanBuffer, cinfo.image_width );
	    jpeg_write_scanlines(&cinfo, &ScanBuffer, 1);
	}
    FBitmap->ClosePixels();
    jpeg_finish_compress(&cinfo);
    jpeg_destroy_compress(&cinfo);
    fclose( outfile );
    delete[] ScanBuffer;
};
Example #15
0
void
Bitmap::write_jpg(const std::string& filename)
{
  struct jpeg_compress_struct cinfo;
  struct jpeg_error_mgr jerr;

  /* More stuff */
  FILE * outfile;		/* target file */

  cinfo.err = jpeg_std_error(&jerr);
  /* Now we can initialize the JPEG compression object. */
  jpeg_create_compress(&cinfo);

  if ((outfile = fopen(filename.c_str(), "wb")) == NULL)
    {
      std::cerr << "can't open "  << filename << std::endl;
      return;
    }
  jpeg_stdio_dest(&cinfo, outfile);

  cinfo.image_width  = get_width();
  cinfo.image_height = get_height();
  cinfo.input_components = 1;	//3	/* # of color components per pixel */
  cinfo.in_color_space = JCS_GRAYSCALE; /* colorspace of input image */

  jpeg_set_defaults(&cinfo);

  jpeg_set_quality(&cinfo, 85, TRUE /* limit to baseline-JPEG values */);

  /* TRUE ensures that we will write a complete interchange-JPEG file.
   * Pass TRUE unless you are very sure of what you're doing. */
  jpeg_start_compress(&cinfo, TRUE);

  JSAMPROW row_pointer[get_height()];	/* pointer to JSAMPLE row[s] */

  for(int y = 0; y < get_height(); ++y)
    row_pointer[y] = &buffer[y * get_width()];

  while (cinfo.next_scanline < cinfo.image_height)
    {
      jpeg_write_scanlines(&cinfo, row_pointer, get_height());
    }

  jpeg_finish_compress(&cinfo);

  fclose(outfile);

  jpeg_destroy_compress(&cinfo);
}
Example #16
0
int main(void) {

	int width = DIM;
	int height = DIM;
	int quality = 75;
	char *scr=(char*)malloc(width*height*3);
	FILE* outfile = fopen("out.jpg", "wb");
	for (int i=0;i<width;i++)
		for (int j=0;j<height;j++) {
			char *p = scr + j * width * 3 + i * 3;
			*p = julia(i, j) * 255;
			p++;
			*p = 0;
			p++;
			*p = 0;
		}

	/* Compress to JPEG */
	struct jpeg_compress_struct cinfo;
	struct jpeg_error_mgr jerr;
	cinfo.err = jpeg_std_error(&jerr);
	jpeg_create_compress(&cinfo);

	jpeg_stdio_dest(&cinfo, outfile);

	cinfo.image_width=width;
	cinfo.image_height=height;
	cinfo.input_components=3;
	cinfo.in_color_space=JCS_RGB;

	jpeg_set_defaults(&cinfo);
	jpeg_set_quality(&cinfo, quality, true);
	jpeg_start_compress(&cinfo, TRUE);

	JSAMPROW row_pointer[1];
	int row_stride;

	row_stride = cinfo.image_width*3;

	while (cinfo.next_scanline < cinfo.image_height) {
		row_pointer[0]=(JSAMPLE *)(scr+cinfo.next_scanline*row_stride);
		jpeg_write_scanlines(&cinfo, row_pointer, 1);
	}
	jpeg_finish_compress(&cinfo);
	jpeg_destroy_compress(&cinfo);

	free(scr);
	return 0;
}
Example #17
0
static int write_jpeg(VInfo *ji, uint8_t *buffer, int quality, FILE *x) {
  uint8_t *line;
  int n, y = 0, i, line_width;

  struct jpeg_compress_struct cjpeg;
  struct jpeg_error_mgr jerr;
  JSAMPROW row_ptr[1];

  line = malloc(ji->out_width * 3);
  if (!line) {
    dlog(DLOG_CRIT, "IMF: OUT OF MEMORY, Exiting...\n");
    exit(1);
  }
  cjpeg.err = jpeg_std_error(&jerr);
  jpeg_create_compress (&cjpeg);
  cjpeg.image_width  = ji->out_width;
  cjpeg.image_height = ji->out_height;
  cjpeg.input_components = 3;
  //cjpeg.smoothing_factor = 0; // 0..100
  cjpeg.in_color_space = JCS_RGB;

  jpeg_set_defaults (&cjpeg);
  jpeg_set_quality (&cjpeg, quality, TRUE);
  cjpeg.dct_method = quality > 90? JDCT_DEFAULT : JDCT_FASTEST;

  jpeg_simple_progression(&cjpeg);

  jpeg_stdio_dest (&cjpeg, x);
  jpeg_start_compress (&cjpeg, TRUE);
  row_ptr[0] = line;
  line_width = ji->out_width * 3;
  n = 0;

  for (y = 0; y < ji->out_height; y++)
    {
      for (i = 0; i< line_width; i += 3)
	{
	  line[i]   = buffer[n];
	  line[i+1] = buffer[n+1];
	  line[i+2] = buffer[n+2];
	  n += 3;
	}
      jpeg_write_scanlines (&cjpeg, row_ptr, 1);
    }
  jpeg_finish_compress (&cjpeg);
  jpeg_destroy_compress (&cjpeg);
  free(line);
  return(0);
}
Example #18
0
int fg_frame_save( fg_frame* fr, const char* filename )
{

    if (fr->format != FG_FORMAT_RGB24)
    {
        fg_debug_error("fg_frame_save(): failed because format is not RGB24");
        return -1;
    }

    FILE *outfile;

    if ( (outfile = fopen(filename, "wb")) == NULL)
    {
        fg_debug_error("fg_frame_save(): unable to open output file");
        return -1;
    }

    struct jpeg_compress_struct cinfo;
    struct jpeg_error_mgr jerr;
    JSAMPROW row_pointer[1];

    cinfo.err = jpeg_std_error(&jerr);
    jpeg_create_compress(&cinfo);
    jpeg_stdio_dest(&cinfo, outfile);

    cinfo.in_color_space = JCS_RGB;
    jpeg_set_defaults(&cinfo);

    cinfo.image_width = fr->size.width;
    cinfo.image_height = fr->size.height;
    cinfo.input_components = 3;
    cinfo.in_color_space = JCS_RGB;
    jpeg_set_defaults(&cinfo);

    jpeg_set_quality(&cinfo, 80, FALSE);

    jpeg_start_compress(&cinfo, TRUE);

    while (cinfo.next_scanline < cinfo.image_height)
    {
        row_pointer[0] = &(fr->data[cinfo.next_scanline*fr->rowstride]);
        jpeg_write_scanlines(&cinfo, row_pointer, 1);
    }

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

    return 0;
}
Example #19
0
void img_save_video(int width, int height, const char *name,unsigned char *image)
{
	//GLubyte *image = (GLubyte *)malloc(width*height*sizeof(GLubyte)*3);
	/*
	unsigned char *image = (unsigned char *)malloc(width*height*sizeof(unsigned char)*3);
	glPixelStorei(GL_PACK_ALIGNMENT, 1);
	glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, image);
	*/

	int quality = 100;
	struct jpeg_compress_struct cinfo;
	struct jpeg_error_mgr jerr;
 
 	FILE * outfile;		
	JSAMPROW row_pointer[1];
	int row_stride;		

	cinfo.err = jpeg_std_error(&jerr);
	jpeg_create_compress(&cinfo);

	if ((outfile = fopen(name, "wb")) != NULL) 
	{
		jpeg_stdio_dest(&cinfo, outfile);
		
		cinfo.image_width = width; 
		cinfo.image_height = height;
		cinfo.input_components = 3;	
		cinfo.in_color_space = JCS_RGB;
	 
		jpeg_set_defaults(&cinfo);
		jpeg_set_quality(&cinfo, quality, TRUE);
		jpeg_start_compress(&cinfo, TRUE);

		row_stride = width * 3;	

		while (cinfo.next_scanline < cinfo.image_height) 
		{
			row_pointer[0] = & image[(cinfo.image_height-1-cinfo.next_scanline) * row_stride];
			(void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
		}

		jpeg_finish_compress(&cinfo);
		fclose(outfile);

		jpeg_destroy_compress(&cinfo);
	//	free(image);

	}
}
Example #20
0
void img_save_jpg_highres(int width, int height, const char name[],GLubyte *image)
{
	int quality = 100;
	struct jpeg_compress_struct cinfo;
	struct jpeg_error_mgr jerr;
 
 	FILE * outfile;		
	JSAMPROW row_pointer[1];
	int row_stride;		

	cinfo.err = jpeg_std_error(&jerr);
	jpeg_create_compress(&cinfo);

	if ( (int)strlen(name) == 0) name="./data/screenshot_hd.jpg";
	printf("path %s\n",name);
 
	if ((outfile = fopen(name, "wb")) != NULL) 
	{
  	
		jpeg_stdio_dest(&cinfo, outfile);
		
		cinfo.image_width = width; 
		cinfo.image_height = height;
		cinfo.input_components = 3;	
		cinfo.in_color_space = JCS_RGB;
	 
		jpeg_set_defaults(&cinfo);
		jpeg_set_quality(&cinfo, quality, TRUE);
		jpeg_start_compress(&cinfo, TRUE);

		row_stride = width * 3;	

		while (cinfo.next_scanline < cinfo.image_height) 
		{
			row_pointer[0] = & image[(cinfo.image_height-1-cinfo.next_scanline) * row_stride];
			(void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
		}

		jpeg_finish_compress(&cinfo);
		fclose(outfile);

		jpeg_destroy_compress(&cinfo);
		free(image);
	}
	else
	{
		printf("can't save image\n");
	}
}
Example #21
0
/***************************************************************
Write XImage data to a JPEG file

Must include <jpeglib.h>
Return value:
0 - failed
1 - success
****************************************************************/
int JpegWriteFileFromImage(char *filename, XImage* img)
{
	FILE* fp;
	struct jpeg_compress_struct cinfo;
	struct jpeg_error_mgr jerr;

	fp = fopen(filename,"wb");
	if(fp==NULL)
	{
		return 0;
	}

	cinfo.err = jpeg_std_error(&jerr);
	jpeg_create_compress(&cinfo);

	jpeg_stdio_dest(&cinfo,fp);
	cinfo.image_width = img->width;
	cinfo.image_height = img->height;
	cinfo.input_components = 3;
	cinfo.in_color_space = JCS_RGB;

	jpeg_set_defaults(&cinfo);
	jpeg_start_compress(&cinfo,TRUE);

	JSAMPROW row_pointer[1];       /* pointer to scanline */
	unsigned char* pBuf = (unsigned char*)malloc(3*img->width);
	row_pointer[0] = pBuf;

	int i=0;
	while(cinfo.next_scanline < cinfo.image_height)
	{
		int j=0;
		for(i=0;i<img->width;i++)
		{
			*(pBuf+j) = *(img->data+cinfo.next_scanline*img->bytes_per_line+i*4+2);
			*(pBuf+j+1) = *(img->data+cinfo.next_scanline*img->bytes_per_line+i*4+1);
			*(pBuf+j+2) = *(img->data+cinfo.next_scanline*img->bytes_per_line+i*4);
			j+=3;
		}
		jpeg_write_scanlines(&cinfo,row_pointer,1);
	}

	free(pBuf);
	jpeg_finish_compress(&cinfo);
	jpeg_destroy_compress(&cinfo);

	fclose(fp);
	return 1;
}
Example #22
0
int
write_jpeg_image(char *fn, Pixel (*frame)[MAX_Y][MAX_X]) {
	FILE *jpegout;
	struct jpeg_compress_struct cinfo;
	struct jpeg_error_mgr jerr;
	int x, y;

	cinfo.err = jpeg_std_error(&jerr);
	jpeg_create_compress(&cinfo);

	if (fn)
		jpegout = fopen(fn, "wb");
	else {
		int fd = mkstemps("XXX.jpeg", 5);
		jpegout = fdopen(fd, "w");
	}
	if (jpegout == NULL) {
		perror("can't open jpeg");
		return 0;
	}

	jpeg_stdio_dest(&cinfo, jpegout);
	cinfo.image_width = MAX_X;      /* image width and height, in pixels */
	cinfo.image_height = MAX_Y;
	cinfo.input_components = 3;     /* # of color components per pixel */
	cinfo.in_color_space = JCS_RGB; /* colorspace of input image */
	cinfo.dct_method = JDCT_ISLOW;	/* slow, high accuracy encoding */
	jpeg_set_defaults(&cinfo);
	jpeg_set_quality (&cinfo, 100, 1);	/* highest quality encoding, w. baseline */
	jpeg_start_compress(&cinfo, TRUE);

	for (y=0; y<MAX_Y; y++) {
		Pixel *in_row = &((*frame)[MAX_Y - y - 1][0]);
		JSAMPLE row[MAX_X*3];
		JSAMPROW row_pointer[1];
		row_pointer[0] = &row[0];

		for (x=0; x<MAX_X; x++) {
			row[x*3]   = in_row[x].r;
			row[x*3+1] = in_row[x].g;
			row[x*3+2] = in_row[x].b;
		}
		jpeg_write_scanlines(&cinfo, row_pointer, 1);
	}
        jpeg_finish_compress(&cinfo);
        fclose(jpegout);
	jpeg_destroy_compress(&cinfo);
	return 1;
}
Example #23
0
int R2Image::
WriteJPEG(const char *filename) const
{
#ifndef OMIT_JPEG
  // Open file
  FILE *fp = fopen(filename, "wb");
  if (!fp) {
    fprintf(stderr, "Unable to open image file: %s", filename);
    return 0;
  }

  // Initialize compression info
  struct jpeg_compress_struct cinfo;
  struct jpeg_error_mgr jerr;
  cinfo.err = jpeg_std_error(&jerr);
  jpeg_create_compress(&cinfo);
  jpeg_stdio_dest(&cinfo, fp);
  cinfo.image_width = width; 	/* image width and height, in pixels */
  cinfo.image_height = height;
  cinfo.input_components = ncomponents;		/* # of color components per pixel */
  cinfo.in_color_space = JCS_RGB; 	/* colorspace of input image */
  cinfo.dct_method = JDCT_ISLOW;
  jpeg_set_defaults(&cinfo);
  cinfo.optimize_coding = TRUE;
  jpeg_set_quality(&cinfo, 75, TRUE);
  jpeg_start_compress(&cinfo, TRUE);
	
  // Output scan lines
  // First jpeg pixel is top-left, so write in opposite scan-line order
  while (cinfo.next_scanline < cinfo.image_height) {
    int scanline = cinfo.image_height - cinfo.next_scanline - 1;
    unsigned char *row_pointer = &pixels[scanline * rowsize];
    jpeg_write_scanlines(&cinfo, &row_pointer, 1);
  }

  // Free everything
  jpeg_finish_compress(&cinfo);
  jpeg_destroy_compress(&cinfo);

  // Close file
  fclose(fp);

  // Return number of bytes written
  return 1;
#else
  RNFail("JPEG not supported");
  return 0;
#endif
}
Example #24
0
int write_image(const char *filename, const struct image_t *img, struct config_t *cfg)
{
	struct jpeg_compress_struct cinfo;
	struct jpeg_error_mgr jerr;
	int quality;

	char *str_quality = NULL;
	if ((str_quality = CFG_GetValue(cfg, "jpeg_quality")) == NULL)
		quality = DEFAULT_QUALITY;
	else
		quality = atoi(str_quality);

	if (quality <= 0 || quality > 100)
		quality = DEFAULT_QUALITY;

	cinfo.err = jpeg_std_error(&jerr);
	jpeg_create_compress(&cinfo);

	FILE *out = fopen(filename, "wb");
	if (out == NULL) {
		WARN(errno, "Could not open output file");
		return -errno;
	}
	jpeg_stdio_dest(&cinfo, out);

	cinfo.image_width = img->width;
	cinfo.image_height = img->height;
	cinfo.input_components = img->num_pixel_components;
	cinfo.in_color_space = writer_to_libjpeg_format(img->format);

	jpeg_set_defaults(&cinfo);
	jpeg_set_quality(&cinfo, quality, TRUE);

	jpeg_start_compress(&cinfo, TRUE);

	JSAMPROW scanline[1] = {(JSAMPROW)img->data};
	while (cinfo.next_scanline < cinfo.image_height) {
		jpeg_write_scanlines(&cinfo, scanline, 1);
		scanline[0] = (JSAMPROW)img->data + cinfo.next_scanline * img->width * img->num_pixel_components;
	}

	jpeg_finish_compress(&cinfo);
	fclose(out);

	jpeg_destroy_compress(&cinfo);
	
	return 0;
}
Example #25
0
/**
int  write_jpeg_file(const char* fname, JPEGImage jp, int qulty)

jp の画像データを fnameに書き出す.

@param  fname  ファイル名
@param  jp     保存する JPEGデータ
@param  qulty  保存のクオリティ 0〜100  100が最高画質

@retval 0                   正常終了
@retval ERROR_GRAPH_OPFILE  ファイルオープンエラー
@retval ERROR_GRAPH_HEADER  不正ファイル(JPEGファイルでない?)
@retval ERROR_GRAPH_MEMORY  メモリエラー
@retval ERROR_GRAPH_NODATA  jp にデータが無い
@retval ERROR_GRAPH_IVDARH  ファイル名が NULL, or サポート外のチャンネル数(現在の所チャンネル数は 1か 3のみをサポート)
*/
int  write_jpeg_file(const char* fname, JPEGImage jp, int qulty)
{
	FILE*  fp;
	struct jpeg_compress_struct jdat;
	struct jpeg_error_mgr	    jerr;


	if (fname==NULL) return ERROR_GRAPH_IVDARG;
	if (jp.col!=1 && jp.col!=3) return ERROR_GRAPH_IVDARG;
	if (jp.gp==NULL || jp.img==NULL) return ERROR_GRAPH_NODATA;

	if (qulty>100)	qulty = 100;
	else if (qulty<0) qulty = 0;
	

	fp = fopen(fname, "wb");
	if (fp==NULL) {
		return ERROR_GRAPH_OPFILE;
	}

	jdat.err = jpeg_std_error(&jerr);
	jpeg_create_compress(&jdat);

	// エラーハンドラ
/*	jdat.client_data = "Client Data";
	jerr.error_exit  = jpeg_error_exit;
	jerr.output_message = print_message;
*/
	jpeg_stdio_dest(&jdat, fp);

	jdat.image_width	  = jp.xs;
	jdat.image_height	  = jp.ys;
	jdat.input_components = jp.col;
	if (jp.col==1) jdat.in_color_space = JCS_GRAYSCALE;
	else		   jdat.in_color_space = JCS_RGB;

	jpeg_set_quality (&jdat, qulty, TRUE);
	jpeg_set_defaults(&jdat);

	jpeg_start_compress (&jdat, TRUE);
	jpeg_write_scanlines(&jdat, jp.img, jp.ys);
	jpeg_finish_compress(&jdat);

	jpeg_destroy_compress(&jdat);
	fclose(fp);	

	return 0;
}
Example #26
0
void init_jpeg(struct jpeg_compress_struct *jpeg, struct jpeg_error_mgr *jerr,
							 FILE *dest_file, int width, int height)
{
	jpeg->err = jpeg_std_error(jerr);
	jpeg_create_compress(jpeg);
	jpeg_stdio_dest(jpeg, dest_file);
	
	jpeg->image_width = width;
	jpeg->image_height = height;
  jpeg->input_components = 3;
  jpeg->in_color_space = JCS_RGB;

	jpeg_set_defaults(jpeg);
	jpeg_set_quality(jpeg, 100, TRUE);

}
Example #27
0
/**
	Write image to jpeg file.

	\param img image to write
*/
static void jpegWrite(unsigned char* img, char* jpegFilename)
{
    struct jpeg_compress_struct cinfo;
    struct jpeg_error_mgr jerr;

    JSAMPROW row_pointer[1];
    FILE *outfile = fopen( jpegFilename, "wb" );

    // try to open file for saving
    if (!outfile) {
        errno_exit("jpeg");
    }

    // create jpeg data
    cinfo.err = jpeg_std_error( &jerr );
    jpeg_create_compress(&cinfo);
    jpeg_stdio_dest(&cinfo, outfile);

    // set image parameters
    cinfo.image_width = width;
    cinfo.image_height = height;
    cinfo.input_components = 3;
    cinfo.in_color_space = JCS_YCbCr;

    // set jpeg compression parameters to default
    jpeg_set_defaults(&cinfo);
    // and then adjust quality setting
    jpeg_set_quality(&cinfo, jpegQuality, TRUE);

    // start compress
    jpeg_start_compress(&cinfo, TRUE);

    // feed data
    while (cinfo.next_scanline < cinfo.image_height) {
        row_pointer[0] = &img[cinfo.next_scanline * cinfo.image_width *  cinfo.input_components];
        jpeg_write_scanlines(&cinfo, row_pointer, 1);
    }

    // finish compression
    jpeg_finish_compress(&cinfo);

    // destroy jpeg data
    jpeg_destroy_compress(&cinfo);

    // close output file
    fclose(outfile);
}
Example #28
0
/**
 * Write jpeg data from memory to file
 * Return:
 *		 0 OK
 *		-1 error
 */
int write_jpeg(const char *path, const Bitmap_t *mem)
{
    VALIDATE_NOT_NULL3 (path, mem, mem->base);

    FILE *fp = fopen (path, "wb");
    if (NULL == fp) {
        return -1;
    }

    struct jpeg_compress_struct jcs;
    struct jpeg_error_mgr jerr;
    jcs.err = jpeg_std_error (&jerr);
    jpeg_create_compress (&jcs);
    jpeg_stdio_dest (&jcs, fp);
    jcs.image_width = mem->width;
    jcs.image_height = mem->height;

#ifdef _DEBUG_ 
    assert (mem->form >= GRAY && mem->form <= RGBA32);
    assert (mem->form != RGB16);
#endif

    int colorSpace = JCS_GRAYSCALE;
    int components = mem->form;
    if (components >= 3) {
        components = 3;
        colorSpace = JCS_RGB;
    }
    jcs.input_components = components;
    jcs.in_color_space = colorSpace;

    jpeg_set_defaults (&jcs);
#define QUALITY 80
    jpeg_set_quality (&jcs, QUALITY, TRUE);
    jpeg_start_compress (&jcs, TRUE);
    JSAMPROW row_pointer[1];
    int row_stride = mem->width * mem->form;

    while ( jcs.next_scanline < jcs.image_height ) {
        row_pointer[0] = (JSAMPROW)(mem->base + jcs.next_scanline * row_stride);
        jpeg_write_scanlines (&jcs, row_pointer, 1);
    }
    jpeg_finish_compress (&jcs);
    jpeg_destroy_compress (&jcs);
    fclose (fp);
    return 0;
}
Example #29
0
//==================================================================
static void write_JPEG_file(
			const char * filename,
			int quality,
			const JSAMPLE * image_buffer,
			int image_width,
			int image_height
			)
{
	struct jpeg_compress_struct cinfo;
	struct jpeg_error_mgr jerr;
	/* More stuff */
	FILE * outfile;		/* target file */
	int row_stride;		/* physical row width in image buffer */

	cinfo.err = jpeg_std_error(&jerr);
	jpeg_create_compress(&cinfo);

	if ( 0 != fopen_s( &outfile, filename, "wb" ) )
	{
		DASSTHROW( 0, ("can't open %s\n", filename) );
	}
	jpeg_stdio_dest(&cinfo, outfile);

	cinfo.image_width = image_width; 	/* image width and height, in pixels */
	cinfo.image_height = image_height;
	cinfo.input_components = 3;		/* # of color components per pixel */
	cinfo.in_color_space = JCS_RGB; 	/* colorspace of input image */

	jpeg_set_defaults(&cinfo);
	jpeg_set_quality(&cinfo, quality, TRUE /* limit to baseline-JPEG values */);

	jpeg_start_compress(&cinfo, TRUE);
	row_stride = image_width * 3;	/* JSAMPLEs per row in image_buffer */

	const JSAMPLE *row_pointer[1];	/* pointer to JSAMPLE row[s] */

	while (cinfo.next_scanline < cinfo.image_height)
	{
		row_pointer[0] = & image_buffer[cinfo.next_scanline * row_stride];
		jpeg_write_scanlines(&cinfo, (JSAMPARRAY)row_pointer, 1);
	}

	jpeg_finish_compress(&cinfo);
	fclose(outfile);

	jpeg_destroy_compress(&cinfo);
}
Example #30
0
bool WriteImageToJPG(const std::string &filename, const Image &image,
        int quality/* = 90*/)
{
    if (image.HasData() == false) {
        PrintWarning("Write JPG failed: image has no data.\n");
        return false;
    }
    if (image.bytes_per_channel_ != 1 ||
            (image.num_of_channels_ != 1 && image.num_of_channels_ != 3)) {
        PrintWarning("Write JPG failed: unsupported image data.\n");
        return false;
    }
    struct jpeg_compress_struct cinfo;
    struct jpeg_error_mgr jerr;
    FILE *file_out;
    JSAMPROW row_pointer[1];

    if ((file_out = fopen(filename.c_str(), "wb")) == NULL) {
        PrintWarning("Write JPG failed: unable to open file: %s\n", filename.c_str());
        return false;
    }

    cinfo.err = jpeg_std_error(&jerr);
    jpeg_create_compress(&cinfo);
    jpeg_stdio_dest(&cinfo, file_out);
    cinfo.image_width = image.width_;
    cinfo.image_height = image.height_;
    cinfo.input_components = image.num_of_channels_;
    cinfo.in_color_space =
            (cinfo.input_components == 1 ? JCS_GRAYSCALE : JCS_RGB);
    jpeg_set_defaults(&cinfo);
    jpeg_set_quality(&cinfo, quality, TRUE);
    jpeg_start_compress(&cinfo, TRUE);
    int row_stride = image.width_ * image.num_of_channels_;
    const uint8_t *pdata = image.data_.data();
    std::vector<uint8_t> buffer(row_stride);
    while (cinfo.next_scanline < cinfo.image_height) {
        memcpy(buffer.data(), pdata, row_stride);
        row_pointer[0] = buffer.data();
        jpeg_write_scanlines(&cinfo, row_pointer, 1);
        pdata += row_stride;
    }
    jpeg_finish_compress(&cinfo);
    fclose(file_out);
    jpeg_destroy_compress(&cinfo);
    return true;
}