make sure "S" is generated.
[AROS-Contrib.git] / gfx / datatype2csource / main.c
blob0e405f7e022d09452b941a0611c1d3037f9eb4bc
1 /*
2 Copyright © 2016, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 /******************************************************************************
8 NAME
10 DatatypeToCSource
12 SYNOPSIS
14 FILES/A/M, PACK/S
16 LOCATION
18 Extras:Multimedia/Gfx/DatatypesToCSource
20 FUNCTION
22 Load picture files with the datatypes library and write C source
23 to stdout. The output can be used by Rawimage MCC.
25 INPUTS
27 FILES -- the files which should be converted
28 PACK -- compress the image with bz2 library. Defaults to FALSE
30 RESULT
32 NOTES
34 EXAMPLE
35 DatatypeToCSource file1.png file2.png
36 DatatypeToCSource ram:#?.png PACK
38 BUGS
40 SEE ALSO
42 INTERNALS
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>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <bzlib.h>
57 //#define DEBUG 1
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)
69 enum
71 ARG_FILES,
72 ARG_PACK,
73 ARG_COUNT
77 int main(int argc, char **argv)
79 STRPTR *filenames = NULL;
80 BOOL compress = FALSE;
81 struct RDArgs *rda;
82 IPTR args[ARG_COUNT] = {0};
83 struct AnchorPath *anchorpath;
84 LONG error;
86 rda = ReadArgs(TEMPLATE, args, NULL);
87 if (!rda)
89 PrintFault(IoErr(), argv[0]);
90 return RETURN_ERROR;
93 if (args[ARG_FILES])
95 filenames = (STRPTR *)args[ARG_FILES];
97 if (args[ARG_PACK])
99 compress = TRUE;
102 if (anchorpath = AllocMem(sizeof(struct AnchorPath) + PATHLEN, MEMF_ANY))
104 while (*filenames)
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]);
127 filenames++;
129 FreeMem(anchorpath, sizeof(struct AnchorPath) + PATHLEN);
132 if (rda)
134 FreeArgs(rda);
136 return RETURN_OK;
140 static void dt2src(STRPTR filename, BOOL compress)
142 UBYTE *mem;
143 Object *obj;
144 struct BitMapHeader *bmhd = NULL;
145 struct pdtBlitPixelArray bpa_msg;
146 ULONG width;
147 ULONG height;
149 obj = NewDTObject(filename,
150 DTA_GroupID, GID_PICTURE,
151 PDTA_DestMode, PMODE_V43,
152 TAG_DONE);
153 if (obj)
155 GetDTAttrs(obj,
156 PDTA_BitMapHeader,&bmhd,
157 TAG_END);
158 if (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);
164 if (mem)
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);
178 FreeVec(mem);
180 else
182 fputs("Can't allocate memory", stderr);
185 DisposeDTObject(obj);
187 else
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;;
197 ULONG i;
199 printf("// Created from file %s\n\n", filename);
200 if (compress)
202 unsigned int destlen = srclen * 1.1 + 600;
203 unsigned char *dest = malloc(destlen);
204 if (dest)
206 int compressresult = BZ2_bzBuffToBuffCompress(dest, &destlen,
207 mem, srclen,
208 COMPRESSRATE,
210 30);
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++)
225 if ((i % 12) == 0)
227 printf("\n ");
229 printf("0x%02x, ", dest[i]);
231 puts("\n};\n");
233 else
235 fprintf(stderr, "BZ2_bzBuffToBuffCompress returned error %d\n", compressresult);
237 free(dest);
239 else
241 fputs("Can't allocate memory for compressing", stderr);
244 else
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++)
257 if ((i % 12) == 0)
259 printf("\n ");
261 printf("0x%02x, ", mem[i]);
263 puts("\n};\n");