Skip to content

Commit

Permalink
Added possibility to skip painting of certain things
Browse files Browse the repository at this point in the history
  • Loading branch information
schoeggu committed Feb 25, 2011
1 parent 1a22fe5 commit 8dcc955
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 82 deletions.
2 changes: 1 addition & 1 deletion include/painter.h
Expand Up @@ -15,7 +15,7 @@ void drawFormatInformation(const PaintContext* pc, int version, int formatInfo);
void drawVersionInformation(const PaintContext* pc, int version, int versionInfo);
void drawData(const PaintContext* pc, const SymbolInfo* si);

void drawBit(const PaintContext* pc, int version, const byte* data, int dataLen, int x, int y, int mask);
void drawBit(const PaintContext* pc, const SymbolInfo* si, int x, int y);
int applyMask(int pixel, int x, int y, int mask);

void drawRaster(const PaintContext* pc, int version);
Expand Down
40 changes: 19 additions & 21 deletions src/pyqrgen/pyqrgen.py
Expand Up @@ -7,9 +7,6 @@

class Screen(gtk.DrawingArea):


surfaceSize = 3000

__gsignals__ = { "expose-event": "override" }

def do_expose_event(self, event):
Expand All @@ -33,6 +30,7 @@ def redraw(self):

def __init__(self):
gtk.DrawingArea.__init__(self)
self.surfaceSize = 3000
self.surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, self.surfaceSize, self.surfaceSize)
self.ctx = cairo.Context(self.surface)
pyqrgen.setCairoContext(self.ctx)
Expand All @@ -44,9 +42,11 @@ def activated(self, expander):
self.w.hide()
else:
self.w.show_all()

def getFrame(self):
return self.frame

def set_expanded(self, exp):
self.expanderlabel.set_expanded(exp)
if exp == True:
self.w.show_all()

def __init__(self, widget, label):
gtk.Frame.__init__(self)
Expand Down Expand Up @@ -75,7 +75,6 @@ def regen_qr(self):
pyqrgen.encode()
self.redraw_qr()


def pcchanged(self, widget):
if widget == self.maskspinner:
pyqrgen.setMask(int(self.maskspinner.props.value)-1)
Expand Down Expand Up @@ -140,8 +139,6 @@ def sichanged(self, widget):

self.regen_qr()



def output(self, widget):
if widget == self.verspinner and self.verspinner.props.value == 0:
self.verspinner.props.text = "Auto"
Expand Down Expand Up @@ -238,7 +235,6 @@ def getDebugPane(self):
frame = ExpanderFrame(detailstable, "Debug Settings")

return frame


def getOptionPane(self):
detailstable = gtk.Table(3, 2, False)
Expand Down Expand Up @@ -292,9 +288,11 @@ def __init__(self):
self.window.set_title("qrgen")
self.window.connect("delete_event", gtk.main_quit)

self.mainbox = gtk.VBox(False, 0)
self.mainbox = gtk.HBox(False, 0)
self.optbox = gtk.VBox(False, 0)

self.options = self.getOptionPane()
self.options.set_expanded(True)
self.advanced = self.getAdvancedPane()
self.debug = self.getDebugPane()

Expand All @@ -311,27 +309,27 @@ def __init__(self):
self.screen = Screen()
self.aframe.add(self.screen)

self.mainbox.pack_start(self.genbox, False, False, 0)
self.mainbox.pack_start(self.options, False, False, 0)
self.mainbox.pack_start(self.advanced, False, False, 0)
self.mainbox.pack_start(self.debug, False, False, 0)
self.optbox.pack_start(self.genbox, False, False, 0)
self.optbox.pack_start(self.options, False, False, 0)
self.optbox.pack_start(self.advanced, False, False, 0)
self.optbox.pack_start(self.debug, False, False, 0)

self.mainbox.pack_start(self.optbox, False, False, 0)
self.mainbox.pack_start(self.aframe, True, True, 0)

self.window.add(self.mainbox)

self.window.set_default_size(215, 260)
self.window.set_default_size(390, 150)

self.genbox.show_all()
self.aframe.show_all()
self.optbox.show()
self.mainbox.show()
self.window.show()

def run(Widget):
widget = Widget()
gtk.main()

if __name__ == "__main__":
run(Win)
widget = Win()
gtk.main()



145 changes: 85 additions & 60 deletions src/qrgen/painter.c
Expand Up @@ -8,6 +8,7 @@
#include <string.h>

