Doxygen Source Code Documentation
        
Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Search   
jmemmac.c
Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00035 
00036 
00037 
00038 
00039 
00040 
00041 
00042 
00043 
00044 
00045 
00046 
00047 
00048 
00049 
00050 
00051 
00052 #define JPEG_INTERNALS
00053 #include "jinclude.h"
00054 #include "jpeglib.h"
00055 #include "jmemsys.h"    
00056 
00057 #ifndef USE_MAC_MEMMGR  
00058   You forgot to define USE_MAC_MEMMGR in jconfig.h. 
00059 #endif
00060 
00061 #include <Memory.h>     
00062 #include <Files.h>      
00063 #include <Folders.h>    
00064 #include <Script.h>     
00065 #include <Gestalt.h>    
00066 
00067 #ifndef TEMP_FILE_NAME          
00068 #define TEMP_FILE_NAME  "JPG%03d.TMP"
00069 #endif
00070 
00071 static int next_file_num;       
00072 
00073 
00074 
00075 
00076 
00077 
00078 
00079 
00080 
00081 GLOBAL(void *)
00082 jpeg_get_small (j_common_ptr cinfo, size_t sizeofobject)
00083 {
00084   return (void *) NewPtr(sizeofobject);
00085 }
00086 
00087 GLOBAL(void)
00088 jpeg_free_small (j_common_ptr cinfo, void * object, size_t sizeofobject)
00089 {
00090   DisposePtr((Ptr) object);
00091 }
00092 
00093 
00094 
00095 
00096 
00097 
00098 
00099 
00100 
00101 GLOBAL(void FAR *)
00102 jpeg_get_large (j_common_ptr cinfo, size_t sizeofobject)
00103 {
00104   return (void FAR *) NewPtr(sizeofobject);
00105 }
00106 
00107 GLOBAL(void)
00108 jpeg_free_large (j_common_ptr cinfo, void FAR * object, size_t sizeofobject)
00109 {
00110   DisposePtr((Ptr) object);
00111 }
00112 
00113 
00114 
00115 
00116 
00117 
00118 GLOBAL(long)
00119 jpeg_mem_available (j_common_ptr cinfo, long min_bytes_needed,
00120                     long max_bytes_needed, long already_allocated)
00121 {
00122   long limit = cinfo->mem->max_memory_to_use - already_allocated;
00123   long slop, mem;
00124 
00125   
00126   if (max_bytes_needed > limit && limit > 0)
00127     max_bytes_needed = limit;
00128   
00129 
00130 
00131 
00132 
00133 
00134   slop = max_bytes_needed / 16 + 32768L;
00135   mem = CompactMem(max_bytes_needed + slop) - slop;
00136   if (mem < 0)
00137     mem = 0;                    
00138   
00139   if (mem > limit && limit > 0)
00140     mem = limit;
00141   return mem;
00142 }
00143 
00144 
00145 
00146 
00147 
00148 
00149 
00150 
00151 
00152 
00153 METHODDEF(void)
00154 read_backing_store (j_common_ptr cinfo, backing_store_ptr info,
00155                     void FAR * buffer_address,
00156                     long file_offset, long byte_count)
00157 {
00158   long bytes = byte_count;
00159   long retVal;
00160 
00161   if ( SetFPos ( info->temp_file, fsFromStart, file_offset ) != noErr )
00162     ERREXIT(cinfo, JERR_TFILE_SEEK);
00163 
00164   retVal = FSRead ( info->temp_file, &bytes,
00165                     (unsigned char *) buffer_address );
00166   if ( retVal != noErr || bytes != byte_count )
00167     ERREXIT(cinfo, JERR_TFILE_READ);
00168 }
00169 
00170 
00171 METHODDEF(void)
00172 write_backing_store (j_common_ptr cinfo, backing_store_ptr info,
00173                      void FAR * buffer_address,
00174                      long file_offset, long byte_count)
00175 {
00176   long bytes = byte_count;
00177   long retVal;
00178 
00179   if ( SetFPos ( info->temp_file, fsFromStart, file_offset ) != noErr )
00180     ERREXIT(cinfo, JERR_TFILE_SEEK);
00181 
00182   retVal = FSWrite ( info->temp_file, &bytes,
00183                      (unsigned char *) buffer_address );
00184   if ( retVal != noErr || bytes != byte_count )
00185     ERREXIT(cinfo, JERR_TFILE_WRITE);
00186 }
00187 
00188 
00189 METHODDEF(void)
00190 close_backing_store (j_common_ptr cinfo, backing_store_ptr info)
00191 {
00192   FSClose ( info->temp_file );
00193   FSpDelete ( &(info->tempSpec) );
00194 }
00195 
00196 
00197 
00198 
00199 
00200 
00201 
00202 
00203 
00204 GLOBAL(void)
00205 jpeg_open_backing_store (j_common_ptr cinfo, backing_store_ptr info,
00206                          long total_bytes_needed)
00207 {
00208   short         tmpRef, vRefNum;
00209   long          dirID;
00210   FInfo         finderInfo;
00211   FSSpec        theSpec;
00212   Str255        fName;
00213   OSErr         osErr;
00214   long          gestaltResponse = 0;
00215 
00216   
00217   osErr = Gestalt( gestaltFSAttr, &gestaltResponse );
00218   if ( ( osErr != noErr )
00219        || !( gestaltResponse & (1<<gestaltHasFSSpecCalls) ) )
00220     ERREXITS(cinfo, JERR_TFILE_CREATE, "- System 7.0 or later required");
00221   
00222 
00223   
00224   osErr = Gestalt( gestaltFindFolderAttr, &gestaltResponse );
00225   if ( ( osErr != noErr )
00226        || !( gestaltResponse & (1<<gestaltFindFolderPresent) ) )
00227     ERREXITS(cinfo, JERR_TFILE_CREATE, "- System 7.0 or later required.");
00228   
00229 
00230   osErr = FindFolder ( kOnSystemDisk, kTemporaryFolderType, kCreateFolder,
00231                        &vRefNum, &dirID );
00232   if ( osErr != noErr )
00233     ERREXITS(cinfo, JERR_TFILE_CREATE, "- temporary items folder unavailable");
00234   
00235 
00236   
00237   for (;;) {
00238     next_file_num++;            
00239 
00240     sprintf(info->temp_name, TEMP_FILE_NAME, next_file_num);
00241     strcpy ( (Ptr)fName+1, info->temp_name );
00242     *fName = strlen (info->temp_name);
00243     osErr = FSMakeFSSpec ( vRefNum, dirID, fName, &theSpec );
00244 
00245     if ( (osErr = FSpGetFInfo ( &theSpec, &finderInfo ) ) != noErr )
00246       break;
00247   }
00248 
00249   osErr = FSpCreate ( &theSpec, '????', '????', smSystemScript );
00250   if ( osErr != noErr )
00251     ERREXITS(cinfo, JERR_TFILE_CREATE, info->temp_name);
00252 
00253   osErr = FSpOpenDF ( &theSpec, fsRdWrPerm, &(info->temp_file) );
00254   if ( osErr != noErr )
00255     ERREXITS(cinfo, JERR_TFILE_CREATE, info->temp_name);
00256 
00257   info->tempSpec = theSpec;
00258 
00259   info->read_backing_store = read_backing_store;
00260   info->write_backing_store = write_backing_store;
00261   info->close_backing_store = close_backing_store;
00262   TRACEMSS(cinfo, 1, JTRC_TFILE_OPEN, info->temp_name);
00263 }
00264 
00265 
00266 
00267 
00268 
00269 
00270 
00271 GLOBAL(long)
00272 jpeg_mem_init (j_common_ptr cinfo)
00273 {
00274   next_file_num = 0;
00275 
00276   
00277 
00278 
00279 
00280 
00281 
00282   return FreeMem();
00283 }
00284 
00285 GLOBAL(void)
00286 jpeg_mem_term (j_common_ptr cinfo)
00287 {
00288   
00289 }