Eeschema: minor s-expression schematic file format improvement.

Save image data using a base64 encoded string to reduce the file size.
This commit is contained in:
Wayne Stambaugh 2020-03-27 08:52:40 -04:00
parent 2f682b6c5f
commit 992d141292
2 changed files with 36 additions and 33 deletions

View File

@ -5,22 +5,18 @@
* *
* @author Wayne Stambaugh <stambaughw@gmail.com> * @author Wayne Stambaugh <stambaughw@gmail.com>
* *
* This program is free software; you can redistribute it and/or * This program is free software: you can redistribute it and/or modify it
* modify it under the terms of the GNU General Public License * under the terms of the GNU General Public License as published by the
* as published by the Free Software Foundation; either version 2 * Free Software Foundation, either version 3 of the License, or (at your
* of the License, or (at your option) any later version. * option) any later version.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful, but
* but WITHOUT ANY WARRANTY; without even the implied warranty of * WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* GNU General Public License for more details. * General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License along
* along with this program; if not, you may find one here: * with this program. If not, see <http://www.gnu.org/licenses/>.
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/ */
/** /**
@ -28,6 +24,10 @@
* @brief Schematic and symbol library s-expression file format parser implementations. * @brief Schematic and symbol library s-expression file format parser implementations.
*/ */
// For some reason wxWidgets is built with wxUSE_BASE64 unset so expose the wxWidgets
// base64 code.
#define wxUSE_BASE64 1
#include <wx/base64.h>
#include <wx/mstream.h> #include <wx/mstream.h>
#include <wx/tokenzr.h> #include <wx/tokenzr.h>
@ -2076,19 +2076,20 @@ SCH_BITMAP* SCH_SEXPR_PARSER::parseImage()
case T_data: case T_data:
{ {
wxMemoryOutputStream stream;
token = NextTok(); token = NextTok();
wxString data;
while( token != T_RIGHT ) while( token != T_RIGHT )
{ {
if( !IsSymbol( token ) || CurStr().find_first_of( "0x" ) != 0 ) if( !IsSymbol( token ) )
Expecting( "hex image data" ); Expecting( "base64 image data" );
stream.PutC( static_cast<char>( strtol( CurText() + 2, NULL, 16 ) & 0xFF ) ); data += FromUTF8();
token = NextTok();
} }
wxMemoryBuffer buffer = wxBase64Decode( data );
wxMemoryOutputStream stream( buffer.GetData(), buffer.GetBufSize() );
wxImage* image = new wxImage(); wxImage* image = new wxImage();
wxMemoryInputStream istream( stream ); wxMemoryInputStream istream( stream );
image->LoadFile( istream, wxBITMAP_TYPE_PNG ); image->LoadFile( istream, wxBITMAP_TYPE_PNG );

View File

@ -23,6 +23,10 @@
#include <boost/algorithm/string/join.hpp> #include <boost/algorithm/string/join.hpp>
#include <cctype> #include <cctype>
// For some reason wxWidgets is built with wxUSE_BASE64 unset so expose the wxWidgets
// base64 code.
#define wxUSE_BASE64 1
#include <wx/base64.h>
#include <wx/mstream.h> #include <wx/mstream.h>
#include <wx/filename.h> #include <wx/filename.h>
#include <wx/tokenzr.h> #include <wx/tokenzr.h>
@ -918,21 +922,19 @@ void SCH_SEXPR_PLUGIN::saveBitmap( SCH_BITMAP* aBitmap, int aNestLevel )
// Write binary data in hexadecimal form (ASCII) // Write binary data in hexadecimal form (ASCII)
wxStreamBuffer* buffer = stream.GetOutputStreamBuffer(); wxStreamBuffer* buffer = stream.GetOutputStreamBuffer();
char* begin = (char*) buffer->GetBufferStart(); wxString out = wxBase64Encode( buffer->GetBufferStart(), buffer->GetBufferSize() );
for( int ii = 0; begin < buffer->GetBufferEnd(); begin++, ii++ ) // Apparently the MIME standard character width for base64 encoding is 76 (unconfirmed)
// so use it in a vein attempt to be standard like.
#define MIME_BASE64_LENGTH 76
size_t first = 0;
while( first < out.Length() )
{ {
if( ii % 16 ) m_out->Print( 0, "\n" );
{ m_out->Print( aNestLevel + 2, "%s", TO_UTF8( out( first, MIME_BASE64_LENGTH ) ) );
first += MIME_BASE64_LENGTH;
m_out->Print( 0, " 0x%2.2X", *begin & 0xFF );
}
else
{
ii = 0;
m_out->Print( 0, "\n" );
m_out->Print( aNestLevel + 2, "0x%2.2X", *begin & 0xFF );
}
} }
m_out->Print( 0, "\n" ); m_out->Print( 0, "\n" );