3 File reading/writing functions. (done)
4 Image loading (via datatypes) (done).
5 Chunky Image loading. (done)
6 Texture loading (automatical scaling).
8 Implemented better error handling.
9 If picture is not found it is replaced by blank image and black palette.
10 If any function fails to allocate memory then program exits automaticaly.
13 Forget to free temporary ARGB buffer in 16bit loading.
15 Improved error handling a bit.
16 Texture filename is preserved (for texture caching?)
18 Added some error handling into JPEG loading code
19 Added some speed optimizations (50% in DT loader!)
20 Fixed some bugs after optimizations.
22 Removed some obsolete functions (8bit support mainly)
25 Cleaned up the code. Fixed 32bit DT loader and some other loading functions.
26 Also some textures haven't got the name assigned.
29 Moved some stuff into other files.
38 #include <exec/memory.h>
40 #include <proto/alib.h>
41 #include <proto/dos.h>
42 #include <proto/graphics.h>
43 #define USE_INLINE_STDARG
44 #include <proto/multimedia.h>
45 #include <classes/multimedia/video.h>
46 #undef USE_INLINE_STDARG
48 #include <utility/tagitem.h>
49 #define SYSTEM_PRIVATE
50 #include <intuition/extensions.h>
52 #include <datatypes/pictureclass.h>
53 #include <intuition/gadgetclass.h>
55 #include <proto/datatypes.h>
56 #include <proto/dtclass.h>
57 #include <proto/muimaster.h>
59 #include <cybergraphx/cybergraphics.h>
61 #include <emul/emulregs.h>
62 #include <emul/emulinterface.h>
67 #include "functions.h"
69 #include "loadsunrast.h"
75 static int debugmode
= 0;
77 static char *lastFileType
= NULL
;
78 static void setLastFileType(char *type
);
80 struct Library
*DataTypesBase
;
82 void dprintf(char *fmt
, ...);
84 void SetDebugMode(int mode
)
89 //#define ENABLE_16BIT
93 void *fileLoad(char *name
)
95 return dataLoadFile(name
);
98 void fileFree(void *file
)
105 /// fileGetFilePart()
107 char *fileGetFilePart(char *fname
)
110 return FilePart(fname
);
119 int savefile(char *name
, void *mem
, int len
)
123 file
= fopen(name
, "w");
126 printf("Could not open file for writing:%s!\n", name
);
130 fwrite(mem
, len
, 1, file
);
139 unsigned int fileLength(char *fname
)
141 unsigned int l
= dataFileLength(fname
);
148 Texture
*AllocTextureStruct(void)
150 return (Texture
*)calloc(1, sizeof(Texture
));
157 void FreeTexture(Texture
*txt
)
164 free(txt
->alphaname
);
172 /// txt_CreateBlank8()
174 Texture
*txt_CreateBlank(int width
, int height
)
176 return txtCreateBlank(width
, height
, CHK_PIXFMT_LUT
);
181 /// txt_CreateBlank16()
183 Texture
*txt_CreateBlank16(int width
, int height
)
185 return txtCreateBlank(width
, height
, CHK_PIXFMT_RGB565
);
190 /// txt_CreateBlank24()
192 Texture
*txt_CreateBlank24(int width
, int height
)
194 return txtCreateBlank(width
, height
, CHK_PIXFMT_RGB888
);
199 /// txt_CreateBlank32()
201 Texture
*txt_CreateBlank32(int width
, int height
)
203 return txtCreateBlank(width
, height
, CHK_PIXFMT_ARGB8888
);
207 /// txtCreate(width, height, format)
209 Texture
*txtCreate(int width
, int height
, int pixfmt
)
211 Texture
*txt
= AllocTextureStruct();
215 txt
->image
= chkCreate(width
, height
, pixfmt
, FALSE
);
217 if (txt
->image
!= NULL
)
219 /* make sure that pixel format is set correctly */
221 txt
->image
->pixfmt
= pixfmt
;
235 /// txtCreateFromImage(ChkImage)
237 Texture
*txtCreateFromImage(ChkImage
*image
)
239 Texture
*txt
= AllocTextureStruct();
249 /// txtCreateBlank(width, height, format)
251 Texture
*txtCreateBlank(int width
, int height
, int pixfmt
)
253 Texture
*txt
= txtCreate(width
, height
, pixfmt
);
257 memset(txt
->image
->data
.p
, 0, width
* height
* fmtGetBytesPerPixel(pixfmt
));
265 /// static LoadDTPicture()
267 static Texture
*LoadDTPicture(char *fname
, int chkpixfmt
)
270 unsigned int *cregs
, numcols
;
271 struct BitMapHeader
*bmhd
;
272 struct BitMap
*bitmap
;
273 struct pdtBlitPixelArray pdtmsg
;
277 //__timerGlobalStart();
279 if (chkpixfmt
!=CHK_PIXFMT_ARGB8888
&& chkpixfmt
!= CHK_PIXFMT_RGB888
)
284 //printf("newdtobject:%s\n", fname);
285 //printf("%d\n", AvailMem(MEMF_ANY));
286 dt_object
= NewDTObject (fname
,
287 DTA_GroupID
, GID_PICTURE
,
288 PDTA_DestMode
, PMODE_V43
,
290 PDTA_Displayable
, FALSE
,
293 //printf("dto:%d. freemem: %d\n", dt_object, AvailMem(MEMF_ANY));
297 dt_object
= NewDTObject (0,
298 DTA_GroupID
, GID_PICTURE
,
299 DTA_SourceType
, DTST_CLIPBOARD
,
300 PDTA_DestMode
, PMODE_V43
,
302 PDTA_Displayable
, FALSE
,
305 if (dt_object
== NULL
)
309 if (dt_object
== NULL
)
311 //printf("not enough memory\n");
317 struct TagItem tags
[] =
319 {PDTA_CRegs
, (unsigned int) &cregs
},
320 {PDTA_NumColors
, (unsigned int) &numcols
},
321 {PDTA_BitMapHeader
, (unsigned int) &bmhd
},
322 {PDTA_BitMap
, (unsigned int) &bitmap
},
323 {PDTA_AlphaChannel
, (unsigned int) &alpha
},
327 result
= GetDTAttrsA(dt_object
, tags
);
331 printf("[FILE]: Error in datatype\n");
332 DisposeDTObject(dt_object
);
337 //printf("time:%f\n",__timerGlobalGet(1.0f));
338 //__timerGlobalStart();
344 struct DataType
*dt
= NULL
;
346 if (GetDTAttrs(dt_object
, DTA_DataType
, (unsigned int)&dt
, TAG_DONE
))
350 char *name_string
= dt
->dtn_Header
->dth_Name
;
351 setLastFileType(name_string
);
356 setLastFileType("Unknown");
360 txt
= txtCreate(bmhd
->bmh_Width
, bmhd
->bmh_Height
, chkpixfmt
);
362 //printf("time2:%f\n",__timerGlobalGet(1.0f));
363 //__timerGlobalStart();
367 //printf("[FILE]: Can't allocate buffer\n");
368 DisposeDTObject(dt_object
);
372 if (bmhd
->bmh_Depth
<= 8) // remap image to 24bit
374 struct RastPort rastport
, temprastport
;
375 struct BitMap
*tempbitmap
;
376 unsigned char *target
, *colormap
;
377 unsigned char *penarray
;
380 tempbitmap
= AllocBitMap(bmhd
->bmh_Width
, 1, bmhd
->bmh_Depth
, BMF_CLEAR
, NULL
);
383 printf("[FILE]: Can't allocate temporary bitmap\n");
384 DisposeDTObject(dt_object
);
389 colormap
= calloc(numcols
, 3);
390 for (i
= 0; i
< numcols
; i
++)
392 colormap
[ i
* 3 + 0 ] = cregs
[ 3 * i
+ 0 ] >> 24;
393 colormap
[ i
* 3 + 1 ] = cregs
[ 3 * i
+ 1 ] >> 24;
394 colormap
[ i
* 3 + 2 ] = cregs
[ 3 * i
+ 2 ] >> 24;
397 InitRastPort(&rastport
);
398 rastport
.BitMap
= bitmap
;
399 InitRastPort(&temprastport
);
400 temprastport
.BitMap
= tempbitmap
;
401 penarray
= calloc(1, ((bmhd
->bmh_Width
+ 15) >> 4) << 4);
403 target
= txt
->image
->data
.b
;
404 for (y
= 0; y
< bmhd
->bmh_Height
; y
++)
406 ReadPixelLine8(&rastport
, 0, y
, bmhd
->bmh_Width
, penarray
, &temprastport
);
408 if (chkpixfmt
== CHK_PIXFMT_ARGB8888
)
410 for (x
= 0; x
< bmhd
->bmh_Width
; x
++)
413 *target
++ = colormap
[ penarray
[ x
] * 3 ];
414 *target
++ = colormap
[ penarray
[ x
] * 3 + 1 ];
415 *target
++ = colormap
[ penarray
[ x
] * 3 + 2 ];
418 else if (chkpixfmt
== CHK_PIXFMT_RGB888
)
420 for (x
= 0; x
< bmhd
->bmh_Width
; x
++)
422 *target
++ = colormap
[ penarray
[ x
] * 3 ];
423 *target
++ = colormap
[ penarray
[ x
] * 3 + 1 ];
424 *target
++ = colormap
[ penarray
[ x
] * 3 + 2 ];
431 FreeBitMap(tempbitmap
);
434 else if ( bmhd
->bmh_Depth
== 24 && alpha
== 0)
439 for (i
=0;i
<txt
->image
->height
;i
++)
443 if (chkpixfmt
== CHK_PIXFMT_ARGB8888
)
444 dst
= txt
->image
->data
.b
+ i
* bmhd
->bmh_Width
* 4;
446 dst
= txt
->image
->data
.b
+ i
* bmhd
->bmh_Width
* 3;
448 pdtmsg
.MethodID
= PDTM_READPIXELARRAY
;
449 pdtmsg
.pbpa_PixelData
= dst
;
450 pdtmsg
.pbpa_PixelFormat
= chkpixfmt
== CHK_PIXFMT_ARGB8888
? PBPAFMT_ARGB
: PBPAFMT_RGB
;
451 pdtmsg
.pbpa_PixelArrayMod
= bmhd
->bmh_Width
* 3;
452 pdtmsg
.pbpa_Left
= 0;
454 pdtmsg
.pbpa_Width
= bmhd
->bmh_Width
;
455 pdtmsg
.pbpa_Height
= 1;
457 DoMethodA(dt_object
, (Msg
)&pdtmsg
);
459 /* this method for RGB images sets alpha to 0. We need to set it to 0xff */
461 if (chkpixfmt
== CHK_PIXFMT_ARGB8888
)
462 convertARGB0888_to_ARGB8888(dst
, dst
, txt
->image
->width
, 1);
467 __timerGlobalStart();
469 pdtmsg
.MethodID
= PDTM_READPIXELARRAY
;
470 pdtmsg
.pbpa_PixelData
= txt
->image
->data
.p
;
471 pdtmsg
.pbpa_PixelFormat
= PBPAFMT_RGB
;
472 pdtmsg
.pbpa_PixelArrayMod
= bmhd
->bmh_Width
* 3;
473 pdtmsg
.pbpa_Left
= 0;
475 pdtmsg
.pbpa_Width
= bmhd
->bmh_Width
;
476 pdtmsg
.pbpa_Height
= bmhd
->bmh_Height
;
478 DoDTMethodA(dt_object
, (Msg
) & pdtmsg
);
479 //printf("time3.1:%f\n",__timerGlobalGet(1.0f));
480 //__timerGlobalStart();
481 //expandRGB888_to_ARGB8888(txt->image->data.p, txt->image->data.p, txt->image->width, txt->image->height);
482 //printf("time3.2:%f\n",__timerGlobalGet(1.0f));
489 pdtmsg
.MethodID
= PDTM_READPIXELARRAY
;
490 pdtmsg
.pbpa_PixelData
= txt
->image
->data
.p
;
491 pdtmsg
.pbpa_PixelFormat
= chkpixfmt
== CHK_PIXFMT_ARGB8888
? PBPAFMT_ARGB
: PBPAFMT_RGB
;
492 pdtmsg
.pbpa_PixelArrayMod
= bmhd
->bmh_Width
* (chkpixfmt
== CHK_PIXFMT_ARGB8888
? 4 : 3);
493 pdtmsg
.pbpa_Left
= 0;
495 pdtmsg
.pbpa_Width
= bmhd
->bmh_Width
;
496 pdtmsg
.pbpa_Height
= bmhd
->bmh_Height
;
498 DoMethodA(dt_object
, (Msg
)&pdtmsg
);
501 //printf("time3:%f\n",__timerGlobalGet(1.0f));
502 //__timerGlobalStart();
503 DisposeDTObject(dt_object
);
504 //printf("time4:%f\n",__timerGlobalGet(1.0f));
510 /// static LoadDTPictureTRUE24()
512 static Texture
*LoadDTPictureTRUE24(char *fname
)
514 return LoadDTPicture(fname
, CHK_PIXFMT_RGB888
);
518 /// static LoadDTPictureTRUE32()
520 static Texture
*LoadDTPictureTRUE32(char *fname
)
522 return LoadDTPicture(fname
, CHK_PIXFMT_ARGB8888
);
526 /// static LoadReggaePictureTRUE32()
528 static Texture
*LoadReggaePicture(char *fname
, int chkpixfmt
)
531 APTR fileobj
= MultimediaBase
!= NULL
? MediaNewObjectTags(
532 MMA_StreamName
, (IPTR
)fname
,
533 MMA_StreamType
, (IPTR
)"file.stream",
534 MMA_MediaType
, MMT_PICTURE
,
541 pics
= xget(fileobj
, MMA_Ports
);
545 int width
= MediaGetPort(fileobj
, 0, MMA_Video_Width
);
546 int height
= MediaGetPort(fileobj
, 0, MMA_Video_Height
);
548 if (width
> 0 && height
> 0)
550 txt
= txtCreate(width
, height
, chkpixfmt
);
555 if (chkpixfmt
== CHK_PIXFMT_ARGB8888
)
556 DoMethod(fileobj
, MMM_Pull
, 0, txt
->image
->data
.p
, width
* height
* 4);
559 unsigned char *buffer
= mmalloc(width
* 4);
564 for(i
=0; i
<height
; i
++)
566 DoMethod(fileobj
, MMM_Pull
, 0, buffer
, width
* 4);
567 convertRGB888_to_ARGB8888(buffer
, txt
->image
->data
.b
+ width
* 3 * i
, width
, 1);
577 DisposeObject(fileobj
);
587 void fileGetType(char *fname
, char *type
)
591 BPTR lock
= Lock(fname
, ACCESS_READ
);
593 strcpy(type
, "Unknown");
597 /* check if it's recognizable by datatypes */
599 struct DataType
*dt
= ObtainDataTypeA(DTST_FILE
, (APTR
)lock
, NULL
);
603 char *name_string
= dt
->dtn_Header
->dth_Name
;
605 strcpy(type
, name_string
);
607 if (!stricmp(type
, "JFIF"))
608 strcpy(type
, "JPEG");
618 /// fileGetInfo()/fileFreeInfo()
620 FileInfo
*fileGetInfo(char *fname
, int flags
)
624 finfo
= calloc(1, sizeof(FileInfo
));
630 BPTR lock
= Lock(fname
, ACCESS_READ
);
634 struct DataType
*dt
= NULL
;
637 /* check if it's recognizable by datatypes */
639 if (flags
& FINFO_DATATYPE
)
640 dt
= ObtainDataTypeA(DTST_FILE
, (APTR
)lock
, NULL
);
644 char *name_string
= dt
->dtn_Header
->dth_Name
;
646 finfo
->type
= strdup(name_string
);
647 if (finfo
->type
&& !stricmp(finfo
->type
, "JFIF"))
648 strcpy(finfo
->type
, "JPEG");
650 finfo
->gid
= dt
->dtn_Header
->dth_GroupID
;
654 else if (MultimediaBase
!= NULL
) /* check if recognized by reggae */
656 fileobj
= MediaNewObjectTags(
657 MMA_StreamName
, (IPTR
)fname
,
658 MMA_StreamType
, (IPTR
)"file.stream",
659 MMA_MediaType
, MMT_PICTURE
,
664 ULONG pics
= xget(fileobj
, MMA_Ports
);
668 char *type
= (char*)MediaGetPort(fileobj
, 0, MMA_DataFormat
);
670 finfo
->type
= strdup(type
);
672 finfo
->gid
= GID_PICTURE
;
676 DisposeObject(fileobj
);
682 if (finfo
->type
== NULL
)
684 finfo
->type
= strdup("Unknown");
687 /* get dimmensions */
689 if (flags
& FINFO_DIMMENSIONS
)
691 int success
= pngGetDimmensions(fname
, &finfo
->width
, &finfo
->height
);
692 if (success
== FALSE
)
693 success
= jpegGetDimmensions(fname
, &finfo
->width
, &finfo
->height
);
694 if (success
== FALSE
)
696 Object
*dt_object
= NewDTObject (fname
,
697 DTA_GroupID
, GID_PICTURE
,
698 PDTA_DestMode
, PMODE_V43
,
700 PDTA_Displayable
, FALSE
,
703 if (dt_object
!= NULL
)
705 struct BitMapHeader
*bmhd
;
706 struct TagItem tags
[] =
708 {PDTA_BitMapHeader
, (unsigned int) &bmhd
},
712 int result
= GetDTAttrsA(dt_object
, tags
);
716 finfo
->width
= bmhd
->bmh_Width
;
717 finfo
->height
= bmhd
->bmh_Height
;
721 DisposeDTObject(dt_object
);
724 if (success
== FALSE
)
728 finfo
->width
= MediaGetPort(fileobj
, 0, MMA_Video_Width
);
729 finfo
->height
= MediaGetPort(fileobj
, 0, MMA_Video_Height
);
735 DisposeObject(fileobj
);
737 /* get other file properties (timestamp. size) */
740 struct FileInfoBlock fib
;
742 if (Examine(lock
, &fib
))
744 finfo
->datestamp
= fib
.fib_Date
;
745 finfo
->filesize
= fib
.fib_Size
;
761 void fileFreeInfo(FileInfo
*finfo
)
772 /// LoadPictureHI16()
774 Texture
*LoadPictureHI16(char *fname
)
778 /* try to load using JPEG loader */
779 if (stricmp(fname
+ strlen(fname
) - 4, ".jpg") == 0)
781 txt
= LoadPictureJPEG(fname
);
787 txt
= LoadDTPictureTRUE24(fname
);
791 /* convert image into 16bit RGB */
792 Texture
*nTxt
= txtCreate(txt
->image
->width
, txt
->image
->height
, CHK_PIXFMT_RGB565
);
800 convertRGB888_to_RGB565(txt
->image
->data
.p
, nTxt
->image
->data
.p
, txt
->image
->width
, txt
->image
->height
);
809 //printf("[File]: Failed to load image: %s\n", fname);
813 txt
->name
= strduplicate(fname
);
820 /// LoadPictureTRUE24()
822 Texture
*LoadPictureTRUE24(char *fname
)
829 /* load from clipboard */
831 txt
= LoadDTPictureTRUE32(NULL
);
836 txt
->name
= strduplicate("Clipboard");
843 /* try to load using JPEG loader */
844 if (stricmp(fname
+ len
- 4, ".jpg") == 0)
846 txt
= LoadPictureJPEG24(fname
);
847 setLastFileType("JPEG");
850 if ((!txt
) && len
> 4 && (stricmp(fname
+ len
- 4, ".ras") == 0))
852 txt
= SUNLoadPicture24(fname
);
853 setLastFileType("Sun Raster");
855 if ((!txt
) && len
> 6 && (stricmp(fname
+ len
- 6, ".ras24") == 0))
857 txt
= SUNLoadPicture24(fname
);
858 setLastFileType("Sun Raster");
860 if ((!txt
) && len
> 6 && (stricmp(fname
+ len
- 6, ".ras32") == 0))
862 txt
= SUNLoadPicture24(fname
);
863 setLastFileType("Sun Raster");
868 txt
= LoadDTPictureTRUE24(fname
);
873 //printf("[File]: Failed to load image: %s\n", fname);
877 txt
->name
= strduplicate(fname
);
878 txt
->type
= strDuplicate(getLastFileType());
885 /// LoadPictureGRAY8()
887 Texture
*LoadPictureGRAY8(char *fname
)
891 /* try to load using JPEG loader */
892 if (stricmp(fname
+ strlen(fname
) - 4, ".jpg") == 0)
894 txt
= LoadPictureJPEG8(fname
);
902 txt
= LoadDTPictureTRUE24(fname
);
906 /* convert image into 8bit LUMINANCE */
907 Texture
*nTxt
= txtCreate(txt
->image
->width
, txt
->image
->height
, CHK_PIXFMT_LUMINANCE
);
915 convertRGB888_to_L8(txt
->image
->data
.p
, nTxt
->image
->data
.p
, txt
->image
->width
, txt
->image
->height
);
924 //printf("[File]: Failed to load image: %s\n", fname);
928 txt
->name
= strduplicate(fname
);
935 /// LoadPictureTRUE32()
937 Texture
*LoadPictureTRUE32(char *fname
)
939 return LoadPictureARGB32(fname
);
944 /// LoadPictureARGB16()
947 Texture
*LoadPictureARGB16(char *fname
)
951 /* try to load using JPEG loader */
952 if (stricmp(fname
+ strlen(fname
) - 4, ".jpg") == 0)
954 txt
= LoadPictureJPEG32(fname
);
958 txt
= LoadDTPictureTRUE32(fname
);
960 /* convert into 16bit ARGB */
964 Texture
*nTxt
= txtCreate(txt
->image
->width
, txt
->image
->height
, CHK_PIXFMT_ARGB4444
);
972 convertARGB8888_to_ARGB4444(txt
->image
->data
.p
, nTxt
->image
->data
.p
, txt
->image
->width
, txt
->image
->height
);
980 //printf("[File]: Failed to load image: %s\n", fname);
984 txt
->name
= strduplicate(fname
);
991 /// LoadPictureARGB32()
993 Texture
*LoadPictureARGB32(char *fname
)
1000 /* load from clipboard */
1002 txt
= LoadDTPictureTRUE32(NULL
);
1007 txt
->name
= strduplicate("Clipboard");
1011 len
= strlen(fname
);
1014 /* try to load using JPEG loader */
1015 if (stricmp(fname
+ len
- 4, ".jpg") == 0)
1017 //__timerGlobalStart();
1018 txt
= LoadPictureJPEG32(fname
);
1019 setLastFileType("JPEG");
1021 //printf("time:%f\n",__timerGlobalGet(1.0f));
1024 if ((!txt
) && len
> 4 && (stricmp(fname
+ len
- 4, ".ras") == 0))
1026 txt
= SUNLoadPicture32(fname
);
1027 setLastFileType("Sun Raster");
1029 if ((!txt
) && len
> 6 && (stricmp(fname
+ len
- 6, ".ras24") == 0))
1031 txt
= SUNLoadPicture32(fname
);
1032 setLastFileType("Sun Raster");
1034 if ((!txt
) && len
> 6 && (stricmp(fname
+ len
- 6, ".ras32") == 0))
1036 txt
= SUNLoadPicture32(fname
);
1037 setLastFileType("Sun Raster");
1041 txt
= LoadDTPictureTRUE32(fname
);
1044 txt
= LoadReggaePicture(fname
, CHK_PIXFMT_ARGB8888
);
1048 //printf("[File]: Failed to load image: %s\n", fname);
1052 txt
->name
= strduplicate(fname
);
1053 txt
->type
= strDuplicate(getLastFileType());
1064 int isDTPicture(char *fname
)
1068 //__timerGlobalStart();
1072 BPTR lock
= Lock(fname
, ACCESS_READ
);
1076 /* check if it's recognizable by datatypes */
1078 struct DataType
*dt
= ObtainDataType(DTST_FILE
, (APTR
)lock
, DTA_GroupID
, GID_PICTURE
, NULL
);
1082 /* check if it's a picture (should always be, but well) */
1084 if (dt
->dtn_Header
->dth_GroupID
== GID_PICTURE
)
1087 ReleaseDataType(dt
);
1092 /* check if recognized by reggae */
1094 if (isPicture
== FALSE
)
1096 APTR fileobj
= MultimediaBase
!= NULL
? MediaNewObjectTags(
1097 MMA_StreamName
, (IPTR
)fname
,
1098 MMA_StreamType
, (IPTR
)"file.stream",
1099 MMA_MediaType
, MMT_PICTURE
,
1102 if (fileobj
!= NULL
)
1105 DisposeObject(fileobj
);
1110 /* check for builtin image formats */
1113 static char *suffixes
[] = {
1120 int len
= strlen(fname
);
1125 int slen
= strlen(suffixes
[ i
]);
1128 if (0 == stricmp(suffixes
[ i
], &fname
[ len
- slen
]))
1136 //printf("Time of analysis:%f\n",__timerGlobalGet(1.0f));
1143 /* Fast recognition method. Not too accurate though */
1146 static char *suffixes
[] = {
1168 int len
= strlen(fname
);
1173 int slen
= strlen(suffixes
[ i
]);
1176 if (0 == stricmp(suffixes
[ i
], &fname
[ len
- slen
]))
1191 /// checkExtension()
1193 int checkExtension(char *fname
, char *extension
)
1195 int len
= strlen(fname
);
1196 int slen
= strlen(extension
);
1200 if (0 == stricmp(extension
, &fname
[len
- slen
]))
1211 void dirDelete(char *path
, int recursive
)
1215 BPTR dirLock
= Lock(path
, ACCESS_READ
);
1216 struct FileInfoBlock fib
;
1223 if (Examine(dirLock
, &fib
))
1227 /* fine. we can start examining the entries */
1229 while(ExNext(dirLock
, &fib
))
1231 /* build new path */
1233 int buffSize
= strlen(path
) + strlen(fib
.fib_FileName
) + 2;
1234 char *newPath
= calloc(1, buffSize
);
1238 strcpy(newPath
, path
);
1239 AddPart(newPath
, fib
.fib_FileName
, buffSize
);
1241 /* check type of an entry */
1243 if (isDir(&fib
) && recursive
)
1245 /* call ourself for this new directory */
1247 dirDelete(newPath
, recursive
);
1251 DeleteFile(newPath
);
1258 printf("Not enough memory for path(%d bytes)\n",buffSize
);
1275 int fileDelete(char *file
)
1277 return DeleteFile(file
);
1284 int fileCopyTo(char *src
, char *dst
)
1291 /* check if they are equal first */
1293 if (pathEquals(src
, dst
))
1300 buf
= (char*)malloc(65536);
1304 BPTR f1
= Open(src
, MODE_OLDFILE
);
1309 SetIoErr(ERROR_OBJECT_NOT_FOUND
);
1314 f2
= Open(dst
, MODE_NEWFILE
);
1318 int len
= fileLength(src
);
1320 if (len
!= SetFileSize(f2
, len
, OFFSET_BEGINING
))
1322 /* not enough space on the device */
1331 Seek(f2
, 0, OFFSET_BEGINING
);
1336 int bytesr
= Read(f1
, buf
, min(len
, 65536));
1338 if (bytesr
!= min(len
, 65536))
1349 bytesw
= Write(f2
, buf
, min(len
, 65536));
1351 if (bytesr
!= bytesw
)
1362 len
-= min(len
, 65536);
1372 /* failed to open file for read or write */
1389 * This one kind of doesn't fit here, but well....
1392 int filesCopy(Object
*app
, char *reqtitle
, char **list
, char *dest
, int move
)
1397 while(list
[selected
] != NULL
)
1402 /* get destination */
1409 /* process each name */
1411 for(i
=0; i
<num
; i
++)
1413 char *name
= list
[ i
];
1415 /* add path to dest */
1417 char *newdest
= strAddFilePart(dest
, FilePart(name
));
1424 if (forall
!= 2 && newdest
) /* forall == 2 -> about, but we move through all anyway */
1426 /* check if file exists */
1428 if (fileLength(newdest
))
1430 if (forall
== 1) /* replace */
1432 else if (forall
== -1)/* skip */
1436 /* file exists. display req */
1441 res
= MUI_Request(app
, NULL
, 0, reqtitle
, "*_Replace|Replace _All|_Skip|S_kip All|Abort ",
1442 "Destination file \33b%s \33nalready exists", FilePart(path
),
1447 case 2: /* replace all */
1449 case 1: /* fall through to replace */
1452 case 4: /* skip all */
1454 case 3: /* fall through to skip */
1464 res
= MUI_Request(app
, NULL
, 0, reqtitle
, "*_Replace|Abort ",
1465 "Destination file \33b%s \33nalready exists", FilePart(newdest
),
1469 case 1: /* replace all */
1472 case 0: /* skip all */
1484 if (!fileCopyTo(path
, newdest
))
1490 Fault(IoErr(), "", msg
, sizeof(msg
));
1492 MUI_Request(app
, NULL
, 0, "ShowGirls ยท Error...", "OK",
1493 "Failed to copy file:\n\n\033b%s\033n to\n\033b%s\033n\n\nError message %s", path
, dest
, msg
,
1499 /* if "move" mode then delete file */
1506 /* free singlefile paths */
1517 char *getLastFileType(void)
1519 return lastFileType
? lastFileType
: "";
1524 int pathEquals(char *p1
, char *p2
)
1526 BPTR l1
= Lock(p1
, ACCESS_READ
);
1527 BPTR l2
= Lock(p2
, ACCESS_READ
);
1532 if (l1
== NULL
|| l2
== NULL
)
1537 if (NameFromLock(l1
, path1
, sizeof(path1
)) && NameFromLock(l2
,path2
,sizeof(path2
)))
1539 if (strcmp(path1
, path2
) == 0)
1552 static void setLastFileType(char *type
)
1556 lastFileType
= strdup(type
);
1557 if (lastFileType
&& !stricmp(lastFileType
, "JFIF"))
1558 strcpy(lastFileType
, "JPEG");
1561 /// fileFormatString()
1563 void fileFormatString(char *str
, int n
, char *fname
)
1568 /// CapacityFormat() - capacity formatter
1570 char *CapacityFormat(char *s
, unsigned int size
, unsigned long long n
)
1573 snprintf(s
, size
, "%u bytes", (unsigned int)n
);
1574 else if (n
< 1024 * 1024)
1575 snprintf(s
, size
, "%u.%u KB", (unsigned int) (n
/ 1024), (unsigned int)(n
% 1024 * 10 / 1024));
1576 else if (n
< 1024 * 1024 * 1024)
1577 snprintf(s
, size
, "%u.%u MB", (unsigned int) n
/ (1024 * 1024), (unsigned int)(n
% (1024 * 1024) * 10 / (1024 * 1024)));
1578 else if (n
< (unsigned long long)1024 * 1024 * 1024 * 1024)
1579 snprintf(s
, size
, "%u.%u GB", (unsigned int) (n
/ (1024 * 1024 * 1024)), (unsigned int) (n
% (1024 * 1024 * 1024) * 10 / (1024 * 1024 * 1024)));
1581 snprintf(s
, size
, "%u.%u TB", (unsigned int) (n
/ ((unsigned long long)1024 * 1024 * 1024 * 1024)), (unsigned int)(n
% ((unsigned long long)1024 * 1024 * 1024 * 1024) * 10 / ((unsigned long long)1024 * 1024 * 1024 * 1024)));