+
+
+/*
+=================================================================
+
+ JPEG compression
+
+=================================================================
+*/
+
+#define JPEG_OUTPUT_BUF_SIZE 4096
+static void JPEG_InitDestination (j_compress_ptr cinfo)
+{
+ my_dest_ptr dest = (my_dest_ptr)cinfo->dest;
+ dest->buffer = (unsigned char*)cinfo->mem->alloc_small ((j_common_ptr) cinfo, JPOOL_IMAGE, JPEG_OUTPUT_BUF_SIZE * sizeof(unsigned char));
+ dest->pub.next_output_byte = dest->buffer;
+ dest->pub.free_in_buffer = JPEG_OUTPUT_BUF_SIZE;
+}
+
+static jboolean JPEG_EmptyOutputBuffer (j_compress_ptr cinfo)
+{
+ my_dest_ptr dest = (my_dest_ptr)cinfo->dest;
+
+ if (FS_Write (dest->outfile, dest->buffer, JPEG_OUTPUT_BUF_SIZE) != (size_t) JPEG_OUTPUT_BUF_SIZE)
+ {
+ error_in_jpeg = true;
+ return false;
+ }
+
+ dest->pub.next_output_byte = dest->buffer;
+ dest->pub.free_in_buffer = JPEG_OUTPUT_BUF_SIZE;
+ return true;
+}
+
+static void JPEG_TermDestination (j_compress_ptr cinfo)
+{
+ my_dest_ptr dest = (my_dest_ptr)cinfo->dest;
+ size_t datacount = JPEG_OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
+
+ // Write any data remaining in the buffer
+ if (datacount > 0)
+ if (FS_Write (dest->outfile, dest->buffer, datacount) != (fs_offset_t)datacount)
+ error_in_jpeg = true;
+}
+
+static void JPEG_MemDest (j_compress_ptr cinfo, qfile_t* outfile)
+{
+ my_dest_ptr dest;
+
+ // First time for this JPEG object?
+ if (cinfo->dest == NULL)
+ cinfo->dest = (struct jpeg_destination_mgr *)(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, sizeof(my_destination_mgr));
+
+ dest = (my_dest_ptr)cinfo->dest;
+ dest->pub.init_destination = JPEG_InitDestination;
+ dest->pub.empty_output_buffer = JPEG_EmptyOutputBuffer;
+ dest->pub.term_destination = JPEG_TermDestination;
+ dest->outfile = outfile;
+}
+
+
+/*
+====================
+JPEG_SaveImage_preflipped
+
+Save a preflipped JPEG image to a file
+====================
+*/
+qboolean JPEG_SaveImage_preflipped (const char *filename, int width, int height, unsigned char *data)
+{
+ struct jpeg_compress_struct cinfo;
+ struct jpeg_error_mgr jerr;
+ unsigned char *scanline;
+ unsigned int offset, linesize;
+ qfile_t* file;
+
+ // No DLL = no JPEGs
+ if (!jpeg_dll)
+ {
+ Con_Print("You need the libjpeg library to save JPEG images\n");
+ return false;
+ }
+
+ // Open the file
+ file = FS_Open (filename, "wb", true, false);
+ if (!file)
+ return false;
+
+ cinfo.err = qjpeg_std_error (&jerr);
+ cinfo.err->error_exit = JPEG_ErrorExit;
+ error_in_jpeg = false;
+
+ qjpeg_create_compress (&cinfo);
+ JPEG_MemDest (&cinfo, file);
+
+ // Set the parameters for compression
+ cinfo.image_width = width;
+ cinfo.image_height = height;
+ cinfo.in_color_space = JCS_RGB;
+ cinfo.input_components = 3;
+ qjpeg_set_defaults (&cinfo);
+ qjpeg_set_quality (&cinfo, (int)(scr_screenshot_jpeg_quality.value * 100), TRUE);
+ qjpeg_start_compress (&cinfo, true);
+
+ // Compress each scanline
+ linesize = cinfo.image_width * 3;
+ offset = linesize * (cinfo.image_height - 1);
+ while (cinfo.next_scanline < cinfo.image_height)
+ {
+ scanline = &data[offset - cinfo.next_scanline * linesize];
+
+ qjpeg_write_scanlines (&cinfo, &scanline, 1);
+ if (error_in_jpeg)
+ break;
+ }
+
+ qjpeg_finish_compress (&cinfo);
+ qjpeg_destroy_compress (&cinfo);
+
+ FS_Close (file);
+ return true;
+}