File: tim.cpp

package info (click to toggle)
xmoto 0.5.11+dfsg-8
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 80,908 kB
  • sloc: cpp: 96,757; ansic: 22,196; sh: 4,940; makefile: 1,073; yacc: 289; sed: 16
file content (506 lines) | stat: -rw-r--r-- 14,610 bytes parent folder | download | duplicates (2)
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/*=============================================================================
  XMOTO

  This file is part of XMOTO.

  XMOTO is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  XMOTO is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with XMOTO; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  =============================================================================*/

/* tim.cpp
 * 
 * TinyImage API main.
 */
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <ctype.h>
#include <stdlib.h>
#include "tim.h"

/*==============================================================================
  BAD BAD BAD
  ==============================================================================*/
#if !defined(WIN32)
static int stricmp(const char *pc1,const char *pc2) {
  int s1 = strlen(pc1);
  int s2 = strlen(pc2);
  if(s1 != s2) return 1;
  for(int i=0;i<s1;i++)
    if(tolower(pc1[i]) != tolower(pc2[i])) return 1;
  return 0;
}
#endif

/*==============================================================================
  Memory system
  ==============================================================================*/

void *tim_alloc(tim_session_t *pSession,int nSize) {
  /* Allocate block of memory */
  return pSession->Memory.tim_callback_alloc(nSize);
}

void *tim_realloc(tim_session_t *pSession,void *pvBlock,int nSize) {
  /* Re-allocate block of memory */
  return pSession->Memory.tim_callback_realloc(pvBlock,nSize);
}

void tim_free(tim_session_t *pSession,void *pvBlock) {
  /* Free block of memory */
  pSession->Memory.tim_callback_free(pvBlock);
}

char *tim_strdup(tim_session_t *pSession,const char *pcString) {
  char *pcNewString;
  int nLen;
  nLen=(int)strlen(pcString)+1;
  pcNewString=(char *)tim_alloc(pSession,nLen);
  if(pcNewString==NULL) return NULL;
  memcpy(pcNewString,pcString,nLen);
  return pcNewString;
}

/*==============================================================================
  File I/O system
  ==============================================================================*/

void *tim_open(tim_session_t *pSession,char *pcWhere,tim_io_mode_t IOMode) {
  /* Open file */
  return pSession->IO.tim_callback_open(pcWhere,IOMode);
}

void tim_close(tim_session_t *pSession,void *pvHandle) {
  /* Close file */
  pSession->IO.tim_callback_close(pvHandle);
}

int tim_seek(tim_session_t *pSession,void *pvHandle,int nOffset,tim_seek_mode_t SeekMode) {
  /* Seek in file and return new offset */
  return pSession->IO.tim_callback_seek(pvHandle,nOffset,SeekMode);
}

int tim_read(tim_session_t *pSession,void *pvHandle,void *pvBuf,int nSize) {
  /* Read from file */
  return pSession->IO.tim_callback_read(pvHandle,pvBuf,nSize);
}

int tim_write(tim_session_t *pSession,void *pvHandle,void *pvBuf,int nSize) {
  /* Write to file */
  return pSession->IO.tim_callback_write(pvHandle,pvBuf,nSize);
}

int tim_eof(tim_session_t *pSession,void *pvHandle) {
  /* Return EOF flag */
  return pSession->IO.tim_callback_eof(pvHandle);
}

/*==============================================================================
  File format management
  ==============================================================================*/

int tim_add_extension_to_file_format(tim_file_format_t *p,const char *pcExtension) {
  /* Room for more extensions? */
  if(p->nNumExtensions>=TIM_MAX_EXTENSIONS_PER_FILE_FORMAT)
    return TIM_RV_ERR_INTERNAL_LIMIT;
	
  /* Set extension */
  p->pcExtensions[p->nNumExtensions++]=tim_strdup(p->pSession,pcExtension);	
  if(p->pcExtensions[p->nNumExtensions-1]==NULL) {
    p->nNumExtensions--;
    return TIM_RV_ERR_MEMORY;
  }
	
  /* Return ok */
  return TIM_RV_OK;
}

