jdatadst.pas 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. Unit JDataDst;
  2. { This file contains compression data destination routines for the case of
  3. emitting JPEG data to a file (or any stdio stream). While these routines
  4. are sufficient for most applications, some will want to use a different
  5. destination manager.
  6. IMPORTANT: we assume that fwrite() will correctly transcribe an array of
  7. JOCTETs into 8-bit-wide elements on external storage. If char is wider
  8. than 8 bits on your machine, you may need to do some tweaking. }
  9. { Original : jdatadst.c ; Copyright (C) 1994-1996, Thomas G. Lane. }
  10. interface
  11. {$I jconfig.inc}
  12. { this is not a core library module, so it doesn't define JPEG_INTERNALS }
  13. uses
  14. jmorecfg,
  15. jpeglib,
  16. jinclude,
  17. jdeferr,
  18. jerror;
  19. { Prepare for output to a stdio stream.
  20. The caller must have already opened the stream, and is responsible
  21. for closing it after finishing compression. }
  22. {GLOBAL}
  23. procedure jpeg_stdio_dest (cinfo : j_compress_ptr; outfile : FILEptr);
  24. implementation
  25. { Expanded data destination object for stdio output }
  26. type
  27. my_dest_ptr = ^my_destination_mgr;
  28. my_destination_mgr = record
  29. pub : jpeg_destination_mgr; { public fields }
  30. outfile : FILEPTR; { target stream }
  31. buffer : JOCTET_FIELD_PTR; { start of buffer }
  32. end; {my_destination_mgr;}
  33. const
  34. OUTPUT_BUF_SIZE = 4096; { choose an efficiently fwrite'able size }
  35. { Initialize destination --- called by jpeg_start_compress
  36. before any data is actually written. }
  37. {METHODDEF}
  38. procedure init_destination (cinfo : j_compress_ptr); far;
  39. var
  40. dest : my_dest_ptr;
  41. begin
  42. dest := my_dest_ptr(cinfo^.dest);
  43. { Allocate the output buffer --- it will be released when done with image }
  44. dest^.buffer := JOCTET_FIELD_PTR(
  45. cinfo^.mem^.alloc_small (j_common_ptr(cinfo), JPOOL_IMAGE,
  46. OUTPUT_BUF_SIZE * SIZEOF(JOCTET)) );
  47. dest^.pub.next_output_byte := JOCTETptr(dest^.buffer);
  48. dest^.pub.free_in_buffer := OUTPUT_BUF_SIZE;
  49. end;
  50. { Empty the output buffer --- called whenever buffer fills up.
  51. In typical applications, this should write the entire output buffer
  52. (ignoring the current state of next_output_byte & free_in_buffer),
  53. reset the pointer & count to the start of the buffer, and return TRUE
  54. indicating that the buffer has been dumped.
  55. In applications that need to be able to suspend compression due to output
  56. overrun, a FALSE return indicates that the buffer cannot be emptied now.
  57. In this situation, the compressor will return to its caller (possibly with
  58. an indication that it has not accepted all the supplied scanlines). The
  59. application should resume compression after it has made more room in the
  60. output buffer. Note that there are substantial restrictions on the use of
  61. suspension --- see the documentation.
  62. When suspending, the compressor will back up to a convenient restart point
  63. (typically the start of the current MCU). next_output_byte & free_in_buffer
  64. indicate where the restart point will be if the current call returns FALSE.
  65. Data beyond this point will be regenerated after resumption, so do not
  66. write it out when emptying the buffer externally. }
  67. {METHODDEF}
  68. function empty_output_buffer (cinfo : j_compress_ptr) : boolean; far;
  69. var
  70. dest : my_dest_ptr;
  71. begin
  72. dest := my_dest_ptr(cinfo^.dest);
  73. if (JFWRITE(dest^.outfile, dest^.buffer, OUTPUT_BUF_SIZE) <>
  74. size_t(OUTPUT_BUF_SIZE)) then
  75. ERREXIT(j_common_ptr(cinfo), JERR_FILE_WRITE);
  76. dest^.pub.next_output_byte := JOCTETptr(dest^.buffer);
  77. dest^.pub.free_in_buffer := OUTPUT_BUF_SIZE;
  78. empty_output_buffer := TRUE;
  79. end;
  80. { Terminate destination --- called by jpeg_finish_compress
  81. after all data has been written. Usually needs to flush buffer.
  82. NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
  83. application must deal with any cleanup that should happen even
  84. for error exit. }
  85. {METHODDEF}
  86. procedure term_destination (cinfo : j_compress_ptr); far;
  87. var
  88. dest : my_dest_ptr;
  89. datacount : size_t;
  90. begin
  91. dest := my_dest_ptr (cinfo^.dest);
  92. datacount := OUTPUT_BUF_SIZE - dest^.pub.free_in_buffer;
  93. { Write any data remaining in the buffer }
  94. if (datacount > 0) then
  95. begin
  96. if (JFWRITE(dest^.outfile, dest^.buffer, datacount) <> datacount) then
  97. ERREXIT(j_common_ptr(cinfo), JERR_FILE_WRITE);
  98. end;
  99. {fflush(dest^.outfile^);}
  100. { Make sure we wrote the output file OK }
  101. {if (ferror(dest^.outfile))
  102. ERREXIT(cinfo, JERR_FILE_WRITE);}
  103. if IOresult <> 0 then
  104. ERREXIT(j_common_ptr(cinfo), JERR_FILE_WRITE);
  105. end;
  106. { Prepare for output to a stdio stream.
  107. The caller must have already opened the stream, and is responsible
  108. for closing it after finishing compression. }
  109. {GLOBAL}
  110. procedure jpeg_stdio_dest (cinfo : j_compress_ptr; outfile : FILEptr);
  111. var
  112. dest : my_dest_ptr;
  113. begin
  114. { The destination object is made permanent so that multiple JPEG images
  115. can be written to the same file without re-executing jpeg_stdio_dest.
  116. This makes it dangerous to use this manager and a different destination
  117. manager serially with the same JPEG object, because their private object
  118. sizes may be different. Caveat programmer. }
  119. if (cinfo^.dest = NIL) then
  120. begin { first time for this JPEG object? }
  121. cinfo^.dest := jpeg_destination_mgr_ptr(
  122. cinfo^.mem^.alloc_small (j_common_ptr(cinfo), JPOOL_PERMANENT,
  123. SIZEOF(my_destination_mgr)) );
  124. end;
  125. dest := my_dest_ptr (cinfo^.dest);
  126. dest^.pub.init_destination := init_destination;
  127. dest^.pub.empty_output_buffer := empty_output_buffer;
  128. dest^.pub.term_destination := term_destination;
  129. dest^.outfile := outfile;
  130. end;
  131. end.