Beispiel #1
0
RVNET_DATATYPE RVnetSlaveProcess(uint8 *px_buf, RVNET_DATATYPE pkSize,
    uint8 device_address)
  {
    volatile uint8 *pxPack = px_buf;
    if (*pxPack == device_address)
      {
        if (CheckCRC(pxPack, pkSize))
          {
            pxPack++;
            switch (*pxPack)
              {
            case 0x00:
              pxPack++;
              pkSize = ReadDeviceID(pxPack);
              break;
            case 0x01:
            case 0x02:
              pxPack++;
              pkSize = ReadNBits(pxPack);
              break;
            case 0x03:
            case 0x04:
              pxPack++;
              pkSize = ReadNWords(pxPack);
              break;
            case 0x05:
              pxPack++;
              pkSize = WriteBit(pxPack);
              break;
            case 0x10:
              pxPack++;
              pkSize = WriteNWords(pxPack);
              break;
            default:
              pxPack++;
              pkSize = ErrorAddress(pxPack);
              break;

              }
          }
        else
          return 0;
      }
    else
      return 0;
    pkSize += 2; // Add heder
    SetCRC(px_buf, pkSize);
    pkSize += 2; // Add CRC
    return pkSize;
  }
Beispiel #2
0
int main(int argc, char *argv[])

	{
	int n, p, row, col, nRows, nCols, imgPlanes, codingPlanes, fileSize,
	  targetBytes, deltaNum, deltaDen, maxCount, plane;
	double decodedFraction = 1;
	char progressIndicator = 'y', verbose = 'n';
	unsigned int outFile = 0;
	Image *recImg;
	FILE *fpInp;
	CModel *cModel;
	CTemplate *cTemplateIntra = InitTemplate(TEMPLATE_INTRA);
	CTemplate *cTemplateInter = InitTemplate(TEMPLATE_INTER);
	PModel *pModel;
	CPattern *cPattern;

	if(argc == 1)
		{
		fprintf(stderr, "Usage: %10s [ -f decodedFraction ]\n", argv[0]);
		//fprintf(stderr,"Usage: Micro3DDec [ -f decodedFraction ]\n");
		//fprintf(stderr,"                  [ -nopi (no progress indicator) ]\n");
		fprintf(stderr,"                  [ -v (verbose) ]\n");
		fprintf(stderr,"                  [ -o outputFile ]\n");
		fprintf(stderr,"                  codeFile\n");
		return EXIT_FAILURE;
		}

	for(n = 1 ; n < argc ; n++)
		if(!strcmp("-f", argv[n]))
			{
			decodedFraction = atof(argv[n+1]);
			break;
			}

	for(n = 1 ; n < argc ; n++)
		if(!strcmp("-nopi", argv[n]))
			{
			progressIndicator = 'n';
			break;
			}

	for(n = 1 ; n < argc ; n++)
		if(!strcmp("-v", argv[n]))
			{
			verbose = 'y';
			progressIndicator = 'n';
			break;
			}

	for(n = 1 ; n < argc ; n++)
		if(!strcmp("-o", argv[n]))
			{
			outFile = n+1;
			break;
			}

	if(!(fpInp = fopen(argv[argc - 1], "r")))
		{
		fprintf(stderr, "Error: couldn't open code file\n");
		return 1;
		}

	fseek(fpInp, 0, SEEK_END);
	fileSize = ftell(fpInp);
	rewind(fpInp);
	printf("Code file has %d bytes\n", fileSize);
	targetBytes = (int)(fileSize * decodedFraction + 0.5);
	if(fileSize != targetBytes)
		printf("Decoding the first %d bytes\n", targetBytes);

	startinputtingbits();
	start_decode(fpInp);

	/* Get the number of rows and cols */
	nRows = ReadNBits(STORAGE_BITS_N_ROWS, fpInp);
	nCols = ReadNBits(STORAGE_BITS_N_COLS, fpInp);

	/* Get the number of image bit-planes (1..16) */
	imgPlanes = ReadNBits(STORAGE_BITS_N_IMG_PLANES, fpInp) + 1;

	/* Get the number of encoded bit-planes (1..16) */
	codingPlanes = ReadNBits(STORAGE_BITS_N_COD_PLANES, fpInp) + 1;

	deltaNum = ReadNBits(STORAGE_BITS_PMODEL_DELTA_NUM, fpInp) + 1;
	deltaDen = ReadNBits(STORAGE_BITS_PMODEL_DELTA_DEN, fpInp) + 1;
	maxCount = ReadNBits(STORAGE_BITS_PMODEL_MAX_COUNT, fpInp);
	if(maxCount == 0 || maxCount > DEFAULT_PMODEL_MAX_COUNT)
		fprintf(stderr, "Warning (maxCount): counters may overflow\n");

	printf("Image has %d rows, %d cols and %d bit-planes\n", nRows,
	  nCols, imgPlanes);
	printf("Decoding the %d most significant bit-planes)\n", codingPlanes);
	printf("Delta: %d/%d, MaxCount: %d\n", deltaNum, deltaDen, maxCount);

	if(!(recImg = CreateImage(nRows, nCols, imgPlanes <= 8 ?
	  DATA_TYPE_C : DATA_TYPE_S, 1)))
		return EXIT_FAILURE;

	if(verbose == 'y')
		{
		printf("Using intra template:\n\n");
		ShowTemplate(cTemplateIntra);
		putchar('\n');
		printf("Using inter template:\n\n");
		ShowTemplate(cTemplateInter);
		putchar('\n');
		}

	cModel = CreateCModel(TEMPLATE_MAX_SIZE, N_SYMBOLS, N_CTX_SYMBOLS,
	  deltaNum, deltaDen, maxCount);
	pModel = CreatePModel(N_SYMBOLS);
	cPattern = CreateCPattern(imgPlanes, MAX(cTemplateIntra->size,
	  cTemplateInter->size));

	for(plane = imgPlanes - 1 ; plane >= imgPlanes - codingPlanes ; plane--)
		{
		/* Get the context pattern for this bit-plane */
		for(p = plane ; p < imgPlanes ; p++)
			{
			cPattern->size[p] = 0;
			for(n = 0 ; n < (p == plane ? cTemplateIntra->size :
			  cTemplateInter->size) ; n++)
				if((cPattern->pattern[p][n] = ReadNBits(1, fpInp)))
					cPattern->size[p]++;

			}

		if(verbose == 'y')
			{
			fprintf(stderr, "Plane %2d: ", plane);
			for(n = plane ; n < imgPlanes ; n++)
				fprintf(stderr, "%d ", cPattern->size[n]);

			fprintf(stderr, "\n");
			}

		DecodeBitPlane(recImg, imgPlanes, plane, cModel, cTemplateIntra,
          cTemplateInter, pModel, cPattern, fpInp, targetBytes);

		if(_bytes_input > targetBytes)
			break;

		if(progressIndicator == 'y')
			fprintf(stderr, "Plane %2d decoded\n", plane);

		}

	for(plane = imgPlanes - codingPlanes - 1 ; plane >= 0 ; plane--)
		for(row = 0 ; row < recImg->nRows ; row++)
			for(col = 0 ; col < recImg->nCols ; col++)
				if(_bytes_input <= targetBytes)
					PutGrayPixel(recImg, row, col, GetGrayPixel(recImg,
					  row, col) | (ReadNBits(1, fpInp) << plane));

	//printf("Decoded %llu bytes\n", _bytes_input);
	printf("Decoded %"PRIu64" bytes\n", (uint64_t)_bytes_input);
	finish_decode();
	doneinputtingbits();

	fclose(fpInp);

	if ((outFile > 0) && (outFile < argc))
		WriteImageFile(argv[outFile], recImg);
	else
		WriteImageFile("!decMicroImg09.pgm", recImg);

	putchar('\n');

	return EXIT_SUCCESS;
	}