Example #1
0
bool verifyne::QR::writeANSI(QRcode *qrcode, std::ostream& out)
{
        unsigned char *row, *p;
        int x, y;
        int realwidth;
        int last;

        char *white, *black, *buffer;
        int white_s, black_s, buffer_s;

        if( image_type == ANSI256_TYPE ){
                /* codes for 256 color compatible terminals */
                white = (char *)"\033[48;5;231m";
                white_s = 11;
                black = (char *)"\033[48;5;16m";
                black_s = 10;
        } else {
                white = (char *)"\033[47m";
                white_s = 5;
                black = (char *)"\033[40m";
                black_s = 5;
        }

        int size = 1;

        realwidth = (qrcode->width + margin * 2) * size;
        buffer_s = ( realwidth * white_s ) * 2;
        buffer = (char *)malloc( buffer_s );
        if(buffer == NULL) {
                std::cerr << "Failed to allocate memory" << std::endl;
                return false;
        }

        /* top margin */
        writeANSI_margin(realwidth, buffer, buffer_s, white, white_s, out);

        /* data */
        p = qrcode->data;
        for(y=0; y<qrcode->width; y++) {
                row = (p+(y*qrcode->width));

                bzero( buffer, buffer_s );
                strncpy( buffer, white, white_s );
                for(x=0; x<margin; x++ ){
                        strncat( buffer, "  ", 2 );
                }
                last = 0;

                for(x=0; x<qrcode->width; x++) {
                        if(*(row+x)&0x1) {
                                if( last != 1 ){
                                        strncat( buffer, black, black_s );
                                        last = 1;
                                }
                        } else {
                                if( last != 0 ){
                                        strncat( buffer, white, white_s );
                                        last = 0;
                                }
                        }
                        strncat( buffer, "  ", 2 );
                }

                if( last != 0 ){
                        strncat( buffer, white, white_s );
                }
                for(x=0; x<margin; x++ ){
                        strncat( buffer, "  ", 2 );
                }
                strncat( buffer, "\033[0m\n", 5 );
                out << buffer;
        }

        /* bottom margin */
        writeANSI_margin(realwidth, buffer, buffer_s, white, white_s, out);

        free(buffer);

        return true;
}
Example #2
0
static int writeANSI(const QRcode *qrcode, const char *outfile)
{
	FILE *fp;
	unsigned char *row, *p;
	int x, y;
	int realwidth;
	int last;

	const char *white, *black;
	char *buffer;
	int white_s, black_s, buffer_s;

	if(image_type == ANSI256_TYPE){
		/* codes for 256 color compatible terminals */
		white = "\033[48;5;231m";
		white_s = 11;
		black = "\033[48;5;16m";
		black_s = 10;
	} else {
		white = "\033[47m";
		white_s = 5;
		black = "\033[40m";
		black_s = 5;
	}

	size = 1;

	fp = openFile(outfile);

	realwidth = (qrcode->width + margin * 2) * size;
	buffer_s = (realwidth * white_s) * 2;
	buffer = (char *)malloc(buffer_s);
	if(buffer == NULL) {
		fprintf(stderr, "Failed to allocate memory.\n");
		exit(EXIT_FAILURE);
	}

	/* top margin */
	writeANSI_margin(fp, realwidth, buffer, white, white_s);

	/* data */
	p = qrcode->data;
	for(y = 0; y < qrcode->width; y++) {
		row = (p+(y*qrcode->width));

		memset(buffer, 0, buffer_s);
		strncpy(buffer, white, white_s);
		for(x = 0; x < margin; x++ ){
			strncat(buffer, "  ", 2);
		}
		last = 0;

		for(x = 0; x < qrcode->width; x++) {
			if(*(row+x)&0x1) {
				if( last != 1 ){
					strncat(buffer, black, black_s);
					last = 1;
				}
			} else if( last != 0 ){
				strncat(buffer, white, white_s);
				last = 0;
			}
			strncat(buffer, "  ", 2);
		}

		if( last != 0 ){
			strncat(buffer, white, white_s);
		}
		for(x = 0; x < margin; x++ ){
			strncat(buffer, "  ", 2);
		}
		strncat(buffer, "\033[0m\n", 5);
		fputs(buffer, fp);
	}

	/* bottom margin */
	writeANSI_margin(fp, realwidth, buffer, white, white_s);

	fclose(fp);
	free(buffer);

	return 0;
}
Example #3
0
File: qrimage.c Project: wxxweb/w2x
static QRimageResultType writeANSI(QRcode *qrcode, const char *outfile, int ansi256)
{
	FILE *fp;
	unsigned char *row, *p;
	int x, y;
	int realwidth;
	int last;

	char *white, *black, *buffer;
	int white_s, black_s, buffer_s;

	if( ansi256 ){
		/* codes for 256 color compatible terminals */
		white = "\033[48;5;231m";
		white_s = 11;
		black = "\033[48;5;16m";
		black_s = 10;
	} else {
		white = "\033[47m";
		white_s = 5;
		black = "\033[40m";
		black_s = 5;
	}

	fp = fopen(outfile, "wb");
	if(fp == NULL) {
		return QR_IMG_FAILED_CREATE_FILE;
	}

	realwidth = (qrcode->width + margin * 2);
	buffer_s = ( realwidth * white_s ) * 2;
	buffer = (char *)malloc( buffer_s );
	if(buffer == NULL) {
		return QR_IMG_FAILED_ALLOCATE_MEMORY;
	}

	/* top margin */
	writeANSI_margin(fp, realwidth, buffer, buffer_s, white, white_s);

	/* data */
	p = qrcode->data;
	for(y=0; y<qrcode->width; y++) {
		row = (p+(y*qrcode->width));

		memset( buffer, 0, buffer_s );
		strncpy_s( buffer, buffer_s, white, white_s );
		for(x=0; x<margin; x++ ){
			strncat_s( buffer, buffer_s, "  ", 2 );
		}
		last = 0;

		for(x=0; x<qrcode->width; x++) {
			if(*(row+x)&0x1) {
				if( last != 1 ){
					strncat_s( buffer, buffer_s, black, black_s );
					last = 1;
				}
			} else {
				if( last != 0 ){
					strncat_s( buffer, buffer_s, white, white_s );
					last = 0;
				}
			}
			strncat_s( buffer, buffer_s, "  ", 2 );
		}

		if( last != 0 ){
			strncat_s( buffer, buffer_s, white, white_s );
		}
		for(x=0; x<margin; x++ ){
			strncat_s( buffer, buffer_s, "  ", 2 );
		}
		strncat_s( buffer, buffer_s, "\033[0m\n", 5 );
		fputs( buffer, fp );
	}

	/* bottom margin */
	writeANSI_margin(fp, realwidth, buffer, buffer_s, white, white_s);

	fclose(fp);
	free(buffer);

	return QR_IMG_SUCCESS;
}