tim_file_format_t *tim_create_file_format(tim_session_t *pSession,const char *pcExtension,
					  tim_callback_save_t SaveCallback,
					  tim_callback_load_t LoadCallback) {					
  tim_file_format_t *p;
	
  /* Room for more formats? */																					
  if(pSession->nNumFileFormats>=TIM_MAX_FILE_FORMATS)
    return NULL;
		
  /* New file format */
  p=&pSession->FileFormats[pSession->nNumFileFormats++];
	
  /* Set contents */
  p->tim_callback_save=SaveCallback;
  p->tim_callback_load=LoadCallback;
  p->pSession=pSession;
	
  /* Set standard extension */
  if(tim_add_extension_to_file_format(p,pcExtension)<0) {
    pSession->nNumFileFormats--;
    return NULL;
  }
	
  /* Return */
  return p;
}

tim_file_format_t *tim_find_file_format(tim_session_t *pSession,const char *pcExtension) {
  int i,j;

  /* Do we have a file format with this extension? */
  for(i=0;i<pSession->nNumFileFormats;i++) {
    for(j=0;j<pSession->FileFormats[i].nNumExtensions;j++) {
      /* This? */
      if(!stricmp(pSession->FileFormats[i].pcExtensions[j],pcExtension)) {
	/* Yep */
	return &pSession->FileFormats[i];
      }
    }
  }
	
  /* No! */
  return NULL;
}

tim_file_format_t *tim_determine_file_format(tim_session_t *pSession,char *pcFileName) {
  int i;

  /* Extract extension */
  for(i=strlen(pcFileName)-1;i>=0;i--) {
    if(pcFileName[i]=='.') return tim_find_file_format(pSession,&pcFileName[i]);
  }
	
  /* Nothing */
  return NULL;
}

tim_file_format_hint_t *tim_find_file_format_hint(tim_file_format_t *pFormat,const char *pcTag) {
  tim_file_format_hint_t *p;

  /* Is this hint found? */
  for(p=pFormat->pFirstHint;p!=NULL;p=p->pNext)
    if(!stricmp(p->cTag,pcTag)) return p;
	
  /* Nope */
  return NULL;
}

tim_file_format_hint_t *tim_add_file_format_hint(tim_session_t *pSession,
                                                 const char *pcExt,const char *pcTag,char *pcValue) {
  tim_file_format_t *pFormat;
  tim_file_format_hint_t *p;
	
  /* Find format */
  pFormat=tim_find_file_format(pSession,pcExt);
  if(!pFormat) return NULL;
																											
  /* Is this hint already there? - if so, change its value */																									
  p=tim_find_file_format_hint(pFormat,pcTag);
  if(!p) {
    /* Nope, create it */
    p=(tim_file_format_hint_t *)tim_alloc(pFormat->pSession,sizeof(tim_file_format_hint_t));
    if(!p) return NULL;
		
    /* Set tag */
    strcpy(p->cTag,pcTag);
		
    /* Insert in list */
    p->pNext=NULL;
    p->pPrev=pFormat->pLastHint;
    if(!pFormat->pFirstHint) pFormat->pFirstHint=p;
    else pFormat->pLastHint->pNext=p;
    pFormat->pLastHint=p;
  }
	
  /* Set value */
  strcpy(p->cValue,pcValue);
	
  return p;
}                                                 

int tim_remove_hint(tim_session_t *pSession,char *pcExt,char *pcTag) {
  tim_file_format_t *pFormat;
  tim_file_format_hint_t *p;
	
  /* Find format */
  pFormat=tim_find_file_format(pSession,pcExt);
  if(!pFormat) return TIM_RV_ERR_NOT_FOUND;
	
  /* Find hint */
  p=tim_find_file_format_hint(pFormat,pcTag);
  if(p) {
    /* It's there... get rid of it!! */
    if(p->pNext) p->pNext->pPrev=p->pPrev;
    else pFormat->pLastHint=p->pPrev;

    if(p->pPrev) p->pPrev->pNext=p->pNext;
    else pFormat->pFirstHint=p->pNext;
		
    tim_free(pSession,p);
  }
	
  /* OK */
  return TIM_RV_OK;
}

int tim_is_hint(tim_session_t *pSession,const char *pcExt,const char *pcTag) {
  tim_file_format_t *pFormat;
	
  /* Find format */
  pFormat=tim_find_file_format(pSession,pcExt);
  if(!pFormat) return FALSE;
	
  /* Hint there? */
  if(tim_find_file_format_hint(pFormat,pcTag)) return TRUE;
	
  /* No such hint */
  return FALSE;
}