inline int getNumPixels(int version) { return 21 + 4 * (version - 1); }
inline bool skip(const PaintContext* pc, int zone) { printf("skipzone[%x], zone[%x], skip[%d]\n", pc->skipZone, zone, (pc->skipZone & zone)); return pc->debug && (pc->skipZone & zone); }

int idx = 0;
int bitNum = 7;
Expand Down Expand Up @@ -64,7 +65,8 @@ bool paint_qrcode(const SymbolInfo* si, PaintContext* pc)
}

cairo_translate(pc->cr, pc->x, pc->y);
cairo_scale(pc->cr, (float)size/(numPixels + quietZoneSize * 2), (float)size/(numPixels + quietZoneSize * 2));
double sc = ((double)size)/((double)numPixels + quietZoneSize * 2);
cairo_scale(pc->cr, sc, sc);


/* draw a background */
Expand Down Expand Up @@ -155,14 +157,18 @@ void drawFinderPattern(const PaintContext* pc, int x, int y)
cairo_save(cr);

cairo_translate(cr, x, y);

if (!skip(pc, QR_FINDER_PATTERN)) {

set_color(cr, pc->foreground);
cairo_rectangle(cr, 2, 2, 3, 3);
cairo_fill(cr);
set_color(cr, pc->foreground);
cairo_rectangle(cr, 2, 2, 3, 3);
cairo_fill(cr);

cairo_set_line_width(cr, 1);
cairo_rectangle(cr, 0.5, 0.5, 6, 6);
cairo_stroke(cr);
cairo_set_line_width(cr, 1);
cairo_rectangle(cr, 0.5, 0.5, 6, 6);
cairo_stroke(cr);

}

cairo_restore(cr);

Expand All @@ -179,13 +185,17 @@ void drawTimingPattern(const PaintContext* pc, int version)
cairo_save(cr);

set_color(cr, pc->foreground);

printf("call skip for timing pattern: [%x]\n", QR_TIMING_PATTERN);
if (!skip(pc, QR_TIMING_PATTERN)) {

for (row = 6, collumn = 8; collumn < numPixels - 8; collumn+= 2) {
cairo_rectangle(cr, row, collumn, 1, 1);
cairo_rectangle(cr, collumn, row, 1, 1);
}
for (row = 6, collumn = 8; collumn < numPixels - 8; collumn+= 2) {
cairo_rectangle(cr, row, collumn, 1, 1);
cairo_rectangle(cr, collumn, row, 1, 1);
}
cairo_fill(cr);

}

cairo_restore(cr);
}

Expand All @@ -196,16 +206,20 @@ void drawAlignmentPattern(const PaintContext* pc, int x, int y)
cairo_save(cr);

cairo_translate(cr, x-2, y-2);

if (!skip(pc, QR_ALIGNEMENT_PATTERN)) {

set_color(cr, pc->foreground);
cairo_rectangle(cr, 0.0, 0.0, 5, 5);
cairo_fill(cr);
set_color(cr, pc->background);
cairo_rectangle(cr, 1.0, 1.0, 3, 3);
cairo_fill(cr);
set_color(cr, pc->foreground);
cairo_rectangle(cr, 2, 2, 1, 1);
cairo_fill(cr);
set_color(cr, pc->foreground);
cairo_rectangle(cr, 0.0, 0.0, 5, 5);
cairo_fill(cr);
set_color(cr, pc->background);
cairo_rectangle(cr, 1.0, 1.0, 3, 3);
cairo_fill(cr);
set_color(cr, pc->foreground);
cairo_rectangle(cr, 2, 2, 1, 1);
cairo_fill(cr);

}

cairo_restore(cr);
}
Expand Down Expand Up @@ -244,28 +258,31 @@ void drawFormatInformation(const PaintContext* pc, int version, int formatInfo)

int numPixels = getNumPixels(version);

set_color(cr, pc->foreground);

