2 Copyright © 2016, The AROS Development Team. All rights reserved.
6 /******************************************************************************
18 Extras:Multimedia/Gfx/DatatypesToCSource
22 Load picture files with the datatypes library and write C source
23 to stdout. The output can be used by Rawimage MCC.
27 FILES -- the files which should be converted
28 PACK -- compress the image with bz2 library. Defaults to FALSE
35 DatatypeToCSource file1.png file2.png
36 DatatypeToCSource ram:#?.png PACK
44 ******************************************************************************/
46 #include <proto/exec.h>
47 #include <proto/dos.h>
48 #include <proto/datatypes.h>
49 #include <proto/alib.h>
51 #include <datatypes/pictureclass.h>
58 #include <aros/debug.h>
60 static const char version
[] __attribute__((used
)) = "$VER: DatatypeToCSource 1.0 (15.3.2016)\n";
62 static void write_source(STRPTR filename
, UBYTE
*mem
, ULONG width
, ULONG height
, BOOL compress
);
63 static void dt2src(STRPTR filename
, BOOL compress
);
65 #define TEMPLATE "FILES/A/M,PACK/S"
66 #define COMPRESSRATE (9)
67 #define PATHLEN (1024)
77 int main(int argc
, char **argv
)
79 STRPTR
*filenames
= NULL
;
80 BOOL compress
= FALSE
;
82 IPTR args
[ARG_COUNT
] = {0};
83 struct AnchorPath
*anchorpath
;
86 rda
= ReadArgs(TEMPLATE
, args
, NULL
);
89 PrintFault(IoErr(), argv
[0]);
95 filenames
= (STRPTR
*)args
[ARG_FILES
];
102 if (anchorpath
= AllocMem(sizeof(struct AnchorPath
) + PATHLEN
, MEMF_ANY
))
106 memset(anchorpath
, 0, sizeof *anchorpath
);
107 anchorpath
->ap_Strlen
= PATHLEN
;
108 anchorpath
->ap_BreakBits
= SIGBREAKF_CTRL_C
;
110 if ((error
= MatchFirst(*filenames
, anchorpath
)) == 0)
114 D(bug("name %s\n", anchorpath
->ap_Buf
));
115 if (anchorpath
->ap_Info
.fib_DirEntryType
< 0) // ignore dirs
117 dt2src(anchorpath
->ap_Buf
, compress
);
119 } while ((error
= MatchNext(anchorpath
)) == 0);
121 MatchEnd(anchorpath
);
123 if (error
!= ERROR_NO_MORE_ENTRIES
)
125 PrintFault(error
, argv
[0]);
129 FreeMem(anchorpath
, sizeof(struct AnchorPath
) + PATHLEN
);
140 static void dt2src(STRPTR filename
, BOOL compress
)
144 struct BitMapHeader
*bmhd
= NULL
;
145 struct pdtBlitPixelArray bpa_msg
;
149 obj
= NewDTObject(filename
,
150 DTA_GroupID
, GID_PICTURE
,
151 PDTA_DestMode
, PMODE_V43
,
156 PDTA_BitMapHeader
,&bmhd
,
160 width
= bmhd
->bmh_Width
;
161 height
= bmhd
->bmh_Height
;
162 D(bug("width %d height %d depth %d\n", width
, height
, bmhd
->bmh_Depth
));
163 mem
= AllocVec(width
* height
* 4, MEMF_CLEAR
);
166 bpa_msg
.MethodID
= PDTM_READPIXELARRAY
;
167 bpa_msg
.pbpa_PixelData
= mem
;
168 bpa_msg
.pbpa_PixelFormat
= PBPAFMT_ARGB
;
169 bpa_msg
.pbpa_PixelArrayMod
= width
* 4;
170 bpa_msg
.pbpa_Left
= 0;
171 bpa_msg
.pbpa_Top
= 0;
172 bpa_msg
.pbpa_Width
= width
;
173 bpa_msg
.pbpa_Height
= height
;
174 DoMethodA(obj
, (Msg
)&bpa_msg
);
176 write_source(filename
, mem
, width
, height
, compress
);
182 fputs("Can't allocate memory", stderr
);
185 DisposeDTObject(obj
);
189 fprintf(stderr
, "Can't create picture datatype object from file %s\n", filename
);
194 static void write_source(STRPTR filename
, UBYTE
*mem
, ULONG width
, ULONG height
, BOOL compress
)
196 ULONG srclen
= width
* height
* 4;;
199 printf("// Created from file %s\n\n", filename
);
202 unsigned int destlen
= srclen
* 1.1 + 600;
203 unsigned char *dest
= malloc(destlen
);
206 int compressresult
= BZ2_bzBuffToBuffCompress(dest
, &destlen
,
211 if (compressresult
== BZ_OK
)
213 puts("const unsigned char img[] =\n{");
215 printf(" 0x%02x, 0x%02x, 0x%02x, 0x%02x, // width\n",
216 (width
& 0xff000000) >> 24, (width
& 0xff0000) >> 16, (width
& 0xff00) >> 8, (width
& 0xff));
217 printf(" 0x%02x, 0x%02x, 0x%02x, 0x%02x, // height\n",
218 (height
& 0xff000000) >> 24, (height
& 0xff0000) >> 16, (height
& 0xff00) >> 8, (height
& 0xff));
219 puts(" 'B', 'Z', '2', '\\0',");
220 printf(" 0x%02x, 0x%02x, 0x%02x, 0x%02x, // number of bytes\n",
221 (destlen
& 0xff000000) >> 24, (destlen
& 0xff0000) >> 16, (destlen
& 0xff00) >> 8, (destlen
& 0xff));
223 for (i
= 0; i
< destlen
; i
++)
229 printf("0x%02x, ", dest
[i
]);
235 fprintf(stderr
, "BZ2_bzBuffToBuffCompress returned error %d\n", compressresult
);
241 fputs("Can't allocate memory for compressing", stderr
);
246 puts("const unsigned char img[] =\n{");
248 printf(" 0x%02x, 0x%02x, 0x%02x, 0x%02x, // width\n",
249 (width
& 0xff000000) >> 24, (width
& 0xff0000) >> 16, (width
& 0xff00) >> 8, (width
& 0xff));
250 printf(" 0x%02x, 0x%02x, 0x%02x, 0x%02x, // height\n",
251 (height
& 0xff000000) >> 24, (height
& 0xff0000) >> 16, (height
& 0xff00) >> 8, (height
& 0xff));
252 puts(" 0x00, 0x00, 0x00, 0x00,");
253 puts(" 0x00, 0x00, 0x00, 0x00, // number of bytes");
255 for (i
= 0; i
< srclen
; i
++)
261 printf("0x%02x, ", mem
[i
]);