const char *tim_get_hint(tim_session_t *pSession,const char *pcExt,const char *pcTag,const char *pcDefaultValue) {
  tim_file_format_t *pFormat;
  tim_file_format_hint_t *p;
	
  /* Find format */
  pFormat=tim_find_file_format(pSession,pcExt);
  if(!pFormat) return pcDefaultValue;
	
  /* Find hint */
  p=tim_find_file_format_hint(pFormat,pcTag);
  if(!p) return pcDefaultValue;
	
  /* Return value */
  return p->cValue;
}

int tim_set_hint(tim_session_t *pSession,const char *pcExt,const char *pcTag,char *pcValue) {
  /* Set hint */
  if(!tim_add_file_format_hint(pSession,pcExt,pcTag,pcValue))
    return TIM_RV_ERR_COULD_NOT_PERFORM;
		
  /* OK */
  return TIM_RV_OK;
}

float tim_get_hint_float(tim_session_t *pSession,char *pcExt,char *pcTag,float fDefaultValue) {
  char cTemp[256];

  /* Make default value string */
  sprintf(cTemp,"%f",fDefaultValue);
	
  /* Get & return */
  return (float)atof(tim_get_hint(pSession,pcExt,pcTag,cTemp));
}

int tim_get_hint_int(tim_session_t *pSession,const char *pcExt,const char *pcTag,int nDefaultValue) {
  char cTemp[256];

  /* Make default value string */
  sprintf(cTemp,"%d",nDefaultValue);
	
  /* Get & return */
  return atoi(tim_get_hint(pSession,pcExt,pcTag,cTemp));
}

int tim_set_hint_float(tim_session_t *pSession,char *pcExt,char *pcTag,float fValue) {
  char cTemp[256];
  sprintf(cTemp,"%f",fValue);
  return tim_set_hint(pSession,pcExt,pcTag,cTemp);
}

int tim_set_hint_int(tim_session_t *pSession,const char *pcExt,const char *pcTag,int nValue) {
  char cTemp[256];
  sprintf(cTemp,"%d",nValue);
  return tim_set_hint(pSession,pcExt,pcTag,cTemp);
}

/*==============================================================================
  Image management
  ==============================================================================*/

int tim_get_bytes_per_pixel(tim_pixel_type_t PixelType) {
  switch(PixelType) {
  case TIM_PT_RGB24:
    return 3;
  case TIM_PT_RGBA32:
    return 4;
  default:
    /* Invalid pixel type */
    return 0;
  }
}

tim_image_t *tim_create_image(tim_session_t *pSession,int nWidth,int nHeight,
                              tim_pixel_type_t PixelType) {
  tim_image_t *p;
  int nBytesPerPixel;
	
  /* How many bytes per pixel? */
  nBytesPerPixel=tim_get_bytes_per_pixel(PixelType);
  if(nBytesPerPixel==0) return NULL;

  /* Allocate image struct */
  p=(tim_image_t *)tim_alloc(pSession,sizeof(tim_image_t));
  if(!p) {
    /* Failed to allocate */
    return NULL;
  }
	
  /* Reset contents */
  memset(p,0,sizeof(tim_image_t));
	
  /* Set session */
  p->pSession=pSession;
	
  /* Insert in list */
  p->pPrev=pSession->pLastImage;
  if(!pSession->pFirstImage) pSession->pFirstImage=p;
  else pSession->pLastImage->pNext=p;
  pSession->pLastImage=p;
	
  /* Set basic information */
  p->Info.nWidth=nWidth;
  p->Info.nHeight=nHeight;
  p->Info.PixelType=PixelType;
	
  /* Allocate pixels */
  p->pvData=(void *)tim_alloc(pSession,nWidth*nHeight*nBytesPerPixel);
  if(!p->pvData) {
    tim_free(pSession,p);
    return NULL;
  }
  memset(p->pvData,0,nWidth*nHeight*nBytesPerPixel);
	
  /* Return */
  return p;
}

void tim_destroy_image(tim_image_t *p) {
  /* Remove from list */
  if(p->pNext) p->pNext->pPrev=p->pPrev;
  else p->pSession->pLastImage=p->pPrev;

  if(p->pPrev) p->pPrev->pNext=p->pNext;
  else p->pSession->pFirstImage=p->pNext;

  /* Free pixel data */
  tim_free(p->pSession,p->pvData);
	
  /* Free Struct */
  tim_free(p->pSession,p);
}

