#usage "Import Bitmap
\n" "Import a bmp bitmap as picture to Eagle layer(s).
" "import_*.bmp files must be in the directory of this ULP.
"
"Importiert eine bmp-Datei in Eagle-Layer.
"
"import_*.bmp Dateien muessen im selben Verzeichnis wie das ULP sein.
" "Usage: Run ULP, select colors, start script import
\n" "Bedienung: ULP starten, Farben waehlen, Script-Import starten
\n"
"Version 2: Verbesserung des Menue"
"");
dlgStretch(1); // stretch bottom
}
dlgStretch(0);
dlgSpacing(20);
dlgLabel(" select up to 2 colors ");
dlgStretch(0);
dlgHBoxLayout {
dlgStretch(0);
dlgPushButton("+OK") dlgAccept();
dlgStretch(0);
dlgPushButton("-ESC") dlgReject();
dlgStretch(1);
dlgPushButton("&set all") setall(1);
dlgStretch(0);
dlgPushButton("&clear all") setall(0);
dlgStretch(0);
}
dlgStretch(1);
};
if (Result == 0) exit (0);
}
// Select menu for 16 color BITmap
void Cselect16(void) {
Result = dlgDialog("Select used colors" + fileName) {
dlgStretch(0);
dlgHBoxLayout {
dlgStretch(0);
dlgSpacing(3);
for(int colum = 0 ; colum < 16; colum++) {
dlgCheckBox("", colorUsed[colum]);
}
dlgSpacing(6);
dlgStretch(1);
}
dlgHBoxLayout {
dlgSpacing(3);
dlgStretch(0);
dlgLabel("
");
dlgStretch(1); // stretch bottom
}
dlgStretch(0);
dlgSpacing(20);
dlgLabel(" select up to 16 colors ");
dlgStretch(0);
dlgHBoxLayout {
dlgStretch(0);
dlgPushButton("+OK") dlgAccept();
dlgStretch(0);
dlgPushButton("-ESC") dlgReject();
dlgStretch(1);
dlgPushButton("&set all") { setall(1); dlgRedisplay();}
dlgStretch(0);
dlgPushButton("&clear all") { setall(0); dlgRedisplay();}
dlgStretch(0);
}
dlgStretch(1);
};
if (Result == 0) exit (0);
}
// Select menu for 256 color BITmap
void Cselect256(void) {
// dlgMessageBox("please be patient\nif you have a slow computer!", "OK");
Result = dlgDialog("Select used colors " + fileName) {
dlgVBoxLayout {
dlgStretch(0);
// ********* 550 DO NOT CHANGE THIS VALUE *********
dlgHBoxLayout dlgSpacing(550);
// ********* 550 DO NOT CHANGE THIS VALUE *********
dlgStretch(0);
for(int row = 0 ; row < 8; row++) {
dlgHBoxLayout {
dlgVBoxLayout {
dlgStretch(0);
dlgHBoxLayout {
dlgStretch(0);
dlgSpacing(3);
for(int colum = 0 ; colum < 32; colum++) {
dlgCheckBox("", colorUsed[row * 32 + colum]);
}
dlgSpacing(6);
dlgStretch(1);
}
dlgHBoxLayout {
dlgSpacing(3);
dlgStretch(0);
dlgLabel(bmps[row]);
dlgStretch(1);
}
}
dlgStretch(1); // stretch right
}
dlgStretch(1); // stretch bottom
}
dlgStretch(1);
}
dlgStretch(0);
dlgVBoxLayout {
dlgStretch(0);
dlgSpacing(20);
string hc;
sprintf( hc, " select up to %d colors ", cselmax);
dlgLabel(hc);
dlgStretch(0);
dlgHBoxLayout {
dlgStretch(0);
dlgPushButton("+OK") dlgAccept();
dlgStretch(0);
dlgPushButton("-ESC") dlgReject();
dlgStretch(1);
dlgPushButton("&set all") setall(1);
dlgStretch(0);
dlgPushButton("&clear all") setall(0);
dlgStretch(0);
}
dlgStretch(1);
}
dlgStretch(1);
};
if (Result == 0) exit (0);
}
// select colors
int selectColors(void) {
switch (ColorBits) {
case 1 : Cselect2();
break;
case 4 : Cselect16();
break;
case 8 : Cselect256();
break;
}
for(int n = 0; n < cselmax; n++) {
colorSelect[n] = -1; // reset selected colors
}
int cs;
for(int s = 0; s < 256; s++) {
if(colorUsed[s]) {
colorSelect[cs] = s;
cs++;
}
}
return cs;
}
// select colors by scan array
void selectMenue() {
int cs;
do {
cs = selectColors();
string hx;
if (cs > cselmax) {
sprintf( hx, "do not use more than %d colors!", cselmax);
dlgMessageBox(hx, "Ok");
}
if (cs < 1) {
sprintf( hx, "no colors selected!");
dlgMessageBox(hx, "Ok");
}
} while (cs > cselmax || cs == 0);
return;
}
// header from Script, define Layer
void scriptheader(void) {
sprintf(st, "Grid %s;\n", Grid);
script += st;
sprintf(st, "Grid %.6f;\n", xScale);
script += st;
script += "Grid ON;\n";
for(int cs = 0; cs < cselmax; cs++) { // max 32 color extract
if(colorSelect[cs] > -1) {
if(Layer + cs > 99){ // user defined layer
sprintf(st, "LAYER %d %dbmp;\n", Layer + cs, Layer + cs);
script += st;
sprintf(st, "SET FILL_LAYER %d 10;\n", Layer);
script += st;
sprintf(st, "SET COLOR_LAYER %d %d;\n", Layer + cs, cs + 1);
script += st;
}
}
}
sprintf(st, "CHANGE LAYER %d;\n", Layer);
script += st;
script += "SET UNDO_LOG OFF;\n";
script += st;
}
// get flag for scan colors
int scan(void) {
return (dlgDialog("Used Colors?") {
dlgStretch(0);
dlgHBoxLayout {
dlgStretch(0);
dlgVBoxLayout {
dlgGroup("") {
string st = "";
sprintf(st, " :\nis a %.0f color bitmap\nist eine %.0f-Farben-Bitmap\n", pow(2, ColorBits), pow(2, ColorBits));
dlgLabel(fileName + st);
dlgLabel(bmpcolor[ColorBits]);
}
dlgGroup("") {
dlgStretch(1);
dlgHBoxLayout {
dlgStretch(0);
dlgPushButton("+scan used colors") dlgAccept();
dlgStretch(1);
dlgPushButton("-no scan") dlgReject();
dlgStretch(0);
}
dlgStretch(0);
}
dlgStretch(1);
}
dlgStretch(1);
}
dlgStretch(1);
} );
}
void colors24(void) {
string st = "bmp file contains more than 256 colors, reduce colors before use.\n\n"
+ "Die benutzte Anzahl der Farben in der bmp-Datei ist groesser 256.\n"
+ "Verringern Sie zuerst die Anzahl der Farben in der bmp-Datei.\n\n";
dlgMessageBox(st, "&OK");
}
void menuchange(void) {
menulbl = menuedlg[scaled];
switch (scaled) {
case 0 : ratiologo = "
";
break;
case 1 : ratiologo = "
";
break;
case 2 : ratiologo = "
";
break;
}
switch (unit) {
case 0 : menuinfo = menuval0[scaled];
break;
case 1 : menuinfo = menuval1[scaled];
break;
case 2 : menuinfo = menuval2[scaled];
break;
case 3 : menuinfo = menuval3[scaled];
break;
}
return ;
}
// ***************** main ****************
void main(void) {
char bkslash = '/';
int pos = strrchr(argv[0], bkslash);
if (pos >= 0) {
ulp_path = strsub(argv[0], 0, pos + 1);
}
ratiologo = "
";
bmpcolor[1] = "
";
bmpcolor[4] = "
";
bmpcolor[8] = "
";
bmps[0] = "
";
bmps[1] = "
";
bmps[2] = "
";
bmps[3] = "
",
bmps[4] = "
";
bmps[5] = "
";
bmps[6] = "
";
bmps[7] = "
";
fileName = dlgFileOpen("select a bmp file", "", "*.bmp");
if (fileName == "") exit (0);
nBytes = fileread(c, fileName); // read file in array
// up to 31 bytes - not all used
if(c[0] != 'B') {
dlgMessageBox(fileName + ":\nis not a bmp file.\n\nist keine bmp-Datei.", "OK");
exit(0);
}
if(c[1] != 'M') {
dlgMessageBox(fileName + ":\nis not a bmp file.\n\nist keine bmp-Datei.", "OK");
exit(0);
}
if(c[21] > 0) {
dlgMessageBox(fileName + ":\nnr of pixels in x axis is too big\n"
+ "\nAnzahl der Pixel in X zu gross\n", "OK");
exit (0);
}
if(c[25] > 0) {
dlgMessageBox(fileName + ":\nnr of pixels in x axis is too big\n"
+ "\nAnzahl der Pixel in Y zu gross\n", "OK");
exit (0);
} //
// case 6 TO 9, 14 TO 17 not used
ColorBits = c[28]; // counter of ColorBits
if(ColorBits > 8) {
colors24(); // to many colors, break
exit(0);
}
AdrEnd = c[2] + c[3] * 256 + c[4] * 256 * 256 + c[5] * 256 * 256 * 256;
AdrStart = c[10]+ c[11] * 256 + c[12] * 256 * 256 + c[13] * 256 * 256 * 256;
X = c[18] + c[19] * 256 + c[20] * 65536 + c[21] * 256 * 256 * 256;
Y = c[22] + c[23] * 256 + c[24] * 65536 + c[25] * 256 * 256 * 256;
sprintf(xy, " X = %5d Pixel\n Y = %5d Pixel", X, Y);
length = AdrEnd - AdrStart; // bitmap legth
Byte4Group = length / Y / 4;
if(scan()) { // first scan used colors
colorscan = 1;
GenScript();
}
selectMenue();
//---------------------------------
int d = 1;
while(d) {
dlgDialog("Info "+ fileName) {
dlgHBoxLayout {
dlgVBoxLayout {
dlgStretch(0);
dlgGroup("File data") {
dlgLabel(xy, 1);
}
dlgStretch(0);
dlgLabel(ratiologo, 1);
dlgStretch(1);
}
dlgStretch(0);
dlgVBoxLayout {
dlgStretch(0);
dlgGroup("Format") {
dlgRadioButton("&DPI", scaled) { unit = 0; menuchange(); }
dlgRadioButton("&Scaled", scaled) {menuchange(); }
dlgRadioButton("&Aspect/Ratio m ", scaled) { menuchange(); }
}
dlgStretch(0);
dlgGroup("Unit") {
dlgRadioButton("&Inch", unit) { menuchange(); dlgRedisplay();}
dlgRadioButton("Mi&l", unit) { if ( scaled == 0) scaled = 1; menuchange(); }
dlgRadioButton("&MM", unit) { if ( scaled == 0) scaled = 1; menuchange(); }
dlgRadioButton("Mi&cron", unit) { if ( scaled == 0) scaled = 1; menuchange(); }
}
dlgStretch(0);
dlgPushButton("s&elected colors") selectMenue();
dlgStretch(1);
}
}
dlgStretch(0);
dlgHBoxLayout {
dlgStretch(0);
dlgVBoxLayout {
dlgLabel(menulbl, 1);
dlgLabel(menuinfo, 1);
dlgStretch(0);
dlgHBoxLayout {
dlgStretch(0);
dlgRealEdit(xScale, vmin[unit], vmax[unit]);
dlgStretch(0);
dlgSpacing(100);
dlgStretch(0);
}
dlgStretch(0);
dlgSpacing(10);
dlgStretch(0);
dlgLabel("select start layer for &1st. selected color");
dlgStretch(0);
dlgHBoxLayout {
dlgStretch(0);
dlgSpinBox(Layer, 1, 255);
dlgStretch(0);
dlgSpacing(100);
dlgStretch(0);
}
dlgStretch(0);
}
dlgStretch(1);
}
dlgStretch(1);
dlgHBoxLayout {
dlgStretch(0);
dlgPushButton("+OK") {dlgAccept(); d = 0; }
dlgStretch(1);
dlgPushButton("-Cancel") exit(0);
}
};
}
//---------------------------------
colorscan = 0; // reset scanning mode
switch(scaled) {
case 0 : Grid = grid[0];
yScale = 1 / xScale; // Dots Per Inch
xScale = yScale;
break;
case 1 : Grid = Grid = grid[unit];
yScale = xScale;
break;
case 2 : Grid = Grid = grid[unit];
yScale = xScale / X; // Aspect Ratio = Width / Pixel X
xScale = yScale;
break;
}
scriptheader();
GenScript(); // generate script string
script += "SET UNDO_LOG ON;\n";
script += "WINDOW FIT;\n";
sprintf(st, "Change Size %.3f;\n", yScale * 2);
script += st;
script += "CHANGE FONT VECTOR;\n";
sprintf(st, "TEXT '" + fileName + "' (0 %.3f);\n", -5 * yScale );
script += st;
Result = dlgDialog("Script accept?") {
dlgTextEdit(script);
dlgHBoxLayout {
dlgStretch(0);
dlgPushButton("+Yes") dlgAccept();
dlgStretch(1);
dlgPushButton("-Cancel") dlgReject();
dlgStretch(0);
}
};
if (Result == 1) output(ulp_path + "bmp.scr", "wt") {
printf(script);
exit ("script " + ulp_path + "bmp.scr");
}
else exit (0);
}