You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

298 lines
9.1 KiB

  1. --- a/drv_SamsungSPF.c
  2. +++ b/drv_SamsungSPF.c
  3. @@ -43,6 +43,7 @@
  4. #include <usb.h>
  5. #include <jpeglib.h>
  6. +#include <jerror.h>
  7. #include "debug.h"
  8. #include "cfg.h"
  9. @@ -117,6 +118,287 @@ static struct {
  10. /*** hardware dependant functions ***/
  11. /****************************************/
  12. +/* libjpeg8 and later come with their own (API compatible) memory source
  13. + and dest */
  14. +#if JPEG_LIB_VERSION < 80 && !defined(MEM_SRCDST_SUPPORTED)
  15. +
  16. +/* Expanded data source object for memory input */
  17. +
  18. +typedef struct {
  19. + struct jpeg_source_mgr pub; /* public fields */
  20. +
  21. + JOCTET eoi_buffer[2]; /* a place to put a dummy EOI */
  22. +} my_source_mgr;
  23. +
  24. +typedef my_source_mgr * my_src_ptr;
  25. +
  26. +
  27. +/*
  28. +* Initialize source --- called by jpeg_read_header
  29. +* before any data is actually read.
  30. +*/
  31. +
  32. +METHODDEF(void)
  33. +init_source (j_decompress_ptr cinfo)
  34. +{
  35. + /* No work, since jpeg_mem_src set up the buffer pointer and count.
  36. + * Indeed, if we want to read multiple JPEG images from one buffer,
  37. + * this *must* not do anything to the pointer.
  38. + */
  39. +}
  40. +
  41. +
  42. +/*
  43. +* Fill the input buffer --- called whenever buffer is emptied.
  44. +*
  45. +* In this application, this routine should never be called; if it is called,
  46. +* the decompressor has overrun the end of the input buffer, implying we
  47. +* supplied an incomplete or corrupt JPEG datastream. A simple error exit
  48. +* might be the most appropriate response.
  49. +*
  50. +* But what we choose to do in this code is to supply dummy EOI markers
  51. +* in order to force the decompressor to finish processing and supply
  52. +* some sort of output image, no matter how corrupted.
  53. +*/
  54. +
  55. +METHODDEF(boolean)
  56. +fill_input_buffer (j_decompress_ptr cinfo)
  57. +{
  58. + my_src_ptr src = (my_src_ptr) cinfo->src;
  59. +
  60. + WARNMS(cinfo, JWRN_JPEG_EOF);
  61. +
  62. + /* Create a fake EOI marker */
  63. + src->eoi_buffer[0] = (JOCTET) 0xFF;
  64. + src->eoi_buffer[1] = (JOCTET) JPEG_EOI;
  65. + src->pub.next_input_byte = src->eoi_buffer;
  66. + src->pub.bytes_in_buffer = 2;
  67. +
  68. + return TRUE;
  69. +}
  70. +
  71. +
  72. +/*
  73. +* Skip data --- used to skip over a potentially large amount of
  74. +* uninteresting data (such as an APPn marker).
  75. +*
  76. +* If we overrun the end of the buffer, we let fill_input_buffer deal with
  77. +* it. An extremely large skip could cause some time-wasting here, but
  78. +* it really isn't supposed to happen ... and the decompressor will never
  79. +* skip more than 64K anyway.
  80. +*/
  81. +
  82. +METHODDEF(void)
  83. +skip_input_data (j_decompress_ptr cinfo, long num_bytes)
  84. +{
  85. + my_src_ptr src = (my_src_ptr) cinfo->src;
  86. +
  87. + if (num_bytes > 0) {
  88. + while (num_bytes > (long) src->pub.bytes_in_buffer) {
  89. + num_bytes -= (long) src->pub.bytes_in_buffer;
  90. + (void) fill_input_buffer(cinfo);
  91. + /* note we assume that fill_input_buffer will never
  92. + * return FALSE, so suspension need not be handled.
  93. + */
  94. + }
  95. + src->pub.next_input_byte += (size_t) num_bytes;
  96. + src->pub.bytes_in_buffer -= (size_t) num_bytes;
  97. + }
  98. +}
  99. +
  100. +
  101. +/*
  102. +* An additional method that can be provided by data source modules is the
  103. +* resync_to_restart method for error recovery in the presence of RST markers.
  104. +* For the moment, this source module just uses the default resync method
  105. +* provided by the JPEG library. That method assumes that no backtracking
  106. +* is possible.
  107. +*/
  108. +
  109. +
  110. +/*
  111. +* Terminate source --- called by jpeg_finish_decompress
  112. +* after all data has been read. Often a no-op.
  113. +*
  114. +* NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
  115. +* application must deal with any cleanup that should happen even
  116. +* for error exit.
  117. +*/
  118. +
  119. +METHODDEF(void)
  120. +term_source (j_decompress_ptr cinfo)
  121. +{
  122. + /* no work necessary here */
  123. +}
  124. +
  125. +
  126. +/*
  127. +* Prepare for input from a memory buffer.
  128. +*/
  129. +
  130. +GLOBAL(void)
  131. +jpeg_mem_src (j_decompress_ptr cinfo, unsigned char * buffer,
  132. + unsigned long bufsize)
  133. +{
  134. + my_src_ptr src;
  135. +
  136. + /* The source object is made permanent so that a series of JPEG images
  137. + * can be read from a single buffer by calling jpeg_mem_src
  138. + * only before the first one.
  139. + * This makes it unsafe to use this manager and a different source
  140. + * manager serially with the same JPEG object. Caveat programmer.
  141. + */
  142. + if (cinfo->src == NULL) { /* first time for this JPEG object? */
  143. + cinfo->src = (struct jpeg_source_mgr *)
  144. + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo,
  145. + JPOOL_PERMANENT,
  146. + sizeof(my_source_mgr));
  147. + }
  148. +
  149. + src = (my_src_ptr) cinfo->src;
  150. + src->pub.init_source = init_source;
  151. + src->pub.fill_input_buffer = fill_input_buffer;
  152. + src->pub.skip_input_data = skip_input_data;
  153. + src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
  154. + src->pub.term_source = term_source;
  155. +
  156. + src->pub.next_input_byte = buffer;
  157. + src->pub.bytes_in_buffer = bufsize;
  158. +}
  159. +
  160. +
  161. +
  162. +/* Memory destination source modelled after Thomas G. Lane's memory source
  163. + support and jdatadst.c
  164. +
  165. + Copyright (C) 2010, Hans de Goede
  166. +
  167. + This code may be used under the same conditions as Thomas G. Lane's memory
  168. + source (see the copyright header at the top of this file).
  169. + */
  170. +
  171. +typedef struct {
  172. + struct jpeg_destination_mgr pub; /* public fields */
  173. +
  174. + JOCTET **buffer; /* start of buffer */
  175. + unsigned long buf_size, *outsize;
  176. +} my_destination_mgr;
  177. +
  178. +typedef my_destination_mgr * my_dest_ptr;
  179. +
  180. +#define OUTPUT_BUF_SIZE 32768 /* choose an efficiently fwrite'able size */
  181. +
  182. +
  183. +/*
  184. + * Initialize destination --- called by jpeg_start_compress
  185. + * before any data is actually written.
  186. + */
  187. +
  188. +METHODDEF(void)
  189. +init_destination (j_compress_ptr cinfo)
  190. +{
  191. + /* No work, since jpeg_mem_dest set up the buffer pointer and count.
  192. + * Indeed, if we want to write multiple JPEG images to one buffer,
  193. + * this *must* not do anything to the pointer.
  194. + */
  195. +}
  196. +
  197. +/*
  198. + * Empty the output buffer --- called whenever buffer fills up.
  199. + *
  200. + * In typical applications, this should write the entire output buffer
  201. + * (ignoring the current state of next_output_byte & free_in_buffer),
  202. + * reset the pointer & count to the start of the buffer, and return TRUE
  203. + * indicating that the buffer has been dumped.
  204. + *
  205. + * In applications that need to be able to suspend compression due to output
  206. + * overrun, a FALSE return indicates that the buffer cannot be emptied now.
  207. + * In this situation, the compressor will return to its caller (possibly with
  208. + * an indication that it has not accepted all the supplied scanlines). The
  209. + * application should resume compression after it has made more room in the
  210. + * output buffer. Note that there are substantial restrictions on the use of
  211. + * suspension --- see the documentation.
  212. + *
  213. + * When suspending, the compressor will back up to a convenient restart point
  214. + * (typically the start of the current MCU). next_output_byte & free_in_buffer
  215. + * indicate where the restart point will be if the current call returns FALSE.
  216. + * Data beyond this point will be regenerated after resumption, so do not
  217. + * write it out when emptying the buffer externally.
  218. + */
  219. +
  220. +METHODDEF(boolean)
  221. +empty_output_buffer (j_compress_ptr cinfo)
  222. +{
  223. + my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
  224. +
  225. + *dest->buffer = realloc (*dest->buffer, dest->buf_size + OUTPUT_BUF_SIZE);
  226. + if (!*dest->buffer)
  227. + ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 0);
  228. +
  229. + dest->pub.next_output_byte = *dest->buffer + dest->buf_size;
  230. + dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
  231. + dest->buf_size += OUTPUT_BUF_SIZE;
  232. +
  233. + return TRUE;
  234. +}
  235. +
  236. +/*
  237. + * Terminate destination --- called by jpeg_finish_compress
  238. + * after all data has been written. Usually needs to flush buffer.
  239. + *
  240. + * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
  241. + * application must deal with any cleanup that should happen even
  242. + * for error exit.
  243. + */
  244. +
  245. +METHODDEF(void)
  246. +term_destination (j_compress_ptr cinfo)
  247. +{
  248. + my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
  249. +
  250. + *dest->outsize = dest->buf_size - dest->pub.free_in_buffer;
  251. +}
  252. +
  253. +GLOBAL(void)
  254. +jpeg_mem_dest (j_compress_ptr cinfo, unsigned char ** outbuffer,
  255. + unsigned long * outsize)
  256. +{
  257. + my_dest_ptr dest;
  258. +
  259. + /* The destination object is made permanent so that multiple JPEG images
  260. + * can be written to the same file without re-executing jpeg_stdio_dest.
  261. + * This makes it dangerous to use this manager and a different destination
  262. + * manager serially with the same JPEG object, because their private object
  263. + * sizes may be different. Caveat programmer.
  264. + */
  265. + if (cinfo->dest == NULL) { /* first time for this JPEG object? */
  266. + cinfo->dest = (struct jpeg_destination_mgr *)
  267. + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo,
  268. + JPOOL_PERMANENT,
  269. + sizeof(my_destination_mgr));
  270. + }
  271. +
  272. + dest = (my_dest_ptr) cinfo->dest;
  273. + dest->pub.init_destination = init_destination;
  274. + dest->pub.empty_output_buffer = empty_output_buffer;
  275. + dest->pub.term_destination = term_destination;
  276. + dest->buffer = outbuffer;
  277. + dest->buf_size = *outsize;
  278. + dest->outsize = outsize;
  279. +
  280. + if (*dest->buffer == NULL || dest->buf_size == 0) {
  281. + /* Allocate initial buffer */
  282. + *dest->buffer = malloc(OUTPUT_BUF_SIZE);
  283. + if (*dest->buffer == NULL)
  284. + ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 10);
  285. + dest->buf_size = OUTPUT_BUF_SIZE;
  286. + }
  287. +
  288. + dest->pub.next_output_byte = *dest->buffer;
  289. + dest->pub.free_in_buffer = dest->buf_size;
  290. +}
  291. +
  292. +#endif /* JPEG_LIB_VERSION < 80 && !defined(MEM_SRCDST_SUPPORTED) */
  293. /* please note that in-memory compression doesn't work satisfactory */
  294. int convert2JPG()