void gen_qr_code(const char* str) { uint8_t tempdata[33*33]; uint8_t w =EncodeData(3, 0, str, 32, &tempdata[0]); uint16_t size=((w*w)/8)+(((w*w)%8)?1:0); uint16_t n; uint16_t idx = 0; uint16_t bit_count = 0; for(n=0;n<size;n++) { int b=0; for(b=7;b>=0;b--) { if (((n+1)*8)-b>w*w){break;} if((tempdata[n] & (1 << b)) != 0) { code[idx] = 0x00; idx++; } else { code[idx] = 0xFF; idx++; } bit_count++; } } }
int main(int argc, char *argv[]) { if (argc<3) { printf("qrencode <input file> <output file> [QR Code level] [QR Code version]\n"); printf("level and version default to 3 and 0\n"); return 1; } int level=3; if(argc>3) { level = atoi(argv[3]); } int version=0; if(argc>4) { version = atoi(argv[4]); } // Read in the input data from file, terminate with 0. FILE *inputfile = fopen(argv[1],"r"); char inputdata[10000]; int n; for(n=0;(!feof(inputfile)) && (n < 10000);n++) { int c = getc(inputfile); inputdata[n] = c; inputdata[n+1]=0; } // **** This calls the library and encodes the data // *** length is taken from NULL termination, however can also be passed by parameter. BYTE QR_m_data[3917]; //max possible bits resolution 177*177/8+1 int QR_width=EncodeData(3,version,inputdata,0,QR_m_data); // Write the data to the output file FILE *f=fopen(argv[2],"w"); int size=((QR_width*QR_width)/8)+(((QR_width*QR_width)%8)?1:0); printf("\nwriting file %i bytes size \n",size); fwrite(QR_m_data,size,1,f); fclose(f); // This code dumps the QR code to the screen as ASCII. printf("QR Code width: %u\n",QR_width); int bit_count=0; for(n=0;n<size;n++) { int b=0; for(b=7;b>=0;b--) { if((bit_count%QR_width) == 0) printf("\n"); if((QR_m_data[n] & (1 << b)) != 0) { printf("1"); } else { printf("0"); } bit_count++; } } return 0; }
int main(int argc, char *argv[]) { if (argc<2) return 1; BYTE QR_m_data[3917]; //max possible bits resolution 177*177/8+1 FILE *f; BYTE *p; p=QR_m_data; int QR_width=EncodeData(3,0,argv[1],0,p); f=fopen("file.tmp","w"); int size=((QR_width*QR_width)/8)+(((QR_width*QR_width)%8)?1:0); printf("\nwriting file %i bytes size \n",size); fwrite(QR_m_data,size,1,f); fclose(f); return 0; }
//Функция помехоустойчивого кодирования радиосообщения //NO: Функция изменяет содержимое исходного сообщения, изменяя длину и кодируя все данные сообщения //кроме первых 2х байт заголовка //Кодер оставляет нетронутыми первые 2 байта (длина и адрес), делит пакет на 18-байтные кадры //и каждый кадр преобразует в 24.5 байтный. Результат снова укладывается в RadioPack //Для стандартного 86-байтного пакета (c 5-байтным заголовком) результат применения кодера - //125-байтный пакет (24.5*5+2+выравнивание 0.5) void EncodeRadioMsg(RadioMessage* RadioPack) { #ifdef DEBUG_USE_TL_LINES TL3_HIGH(); #endif uint8_t arEncodedRadioPack[RADIOPACK_MAX_SIZE]; memset(arEncodedRadioPack,0,RADIOPACK_MAX_SIZE); uint8_t sizeOfEncodedRadioPack; const uint8_t sizeNoFECData = 2; memcpy(arEncodedRadioPack,RadioPack->Data,sizeNoFECData); EncodeData(RadioPack->Data+sizeNoFECData, RadioPack->Size-sizeNoFECData, arEncodedRadioPack+sizeNoFECData, sizeOfEncodedRadioPack); #ifdef DEBUG_TEST_ENCDEC_RADIODATA_TRELLIS uint8_t arDecodedRadioPack[RADIOPACK_MAX_SIZE]; memset(arDecodedRadioPack,0,RADIOPACK_MAX_SIZE); uint8_t sizeOfDecodedRadioPack; int16_t numDetectedErrors = 0; DecodeData(arEncodedRadioPack+sizeNoFECData, sizeOfEncodedRadioPack, arDecodedRadioPack, sizeOfDecodedRadioPack, numDetectedErrors); #endif sizeOfEncodedRadioPack+=sizeNoFECData; #ifdef DEBUG_CC1120_VARIABLE_PACKET_LENGTH //Корректируем длину пакета, которая хранится в первом (незакодированном!) байте радиосообщения arEncodedRadioPack[0] = sizeOfEncodedRadioPack - 1; #endif RadioPack->setMsg(arEncodedRadioPack,sizeOfEncodedRadioPack); #ifdef DEBUG_USE_TL_LINES TL3_LOW(); //С помощью TL-линии определено время кодирования радиопакета //Оно составляет 2-3 мс без применения оптимизации #endif }
static void UpdateFrame(CP_INSTANCE *cpi){ double CorrectionFactor; /* Reset the DC predictors. */ cpi->pb.LastIntraDC = 0; cpi->pb.InvLastIntraDC = 0; cpi->pb.LastInterDC = 0; cpi->pb.InvLastInterDC = 0; /* Initialise bit packing mechanism. */ #ifndef LIBOGG2 oggpackB_reset(cpi->oggbuffer); #else oggpackB_writeinit(cpi->oggbuffer, cpi->oggbufferstate); #endif /* mark as video frame */ oggpackB_write(cpi->oggbuffer,0,1); /* Write out the frame header information including size. */ WriteFrameHeader(cpi); /* Copy back any extra frags that are to be updated by the codec as part of the background cleanup task */ CopyBackExtraFrags(cpi); /* Encode the data. */ EncodeData(cpi); /* Adjust drop frame trigger. */ if ( GetFrameType(&cpi->pb) != KEY_FRAME ) { /* Apply decay factor then add in the last frame size. */ cpi->DropFrameTriggerBytes = ((cpi->DropFrameTriggerBytes * (DF_CANDIDATE_WINDOW-1)) / DF_CANDIDATE_WINDOW) + oggpackB_bytes(cpi->oggbuffer); }else{ /* Increase cpi->DropFrameTriggerBytes a little. Just after a key frame may actually be a good time to drop a frame. */ cpi->DropFrameTriggerBytes = (cpi->DropFrameTriggerBytes * DF_CANDIDATE_WINDOW) / (DF_CANDIDATE_WINDOW-1); } /* Test for overshoot which may require a dropped frame next time around. If we are already in a drop frame condition but the previous frame was not dropped then the threshold for continuing to allow dropped frames is reduced. */ if ( cpi->DropFrameCandidate ) { if ( cpi->DropFrameTriggerBytes > (cpi->frame_target_rate * (DF_CANDIDATE_WINDOW+1)) ) cpi->DropFrameCandidate = 1; else cpi->DropFrameCandidate = 0; } else { if ( cpi->DropFrameTriggerBytes > (cpi->frame_target_rate * ((DF_CANDIDATE_WINDOW*2)-2)) ) cpi->DropFrameCandidate = 1; else cpi->DropFrameCandidate = 0; } /* Update the BpbCorrectionFactor variable according to whether or not we were close enough with our selection of DCT quantiser. */ if ( GetFrameType(&cpi->pb) != KEY_FRAME ) { /* Work out a size correction factor. */ CorrectionFactor = (double)oggpackB_bytes(cpi->oggbuffer) / (double)cpi->ThisFrameTargetBytes; if ( (CorrectionFactor > 1.05) && (cpi->pb.ThisFrameQualityValue < cpi->pb.QThreshTable[cpi->Configuration.ActiveMaxQ]) ) { CorrectionFactor = 1.0 + ((CorrectionFactor - 1.0)/2); if ( CorrectionFactor > 1.5 ) cpi->BpbCorrectionFactor *= 1.5; else cpi->BpbCorrectionFactor *= CorrectionFactor; /* Keep BpbCorrectionFactor within limits */ if ( cpi->BpbCorrectionFactor > MAX_BPB_FACTOR ) cpi->BpbCorrectionFactor = MAX_BPB_FACTOR; } else if ( (CorrectionFactor < 0.95) && (cpi->pb.ThisFrameQualityValue > VERY_BEST_Q) ){ CorrectionFactor = 1.0 - ((1.0 - CorrectionFactor)/2); if ( CorrectionFactor < 0.75 ) cpi->BpbCorrectionFactor *= 0.75; else cpi->BpbCorrectionFactor *= CorrectionFactor; /* Keep BpbCorrectionFactor within limits */ if ( cpi->BpbCorrectionFactor < MIN_BPB_FACTOR ) cpi->BpbCorrectionFactor = MIN_BPB_FACTOR; } } /* Adjust carry over and or key frame context. */ if ( GetFrameType(&cpi->pb) == KEY_FRAME ) { /* Adjust the key frame context unless the key frame was very small */ AdjustKeyFrameContext(cpi); } else { /* Update the frame carry over */ cpi->CarryOver += ((ogg_int32_t)cpi->frame_target_rate - (ogg_int32_t)oggpackB_bytes(cpi->oggbuffer)); } cpi->TotalByteCount += oggpackB_bytes(cpi->oggbuffer); }
void qr_layer_set_data(QRLayer* layer, char* data) { QRData* qr_data = (QRData*) layer_get_data(layer); qr_data->width = EncodeData(QR_LEVEL_L, 0, data, 0, qr_data->str); layer_mark_dirty(qr_layer_get_layer(layer)); }