int tim_is_image_readable(tim_session_t *pSession,char *pcFileName) {
  tim_file_format_t *pFileFormat;

  /* Find file format */
  pFileFormat=tim_determine_file_format(pSession,pcFileName);
  if(pFileFormat && pFileFormat->tim_callback_load) return TRUE;
  return FALSE;
}

int tim_is_image_writable(tim_session_t *pSession,char *pcFileName) {
  tim_file_format_t *pFileFormat;

  /* Find file format */
  pFileFormat=tim_determine_file_format(pSession,pcFileName);
  if(pFileFormat && pFileFormat->tim_callback_save) return TRUE;
  return FALSE;
}

int tim_get_image_info(tim_session_t *pSession,char *pcFileName,tim_image_info_t *pInfo) {
  tim_file_format_t *pFileFormat;

  /* Find file format */
  pFileFormat=tim_determine_file_format(pSession,pcFileName);
  if(pFileFormat && pFileFormat->tim_callback_load) {
    return pFileFormat->tim_callback_load(pSession,NULL,pInfo,pcFileName);
  }
	
  return TIM_RV_ERR_COULD_NOT_PERFORM;
}

int tim_read_image(tim_session_t *pSession,char *pcFileName,tim_image_t **ppImage) {
  tim_file_format_t *pFileFormat;
 
  /* Find file format */
  pFileFormat=tim_determine_file_format(pSession,pcFileName);
  if(pFileFormat && pFileFormat->tim_callback_load) {
    return pFileFormat->tim_callback_load(pSession,ppImage,NULL,pcFileName);
  }
	
  return TIM_RV_ERR_COULD_NOT_PERFORM;	
}

int tim_write_image(tim_image_t *pImage,char *pcFileName) {
  tim_file_format_t *pFileFormat;

  /* Find file format */
  pFileFormat=tim_determine_file_format(pImage->pSession,pcFileName);
  if(pFileFormat && pFileFormat->tim_callback_save) {
    return pFileFormat->tim_callback_save(pImage,pcFileName);
  }
	
  return TIM_RV_ERR_COULD_NOT_PERFORM;	
}

/*==============================================================================
  Session management
  ==============================================================================*/

void tim_free_session(tim_session_t *pSession) {
  int i,j;
	
  /* Free images */
  while(pSession->pFirstImage) tim_destroy_image(pSession->pFirstImage);

  /* Free memory associated with this session */
  /* File format extensions and hints must be freed */
  for(i=0;i<pSession->nNumFileFormats;i++) {			
    /* Hints */
    while(pSession->FileFormats[i].pFirstHint) 
      tim_remove_hint(pSession,pSession->FileFormats[i].pcExtensions[0],
		      pSession->FileFormats[i].pFirstHint->cTag);

    /* Exts */
    for(j=0;j<pSession->FileFormats[i].nNumExtensions;j++) 
      tim_free(pSession,pSession->FileFormats[i].pcExtensions[j]);
  }
}

int tim_init_session(tim_session_t *pSession,tim_memory_t *pMemory,tim_io_t *pIO) {
  /* Validate memory struct */
  if(pMemory->tim_callback_alloc==NULL ||
     pMemory->tim_callback_free==NULL ||
     pMemory->tim_callback_realloc==NULL)
    return TIM_RV_ERR_INVALID_PARAM;
	
  /* Validate I/O struct */
  if(pIO->tim_callback_close==NULL ||
     pIO->tim_callback_open==NULL ||
     pIO->tim_callback_read==NULL ||
     pIO->tim_callback_seek==NULL ||
     pIO->tim_callback_write==NULL ||
     pIO->tim_callback_eof==NULL)
    return TIM_RV_ERR_INVALID_PARAM;
		
  /* Reset session */
  memset(pSession,0,sizeof(tim_session_t));
		
  /* Copy callback structs */
  memcpy(&pSession->Memory,pMemory,sizeof(tim_memory_t));
  memcpy(&pSession->IO,pIO,sizeof(tim_io_t));
	   
  /* OK */
  return TIM_RV_OK;
}