I need to encode some binary blob (say, 100KB) in Dart code. Do not want to put it in a separate file, but hope can put it in .dart code (because, for example, I want the excellent flutter hot restart instead of rebuilding the Flutter app again and again if the file chages).
What is the most brief way to do so?
For example I can come up with final bytes = [100, 70, 220, 132, ...]; but it will make the .dart file huge.
I can also do final data = 'aGVsbG8...' and use it by base64.decode(data), but it takes up 33% extra space in generated binary.
Unfortunately those are your options as there's no way to export binary data from a file (i.e., like directly generating a .o file that exports a symbol in C/C++) without using either a Dart list or some other form of encoding.
However, if you're dealing with fixed binary data representations, I'd highly recommend looking at types provided in dart:typed_data to ensure you're storing this data in memory efficiently (e.g., using UInt8List vs List<int> will use 8-bits per element instead of the 64-bits that can be represented by int).
Related
How can we create a contract for an image upload in base 64?
These base64 strings tend to get very long and we seem to get this exception:
String too long. The given string is 500000 Unicode code units long, but only a maximum of 65535 is allowed
Do you really need to upload such a gigantic image for a contract test? You want to use contract testing to just check whether your application can parse an image, not parse gigantic images. For the latter tests you can do integration testing. In my opinion you're misusing the tool.
As using hardcoded strings in scala(with play) is considered a sin. I have some hardcoded numbers in my code. So can i declare these numbers in the Messages file of play and then access them in project & convert them to Int/BigInteger whatever?
for storing configuration data use application.conf which is located in ./conf directory and then use Play.current.configuration.getInt("some.property") to retrieve it
I am writing a C++ (MFC in particular) class which uses an external .gif image file and produces another image file after some processing. It would be nice if the initial image could be embedded in the code somehow. I have read in MSDN about using multiple .rc files and the whole thing seems quite complicated.
I would like to know from people who have gone through this before how to handle this problem.
EDIT : Sorry I was not clear. The class I am writing should be standalone, so I could use it again. If I put the image in a resource file, then the class will not compile if used in a fresh project.
You cannot embedd MFC resources inside a class or similar C++ container. They can only be embedded in DLL or EXE files - in a separate section of the produced binary. Since you want your class to be reusable, you must put it in a DLL. Hence, you must tag your class with the AFX_EXT_CLASS keyword.
There are two solutions.
Solution #1:
Create an MFC DLL project (MFC Extension DLL). Call it MyLibrary or whatever.
Put all your standalone classes in this DLL.
Embed all necessary resources.
Let your classes load resources from the HINSTANCE of your DLL as described below.
There are several ways to retrieve the HINSTANCE of your DLL. If you ask me, the best solution is to grab it in DllMain. This is done automatically if you choose the MFC Extension DLL configuration:
static AFX_EXTENSION_MODULE MyLibDLL = { NULL, NULL }; // Make this variable global!
// Then access the hInstance as follows:
LoadResource(MyLibDLL.hModule, ...)
Solution #2:
Store your resource as a byte buffer. Or better, convert it to Base64 and store it as an ASCII string. But remember not to blow the stack! Keep your resources small or increase the stack size in your project settings. Example:
const char *encodedResource = "SGVsbG8gd29ybGQh";
char *data = decode(encodedResource);
foo(data);
In the solution explorer go to resource view, Right click and click Add Resource then click Import and add the gif file. Now you can use your Resource ID to access the gif file in your code.
Just adding the file to a resource doesn't embed the file in the actual resource file it just links to the file. If you open your .rc file you'll see it says something like:
IDB_GIF_MYIMAGE GIF "artwork\\mygif.gif"
During the compilation face the resource will be included in the EXE, which you reference using the resource id IDB_GIF_MYIMAGE. You can reference the same file in other projects without having to duplicate the file.
To embed an image (or any other type of binary data) into your class without using resource files, use the bin2c utility, for example you can download it from here: http://www.opensource.apple.com/source/libpcap/libpcap-16/libpcap/msdos/bin2c.c . Running this on a file will produce what is basically a static array with the bytes of the file as members of that array. Stuff this array into a .h file (or put it in the header of your class, or make it a static member...) and then you will have that file available in-memory without having to use LoadResource() and its brethren.
If you want to use this with CImage::Load(), you will have to write your own class that derives from IStream, and implement a few of the methods in a way so that they 'read' from memory. I don't know of any ways to let CImage decode an image from an in-memory representation of a gif file.
I think the best solution is just to document that to use the class you must also import to your project a certain .gif file and give it a certain expected identifier (e.g. IDB_MYCLASS_MYGIF). You can then use the preprocessor to detect if the resource has been correctly added, e.g.:
#ifndef IDB_MYCLASS_MYGIF
#error Make sure you import mygif.gif to the project. See docs for more info.
#endif
This will prevent the class compiling until the user imports the image properly. Alternatively you could just use #ifdefs to fall back to code which does not use the default image if it is not provided.
Have a look at the CRuntimeDialog class presented in http://www.codeproject.com/Articles/5371/ToDoList-6-5-4-Feature-Release-An-effective-and-fl . It provides a way to create a dialog from the string that makes up the resource definition.
I use openCV. I've trained SVM, but the output file is too large (after svm.save()). I want to use trained SVM in iPhone's application, but the file is about 300 Mb. File's size after archive is about 6 Mb.
So, my questions:
1) Can I extract the archive with bzip2 when iPhone application starts and then use this file with SVM::load ?
2) Can CvSVM's save/load methods work with binary formats?
3) Do I need all information from output file to do a prediction?
4) Does a better solution exist for this problem?
Thank you in advance!
You may try to save svm object with bzip library (if it is possible, I don't know this library) and try to load the object with bzip afterwards to memory and for example use a pointer to it. CvSVM.load() generates xml file with support vectors and some additional data. I don't know if any of it is optional, but I doubt it.
I'm currently making a word game for iOS that, when loading, reads in a text file of around 30000 words and loads them into a prefix tree for quick searching during gameplay. This works well, but the loading and tree construction process adds a noticeable few seconds to the app's startup time. At the moment I'm testing on an iPhone 4, but I imagine it would be a good deal slower on a 3GS earlier models.
Is there a way to create this tree at compile time rather than when the app opens? Or, less ideally, would it be possible to prebake the data with another program and add that file to the project instead of doing it at runtime? How would I go about doing that?
I run into the same issue with a game we developed, and for us, it performed better to use a SQLite DB with the words instead of the in-memory tree. The DB used less space than the plist that represented the tree, it did not require us to preload it in memory, and the performance (when querying for a valid word) was about the same.
pgb's answer is good. If you do not want to use SQLite, you can store your data in a plist and have [NSDictionary dictionaryWithContentsOfFile:] create a tree for you.
If you do choose to have the data compiled into your program, it will have to be built of primitive types, such as numbers and characters. Then, use structures and arrays to define the structure, and use a constant variable to store the data. Here is a simple example which is just an array of character strings:
const char *words[] = {"Word1","Word2","Word3"};
const unsigned numWords = (sizeof(words) / sizeof(char*));