Skip to content

Commit

Permalink
input: corrected two bugs in top input engine
Browse files Browse the repository at this point in the history
input: finalised the dhw input engine
output: corrected a delete[] mismatch bug
  • Loading branch information
maleadt committed Jan 18, 2009
1 parent d5021d2 commit e636aa0
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 6 deletions.
Binary file modified doc/file formats/ACECAD digital page file (DHW).odt
Binary file not shown.
108 changes: 104 additions & 4 deletions src/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,10 @@ void Input::data_input_top(std::ifstream& stream)

// Read untill at end of file
bool end_of_stroke = false;
stream.read(buffer, 6);
while (!stream.eof())
{
// Initialise and read end coördinates
stream.read(buffer, 6); //TODO fails at end of file, might need to read first bits to indicate if EOF? Check file!
buffer[6] = 0; // Fix nullpointer at end of string
int x2 = dbytes_to_value(buffer[4], buffer[3]);
int y2 = 12000 - dbytes_to_value(buffer[2], buffer[1]);

Expand Down Expand Up @@ -191,6 +190,9 @@ void Input::data_input_top(std::ifstream& stream)
{
end_of_stroke = true;
}

// Read new bytes
stream.read(buffer, 6);
}

// Clear the buffer
Expand All @@ -214,7 +216,7 @@ void Input::data_input_dhw(std::ifstream& stream)
delete[] buffer;

// Version
char version = 0x01;
char version = 1;
buffer = new char [1];
stream.read(buffer, 1);
if (strncmp(buffer, &version, 1) != 0)
Expand All @@ -225,12 +227,15 @@ void Input::data_input_dhw(std::ifstream& stream)
delete[] buffer;

// Image size
buffer = new char [2];
buffer = new char [4];
stream.read(buffer, 4);
data->imgSizeX = dbytes_to_value(buffer[1], buffer[0]);
data->imgSizeY = dbytes_to_value(buffer[3], buffer[2]);
delete[] buffer;

// Background
data->imgBackground = WHITE;

// Page type
// TODO: preserve field in data structure
buffer = new char [1];
Expand Down Expand Up @@ -284,4 +289,99 @@ void Input::data_input_dhw(std::ifstream& stream)
return;
}
delete[] buffer;

// Process the file
vector<double> points;
buffer = new char[1];
stream.read(buffer, 1);
while (!stream.eof())
{
// Process the byte
int tag = byte_to_value(buffer[0]);

// Layer
if (tag == 0x90)
{
stream.read(buffer, 1);
int data = byte_to_value(buffer[0]);
//std::cout << "Layer number: " << data << std::endl;
}

// Timestamp
else if (tag == 0x88)
{
stream.read(buffer, 1);
int data = byte_to_value(buffer[0]);
//std::cout << "Timestamp: " << data << std::endl;
}

// Pen state
else if (tag >= 128 && tag <= 135) // Pattern 10000XXX
{
// Pen up or down?
if (tag%2 == 0)
{
// Pen up
// Cannot save right now, still 1 point to follow
} else {
// Pen down, save previous points
if (!points.empty())
{
data->addPolyline(points);
points.clear();
}
}

// Extract colour
int colour = (tag >> 1)-64;
switch (colour)
{
case 0:
data->penForeground = BLACK;
break;
case 1:
data->penForeground = RED;
break;
case 2:
data->penForeground = BLUE;
break;
case 3:
data->penForeground = GREEN;
break;
default:
throw std::string("input-dhw: unknown pen colour");
break;
}
}

// Point
if (tag < 128) // Pattern 0XXXXXXX
{
// Read raw coördinates
int x1 = byte_to_value(buffer[0]);
stream.read(buffer, 1);
int x2 = byte_to_value(buffer[0]);
stream.read(buffer, 1);
int y1 = byte_to_value(buffer[0]);
stream.read(buffer, 1);
int y2 = byte_to_value(buffer[0]);

// Shift to actual coördinates
double x = x1 | x2<<7;
double y = data->imgSizeY - (y1 | y2<<7);

// Push them up the temporary queue
points.push_back(x);
points.push_back(y);
}

// Read a new byte
stream.read(buffer, 1);
}

// Push last series of points
data->addPolyline(points);

// Remove buffer
delete[] buffer;
}
4 changes: 4 additions & 0 deletions src/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,14 @@
// Containers
#ifdef WITH_USTD
#include "ustd.h"
#include <queue>
using ustd::vector;
using std::queue;
#else
#include <vector>
#include <queue>
using std::vector;
using std::queue;
#endif

//////////////////////
Expand Down
4 changes: 2 additions & 2 deletions src/output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ void Output::write(wxDC& dc) const
dc.DrawBitmap(m_bitmap, 0, 0, true);

// Cleanup
delete dataWx, dataCairo;
delete[] dataWx, dataCairo;
cairo_destroy(cr);
cairo_surface_destroy(surface);
}
Expand Down Expand Up @@ -353,7 +353,7 @@ void Output::data_output_cairo(cairo_t* cr, float scale) const

// Unsupported type
default:
throw std::string("unsupported element during dc output");
throw std::string("unsupported element during cairo output");
}
++tempIterator;
}
Expand Down

0 comments on commit e636aa0

Please sign in to comment.