Why not use a string for binary data? I need to merge pieces together, and being able to do things like string x = y + z is so much easier than resizing an array, and copying in the data. I haven't ever tried returning an array from a function, but I know I can return a string. Also, I didn't realize that there was the possibility of writing anything
but a string to a file.
Sorry, I didn't mean to be keeping the code a secret. It's just that normally when someone posts a large program with #include s, you ask for an example program that demonstrates the problem.
Here is the main program:
Code: Select all
#include "File BMP Mono lib.nxc"
#include "File.nxc"
#define Width 128
#define Hight 128
#define Size 2048
#define uint32_t unsigned long
#define AdvSize 1.40625 // convert 0-255 to 0-359
uint32_t gray_encode(uint32_t b)
{
return b ^ (b >> 1);
}
uint32_t gray_decode(uint32_t g)
{
for (uint32_t bit = 1 << 31; bit > 1; bit >>= 1)
{
if (g & bit) g ^= bit >> 1;
}
return g;
}
void PointOutBMP(unsigned int x, unsigned int y, unsigned int width, unsigned int hight, byte & data[], unsigned int options = DRAW_OPT_NORMAL){
byte Bytes_Per_Line = (width + 7) / 8; // Get the minimum number of bytes used.
while(Bytes_Per_Line%4){ // While it isn't a multiple of 4 ...
Bytes_Per_Line ++; // ... keep adding to it
}
unsigned int Y_Offset = y * Bytes_Per_Line;
unsigned int X_Offset = x / 8;
unsigned int data_pointer = Y_Offset + X_Offset;
byte mask = 0x80 >> (x % 8);
switch (options){
case DRAW_OPT_NORMAL:
data[data_pointer] &= (~mask);
break;
case DRAW_OPT_CLEAR:
data[data_pointer] |= (0xFF&mask);
break;
}
}
void ClearScreenBMP(unsigned int size, byte & data[]){
ArrayInit(data, 0xFF, size);
}
task main(){
byte BMP[];
ClearScreenBMP(Size, BMP);
for(float i=0; i<10; i+=0.5){
byte gray = gray_encode(i);
for(int ii=0; ii<8; ii++){
if((gray >> ii) & 1){
PointOutBMP((ii*3+43)*sind(i*AdvSize) + 64, (ii*3+43)*cosd(i*AdvSize) + 64, Width, Hight, BMP);
}
}
}
string BMP_String = CreateMonochromeBMP(BMP, Width, Hight);
ArrayInit(BMP, 0, 1);
NumOut(0, LCD_LINE1, strlen(BMP_String));
PlayTone(3500,100);
Wait(100);
until(ButtonPressed(BTNCENTER, false));
until(!ButtonPressed(BTNCENTER, false));
WriteFileSingle("test image.bmp", BMP_String);
PlayTone(5000,100);
Wait(100);
until(ButtonPressed(BTNCENTER, false));
until(!ButtonPressed(BTNCENTER, false));
}
and "File BMP Mono lib.nxc":
Code: Select all
string CreateBMP_Header(unsigned long TotalSize, unsigned long OffsetToBitmap){
byte data[14] = {
0x42, 0x4D,
0x00, 0x00, 0x00, 0x00, // will get replaced
0x00, 0x00,
0x00, 0x00,
0x00, 0x00, 0x00, 0x00 // will get replaced
};
data[2] = TotalSize;
data[3] = TotalSize >> 8;
data[4] = TotalSize >> 16;
data[5] = TotalSize >> 24;
data[10] = OffsetToBitmap;
data[11] = OffsetToBitmap >> 8;
data[12] = OffsetToBitmap >> 16;
data[13] = OffsetToBitmap >> 24;
return FlattenVar(data);
}
string CreateDIB_Header(
unsigned long DIB_Len,
unsigned long Width,
unsigned long Hight,
unsigned int Color_Planes,
unsigned int Bits_Per_Pixel,
unsigned long Compression,
unsigned long PixelArrSize,
unsigned long HorzRes,
unsigned long VertRes
unsigned long Colors_In_Palette,
unsigned long Important_colors){
byte data[40];
data[0] = DIB_Len;
data[1] = DIB_Len >> 8;
data[2] = DIB_Len >> 16;
data[3] = DIB_Len >> 24;
data[4] = Width;
data[5] = Width >> 8;
data[6] = Width >> 16;
data[7] = Width >> 24;
data[8] = Hight;
data[9] = Hight >> 8;
data[10] = Hight >> 16;
data[11] = Hight >> 24;
data[12] = Color_Planes;
data[13] = Color_Planes >> 8;
data[14] = Bits_Per_Pixel;
data[15] = Bits_Per_Pixel >> 8;
data[16] = Compression;
data[17] = Compression >> 8;
data[18] = Compression >> 16;
data[19] = Compression >> 24;
data[20] = PixelArrSize;
data[21] = PixelArrSize >> 8;
data[22] = PixelArrSize >> 16;
data[23] = PixelArrSize >> 24;
data[24] = HorzRes;
data[25] = HorzRes >> 8;
data[26] = HorzRes >> 16;
data[27] = HorzRes >> 24;
data[28] = VertRes;
data[29] = VertRes >> 8;
data[30] = VertRes >> 16;
data[31] = VertRes >> 24;
data[32] = Colors_In_Palette;
data[33] = Colors_In_Palette >> 8;
data[34] = Colors_In_Palette >> 16;
data[35] = Colors_In_Palette >> 24;
data[36] = Important_colors;
data[37] = Important_colors >> 8;
data[38] = Important_colors >> 16;
data[39] = Important_colors >> 24;
string Sdata = FlattenVar(data);
return Sdata;
}
string CreateMonochromeColorTable(){
byte data[8] = {
0x00, 0x00, 0x00, 0x00,
0xFF, 0xFF, 0xFF, 0x00
};
return FlattenVar(data);
}
string CreateMonochromeBMP(byte bitmap_data[], unsigned long width, unsigned long hight){
unsigned long length = ArrayLen(bitmap_data) + 62;
string BuildingResult = "";
BuildingResult += CreateBMP_Header(length, 62);
BuildingResult += CreateDIB_Header(40, width, hight, 1, 1, 0, 0, 2835, 2835, 0, 0);
BuildingResult += CreateMonochromeColorTable();
BuildingResult += FlattenVar(bitmap_data);
Wait(10);
string Result = BuildingResult;
BuildingResult = "";
Wait(10);
return Result;
}
and "File.nxc":
Code: Select all
void WriteFileSingle(string file_name, string message)
{
DeleteFile(file_name);
byte file_handle;
unsigned long file_size = strlen(message);
CreateFile(file_name, file_size, file_handle);
unsigned long amount_written;
WriteString(file_handle, message, amount_written);
CloseFile(file_handle);
}
void ReadFileSingle(string file_name, string & message)
{
byte File_Handle;
int File_Size;
bool Success = false;
if(OpenFileRead(file_name, File_Size, File_Handle) == NO_ERR)
{
Success = ReadLnString(File_Handle, message);
}
CloseFile(File_Handle);
}
/*
task main(){
string Message;
for(int i = 0; i<500; i++){
Message = Message + NumToStr(i) + " ";
}
string name = "Test file.txt";
WriteFileSingle(name, Message);
}
It's supposed to create a BMP image file on the NXT file system. If it creates it successfully, you can see what it's supposed to look like (or look on FaceBook for a similar image I posted earlier).