int x, y;
for (x = 8, y = 0; y < 8; y++) {
if (formatInfo & 1) {
cairo_rectangle(cr, x, (y < 6 ? y : y + 1), 1, 1);
cairo_rectangle(cr, numPixels - y - 1, x, 1, 1);
if (!skip(pc, QR_FORMAT_INFO)) {
set_color(cr, pc->foreground);

int x, y;
for (x = 8, y = 0; y < 8; y++) {
if (formatInfo & 1) {
cairo_rectangle(cr, x, (y < 6 ? y : y + 1), 1, 1);
cairo_rectangle(cr, numPixels - y - 1, x, 1, 1);
}
formatInfo >>= 1;
}
formatInfo >>= 1;
}
for (x = 6, y = 8; x >= 0; x--) {
if (formatInfo & 1) {
cairo_rectangle(cr, (x < 6 ? x : x + 1), y, 1, 1);
cairo_rectangle(cr, y, numPixels - x - 1, 1, 1);
for (x = 6, y = 8; x >= 0; x--) {
if (formatInfo & 1) {
cairo_rectangle(cr, (x < 6 ? x : x + 1), y, 1, 1);
cairo_rectangle(cr, y, numPixels - x - 1, 1, 1);
}
formatInfo >>= 1;
}
formatInfo >>= 1;
}

/* print Black Module above lower left Version Info */
cairo_rectangle(cr, 8, numPixels - 8, 1, 1);
/* print Black Module above lower left Version Info */
cairo_rectangle(cr, 8, numPixels - 8, 1, 1);

cairo_fill(cr);
cairo_fill(cr);
}

cairo_restore(cr);
}

Expand All @@ -278,22 +295,24 @@ void drawVersionInformation(const PaintContext* pc, int version, int versionInfo

int numPixels = getNumPixels(version);

set_color(cr, pc->foreground);

int x, y;
for (x = 0; x < 6; x++) {
for (y = numPixels - 11; y < numPixels - 8; y++) {
if (versionInfo & 1) {
cairo_rectangle(cr, x, y, 1, 1);
cairo_rectangle(cr, y, x, 1, 1);
}
versionInfo >>= 1;
if (!skip(pc, QR_VERSION_INFO)) {
set_color(cr, pc->foreground);

int x, y;
for (x = 0; x < 6; x++) {
for (y = numPixels - 11; y < numPixels - 8; y++) {
if (versionInfo & 1) {
cairo_rectangle(cr, x, y, 1, 1);
cairo_rectangle(cr, y, x, 1, 1);
}
versionInfo >>= 1;

}
}

cairo_fill(cr);
}

cairo_fill(cr);

cairo_restore(cr);
}

Expand All @@ -311,34 +330,40 @@ void drawData(const PaintContext* pc, const SymbolInfo* si)

//draw two columns upwards
for (y = numPixels-1; y >= 0; y--) {
drawBit(pc, si->version, si->encodedData, si->totalCodeWords, x, y, si->mask);
drawBit(pc, si->version, si->encodedData, si->totalCodeWords, x-1, y, si->mask);
drawBit(pc, si, x, y);
drawBit(pc, si, x-1, y);
}

x-=2;
if (x == 6) x-=1; //column 6 is the vertical timing pattern, skip this column

//draw two columns downwards
for (y = 0; y <= numPixels-1; y++) {
drawBit(pc, si->version, si->encodedData, si->totalCodeWords, x, y, si->mask);
drawBit(pc, si->version, si->encodedData, si->totalCodeWords, x-1, y, si->mask);
drawBit(pc, si, x, y);
drawBit(pc, si, x-1, y);
}
}
cairo_fill(cr);

cairo_restore(cr);
}

void drawBit(const PaintContext* pc, int version, const byte* data, int dataLen, int x, int y, int mask)
void drawBit(const PaintContext* pc, const SymbolInfo* si, int x, int y)
{

if (!intersectsPattern(x, y, version)) {
if (!intersectsPattern(x, y, si->version)) {
if (bitNum < 0) { bitNum = 7; idx++; }
int pixel = 0;
if (idx < dataLen && (!(pc->debug && pc->noData))) pixel = (data[idx] >> bitNum--);
if (applyMask(pixel, x, y, (pc->debug && pc->noMask) ? MASK_NONE : mask) & 1) {
cairo_rectangle(pc->cr, x, y, 1, 1);

if (idx < si->totalCodeWords && (!(pc->debug && pc->noData))) {
pixel = (si->encodedData[idx] >> bitNum--);
}

if (!skip(pc, ((idx < si->dataCodeWords) ? QR_DATA : ((idx < si->totalCodeWords) ? QR_EC : QR_REMAINDER)))) {
if (applyMask(pixel, x, y, (pc->debug && pc->noMask) ? MASK_NONE : si->mask) & 1) {
cairo_rectangle(pc->cr, x, y, 1, 1);
}
}
}
}

Expand Down

0 comments on commit 8dcc955

Please sign in to comment.