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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
/*
* jmemdest.cpp
*
* This file contains a compression data destination which takes a
* memory region as destination.
* The only thing these routines really do is tell the library where the
* data is on construction (jpeg_mem_dest()). Everything else is there
* for error checking purposes.
* Adapted from jdatasrc.c 9/96 by Ulrich von Zadow.
* Adapted from jmemsrc.c 3.6.99 by MS
*/
/* this is not a core library module, so it doesn't define JPEG_INTERNALS */
#include "pldatasink.h"
#include <stdio.h>
#include "jpeglib.h"
#include "jerror.h"
#include "jmemdest.h"
#include <stdio.h>
#define JPEG_WORK_BUFFER_SIZE 8192 // 8KB Work Buffer
typedef struct
{
struct jpeg_destination_mgr pub; // public fields
PLDataSink * pDataSink ; // Data Sink
JOCTET * buffer;
}
datasink_dest_mgr;
/*
* Initialize destination
* before any data is actually written.
*/
METHODDEF(void)
jpeg_mem_dest_init (j_compress_ptr cinfo)
{
/* Set up data pointer */
datasink_dest_mgr * dest = (datasink_dest_mgr * ) cinfo->dest;
/* Allocate the output buffer --- it will be released when done with image */
dest->buffer = (JOCTET *)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
JPEG_WORK_BUFFER_SIZE);
dest->pub.next_output_byte = dest->buffer;
dest->pub.free_in_buffer = JPEG_WORK_BUFFER_SIZE;
}
/*
* Flush the output buffer --- called whenever buffer is full.
*
* If this procedure gets called, we have a buffer overrun condition -
* We now send the data to the DataSink
*/
METHODDEF(boolean)
jpeg_mem_dest_empty_output_buffer (j_compress_ptr cinfo)
{
datasink_dest_mgr * dest = (datasink_dest_mgr *) cinfo->dest;
if (dest->pDataSink->WriteNBytes(JPEG_WORK_BUFFER_SIZE, dest->buffer) !=
(size_t) JPEG_WORK_BUFFER_SIZE)
ERREXIT(cinfo, JERR_FILE_WRITE);
dest->pub.next_output_byte = dest->buffer;
dest->pub.free_in_buffer = JPEG_WORK_BUFFER_SIZE;
return true;
}
/*
* Terminate source --- called by jpeg_finish_compress
* after all data has been written.
*
* NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
* application must deal with any cleanup that should happen even
* for error exit.
*/
METHODDEF(void)
jpeg_mem_dest_term (j_compress_ptr cinfo)
{
datasink_dest_mgr * dest = (datasink_dest_mgr *) cinfo->dest;
size_t datacount = JPEG_WORK_BUFFER_SIZE - dest->pub.free_in_buffer;
/* Write any data remaining in the buffer */
if (datacount > 0)
{
if (dest->pDataSink->WriteNBytes(datacount, dest->buffer) != datacount)
ERREXIT(cinfo, JERR_FILE_WRITE);
}
}
/*
* Prepare for ouput. This routine tells the jpeg library where to find
* the output buffer & sets up the function pointers the library needs.
*
*/
GLOBAL(void)
jpeg_mem_dest(
j_compress_ptr cinfo,
JOCTET * pData,
int FileSize,
PLDataSink * pDataSink
)
{
datasink_dest_mgr * dest;
if (cinfo->dest == NULL)
{ /* first time for this JPEG object? */
cinfo->dest = (struct jpeg_destination_mgr *)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
sizeof(datasink_dest_mgr));
}
dest = (datasink_dest_mgr *) cinfo->dest;
dest->pub.init_destination = jpeg_mem_dest_init;
dest->pub.empty_output_buffer = jpeg_mem_dest_empty_output_buffer;
dest->pub.term_destination = jpeg_mem_dest_term;
dest->pDataSink = pDataSink;
}
|