コード例 #1
0
ファイル: gefont_ttf.c プロジェクト: drewet/libge
static void fontPrintTextImpl(FT_Bitmap* bitmap, int xofs, int yofs, u32 text_color, u32* framebuffer, int width, int height, int lineSize){
	int x, y;
	
	u8* line = bitmap->buffer;
	u32* fbLine = framebuffer + xofs + yofs * lineSize;
	for (y = 0; y < bitmap->rows; y++) {
		u8* column = line;
		u32* fbColumn = fbLine;
		for (x = 0; x < bitmap->width; x++) {
			if (x + xofs < width && x + xofs >= 0 && y + yofs < height && y + yofs >= 0) {
				u8 val = *column;
				u32 color = *fbColumn;

				float f_val = ((float)val) / 255.0;
				f_val *= Af(text_color);

				float r = min(1.0, Rf(text_color) * f_val + Rf(color) * (1.0 - f_val));
				float g = min(1.0, Gf(text_color) * f_val + Gf(color) * (1.0 - f_val));
				float b = min(1.0, Bf(text_color) * f_val + Bf(color) * (1.0 - f_val));
				float a = min(1.0, Af(color) + f_val);

				*fbColumn = RGBAf(r, g, b, a);
			}
			column++;
			fbColumn++;
		}
		line += bitmap->pitch;
		fbLine += lineSize;
	}

//	gePrintDebug(0x100, "fontPrintTextImpl(bitmap, %d, %d, 0x%8.8X, framebuffer, %d, %d, %d)\n", xofs, yofs, color, width, height, lineSize);
	/*
	u8* line = bitmap->buffer;
	u32* fbLine = framebuffer + xofs + yofs * lineSize;
	for (y = 0; y < bitmap->rows; y++) {
		u8* column = line;
		u32* fbColumn = fbLine;
		for (x = 0; x < bitmap->width; x++) {
			if (x + xofs < width && x + xofs >= 0 && y + yofs < height && y + yofs >= 0) {
				u8 val = *column;
				color = *fbColumn;
				u8 r = color & 0xff;
				u8 g = (color >> 8) & 0xff;
				u8 b = (color >> 16) & 0xff;
				u8 a = (color >> 24) & 0xff;
				r = rf * val / 255 + (255 - val) * r / 255;
				g = gf * val / 255 + (255 - val) * g / 255;
				b = bf * val / 255 + (255 - val) * b / 255;
				a = af * val / 255 + (255 - val) * a / 255;
				*fbColumn = RGBA(r, g, b, a);
			//	*fbColumn = RGBA(255, 255, 255, val);
			}
			column++;
			fbColumn++;
		}
		line += bitmap->pitch;
		fbLine += lineSize;
	}
	*/
}
コード例 #2
0
void omxComputeNumericDeriv::computeImpl(FitContext *fc)
{
	if (fc->fitUnits == FIT_UNITS_SQUARED_RESIDUAL ||
	    fc->fitUnits == FIT_UNITS_SQUARED_RESIDUAL_CHISQ) {  // refactor TODO
		numParams = 0;
		if (verbose >= 1) mxLog("%s: derivatives %s units are meaningless",
					name, fitUnitsToName(fc->fitUnits));
		return; //Possible TODO: calculate Hessian anyway?
	}

	int newWanted = fc->wanted | FF_COMPUTE_GRADIENT;
	if (wantHessian) newWanted |= FF_COMPUTE_HESSIAN;

	int nf = fc->calcNumFree();
	if (numParams != 0 && numParams != nf) {
		mxThrow("%s: number of parameters changed from %d to %d",
			 name, numParams, nf);
	}

	numParams = nf;
	if (numParams <= 0) { complainNoFreeParam(); return; }

	optima.resize(numParams);
	fc->copyEstToOptimizer(optima);
	paramMap.resize(numParams);
	for (int px=0,ex=0; px < numParams; ++ex) {
		if (fc->profiledOut[ex]) continue;
		paramMap[px++] = ex;
	}

	omxAlgebraPreeval(fitMat, fc);
	fc->createChildren(fitMat); // allow FIML rowwiseParallel even when parallel=false

	fc->state->countNonlinearConstraints(fc->state->numEqC, fc->state->numIneqC, false);
	int c_n = fc->state->numEqC + fc->state->numIneqC;
	fc->constraintFunVals.resize(c_n);
	fc->constraintJacobian.resize(c_n, numParams);
	if(c_n){
		omxCalcFinalConstraintJacobian(fc, numParams);
	}
	// TODO: Allow more than one hessian value for calculation

	int numChildren = 1;
	if (parallel && !fc->openmpUser && fc->childList.size()) numChildren = fc->childList.size();

	if (!fc->haveReferenceFit(fitMat)) return;

	minimum = fc->fit;

	hessWorkVector = new hess_struct[numChildren];
	if (numChildren == 1) {
		omxPopulateHessianWork(hessWorkVector, fc);
	} else {
		for(int i = 0; i < numChildren; i++) {
			omxPopulateHessianWork(hessWorkVector + i, fc->childList[i]);
		}
	}
	if(verbose >= 1) mxLog("Numerical Hessian approximation (%d children, ref fit %.2f)",
			       numChildren, minimum);

	hessian = NULL;
	if (wantHessian) {
		hessian = fc->getDenseHessUninitialized();
		Eigen::Map< Eigen::MatrixXd > eH(hessian, numParams, numParams);
		eH.setConstant(NA_REAL);

		if (knownHessian) {
			int khSize = int(khMap.size());
			Eigen::Map< Eigen::MatrixXd > kh(knownHessian, khSize, khMap.size());
			for (int rx=0; rx < khSize; ++rx) {
				for (int cx=0; cx < khSize; ++cx) {
					if (khMap[rx] < 0 || khMap[cx] < 0) continue;
					eH(khMap[rx], khMap[cx]) = kh(rx, cx);
				}
			}
		}
	}

	if (detail) {
		recordDetail = false; // already done it once
	} else {
		Rf_protect(detail = Rf_allocVector(VECSXP, 4));
		SET_VECTOR_ELT(detail, 0, Rf_allocVector(LGLSXP, numParams));
		for (int gx=0; gx < 3; ++gx) {
			SET_VECTOR_ELT(detail, 1+gx, Rf_allocVector(REALSXP, numParams));
		}
		SEXP detailCols;
		Rf_protect(detailCols = Rf_allocVector(STRSXP, 4));
		Rf_setAttrib(detail, R_NamesSymbol, detailCols);
		SET_STRING_ELT(detailCols, 0, Rf_mkChar("symmetric"));
		SET_STRING_ELT(detailCols, 1, Rf_mkChar("forward"));
		SET_STRING_ELT(detailCols, 2, Rf_mkChar("central"));
		SET_STRING_ELT(detailCols, 3, Rf_mkChar("backward"));

		SEXP detailRowNames;
		Rf_protect(detailRowNames = Rf_allocVector(STRSXP, numParams));
		Rf_setAttrib(detail, R_RowNamesSymbol, detailRowNames);
		for (int nx=0; nx < int(numParams); ++nx) {
			SET_STRING_ELT(detailRowNames, nx, Rf_mkChar(fc->varGroup->vars[nx]->name));
		}
		markAsDataFrame(detail);
	}

	gforward = REAL(VECTOR_ELT(detail, 1));
	gcentral = REAL(VECTOR_ELT(detail, 2));
	gbackward = REAL(VECTOR_ELT(detail, 3));
	Eigen::Map< Eigen::ArrayXd > Gf(gforward, numParams);
	Eigen::Map< Eigen::ArrayXd > Gc(gcentral, numParams);
	Eigen::Map< Eigen::ArrayXd > Gb(gbackward, numParams);
	Gf.setConstant(NA_REAL);
	Gc.setConstant(NA_REAL);
	Gb.setConstant(NA_REAL);

	calcHessianEntry che(this);
	CovEntrywiseParallel(numChildren, che);

	for(int i = 0; i < numChildren; i++) {
		struct hess_struct *hw = hessWorkVector + i;
		totalProbeCount += hw->probeCount;
	}
	delete [] hessWorkVector;
	if (isErrorRaised()) return;

	Eigen::Map< Eigen::ArrayXi > Gsymmetric(LOGICAL(VECTOR_ELT(detail, 0)), numParams);
	double gradNorm = 0.0;
	
	double feasibilityTolerance = Global->feasibilityTolerance;
	for (int px=0; px < numParams; ++px) {
		// factor out simliar code in ComputeNR
		omxFreeVar &fv = *fc->varGroup->vars[ paramMap[px] ];
		if ((fabs(optima[px] - fv.lbound) < feasibilityTolerance && Gc[px] > 0) ||
		    (fabs(optima[px] - fv.ubound) < feasibilityTolerance && Gc[px] < 0)) {
			Gsymmetric[px] = false;
			continue;
		}
		gradNorm += Gc[px] * Gc[px];
		double relsym = 2 * fabs(Gf[px] + Gb[px]) / (Gb[px] - Gf[px]);
		Gsymmetric[px] = (Gf[px] < 0 && 0 < Gb[px] && relsym < 1.5);
		if (checkGradient && verbose >= 2 && !Gsymmetric[px]) {
			mxLog("%s: param[%d] %d %f", name, px, Gsymmetric[px], relsym);
		}
	}
	
	fc->grad.resize(fc->numParam);
	fc->grad.setZero();
	fc->copyGradFromOptimizer(Gc);
	
	if(c_n){
		fc->inequality.resize(fc->state->numIneqC);
		fc->analyticIneqJacTmp.resize(fc->state->numIneqC, numParams);
		fc->myineqFun(true, verbose, omxConstraint::LESS_THAN, false);
	}

	gradNorm = sqrt(gradNorm);
	double gradThresh = Global->getGradientThreshold(minimum);
	//The gradient will generally not be near zero at a local minimum if there are equality constraints 
	//or active inequality constraints:
	if ( checkGradient && gradNorm > gradThresh && !(fc->state->numEqC || fc->inequality.array().sum()) ) {
		if (verbose >= 1) {
			mxLog("Some gradient entries are too large, norm %f", gradNorm);
		}
		if (fc->getInform() < INFORM_NOT_AT_OPTIMUM) fc->setInform(INFORM_NOT_AT_OPTIMUM);
	}

	fc->setEstFromOptimizer(optima);
	// auxillary information like per-row likelihoods need a refresh
	ComputeFit(name, fitMat, FF_COMPUTE_FIT, fc);
	fc->wanted = newWanted;
}
コード例 #3
0
ファイル: Interpreter.c プロジェクト: MARFMS/chiquitico
void Execute(void)
{
   Instruction I;
   int temp, temp1, temp2,temp3, Addr;
   char tempc;


    /* Start  here */
   I = StartInstruction;
   while (OpCodeOf(I) != HALTOP) 
   {
      if (TraceSpecified)
         TraceExecution(I);

      if (ValidOpCode(I)) 
      {
         switch (OpCodeOf(I)) 
         {
            case NOP     : break;

	    case LITOP   : PushLf(Operand1Of(I));
                           break;

	    case LLVOP   : PushLf(Lf(Operand1Of(I)));
                           break;

	    case LGVOP   : PushLf(Gf(Operand1Of(I)));
                           break;

            case SLVOP   : UpdateLf(Operand1Of(I),PopLf());
                           break;
 
            case SGVOP   : UpdateGf(Operand1Of(I),PopLf());
                           break;
 
            case LLIVOP  : PushLf(Gf(Lf(Operand1Of(I))));
                           break;
 
            case LGIVOP  : PushLf(Gf(Gf(Operand1Of(I))));
		    	   break;
 
            case SLIVOP  : UpdateGf(Lf(Operand1Of(I)),PopLf());
			   break;
 
            case SGIVOP  : UpdateGf(Gf(Operand1Of(I)),PopLf());
			   break;
 
            case LLAOP   : PushLf(LocalAddress(Operand1Of(I)));
			   break;
 
            case LGAOP   : PushLf(GlobalAddress(Operand1Of(I)));
			   break;

            case LUVOP   : Addr = LocalAddress(0);
                           for (temp = 1; temp <= Operand1Of(I); temp++) 
                           { 
                              if (Addr == 0) 
                              {
                                 fprintf(output,
                                         "TOO MANY FRAMES SPECIFIED IN LUV\n");
                                 FatalError();
	                      }
                              Addr = Gf(Addr);
                           }
                           PushLf(Gf(Addr + Operand2Of(I)));
		           break;

            case SUVOP   : Addr = LocalAddress(0);
                           for (temp = 1; temp <= Operand1Of(I); temp++) 
                           { 
                              if (Addr == 0)
                              {
                                 fprintf(output,
                                         "TOO MANY FRAMES SPECIFIED IN SUV\n");
                                 FatalError();
                              }
                              Addr = Gf(Addr);
                            }
                            UpdateGf (Addr+Operand2Of(I),PopLf());
                            break;

            case UOPOP   : temp = PopLf();
                           switch (Operand1Of(I)) 
                           {
                              case UNOT  : PushLf(1 - temp); 
                                           break;

                              case UNEG  : PushLf(- temp);
                                           break;

                              case USUCC : PushLf(temp + 1);
                                           break;

                              case UPRED : PushLf(temp - 1);
                                           break;
			    }
			    break;

	     case BOPOP   : temp2 = PopLf(); 
                            temp1 = PopLf();
                            if (ValidBinOp(I)) 
                            {
                               switch (Operand1Of(I)) 
                               {
                                  case BAND   : PushLf(temp1 * temp2);
                                                break;

                                  case BOR    : if ((temp1+temp2) > 0)
                                                   PushLf (1);
                                                else 
                                                   PushLf(0);
                                                break;

                                  case BPLUS  : PushLf(temp1 + temp2);
                                                break;

                                  case BMINUS : PushLf(temp1 - temp2);
                                                break;

                                  case BMULT  : PushLf(temp1 * temp2);
                                                break;

                                  case BDIV   : if (temp2 == 0) 
                                                {
                                                   fprintf(output,
                                                    "<<< MACHINE ERROR >>>: ");
                                                   fprintf(output,
                                                    "DIVISION BY ZERO.\n");
                                                   DumpMemory(output);
                                                   FatalError();
					        } 
                                                else 
                                                   PushLf(temp1 / temp2);
                                              
                                                break;

                                  case BEXP   : if (temp2 < 0) 
                                                {
                                                   fprintf(output,
                                                    "<<< MACHINE ERROR >>>: ");
                                                   fprintf(output,
                                                    "NEGATIVE EXPONENT. \n");
                                                   DumpMemory(output);
                                                   FatalError();
                                                } 
                                                else 
                                                {
                                                   temp = 1;
                                                   for (temp3=1; temp3<=temp2; temp3++)
                                                      temp = temp * temp1;
                                                   PushLf(temp);
					        }
					        break;

                                  case BMOD   : PushLf(temp1 % temp2);
                                                break;

                                  case BEQ    : if (temp1 ==  temp2)
                                                   PushLf(1);
                                                else
                                                   PushLf(0);
                                                break;

                                  case BNE    : if (temp1 != temp2) 
                                                   PushLf(1);
                                                else
                                                   PushLf(0);
                                                break;

                                  case BLE    : if (temp1 <= temp2)
                                                   PushLf(1);
                                                else
                                                   PushLf (0);
                                                break;

                                  case BGE    : if (temp1 >= temp2)
                                                   PushLf (1);
                                                else
                                                   PushLf (0);
                                                break;

                                  case BLT    : if (temp1 <  temp2)
                                                   PushLf(1);
                                                else
                                                   PushLf(0);
                                                break;

                                  case BGT    : if (temp1 >  temp2)
                                                   PushLf(1);
                                                else
                                                   PushLf(0);
                                                break;

			       } /* switch(Operand1Of(I)) */

			    } 
                            else 
                            {
                               fprintf(output,"<<< MACHINE ERROR >>>: ");
                               fprintf(output,"UNKNOWN OPERAND NAME: ");
    		               fprintf(TraceFile," %d ",Operand1Of(I));
                               fprintf(output,"\n");
                               DumpMemory(output);
                               FatalError();
		            }
		            break;

             case POPOP   : PopOffLf(Operand1Of(I));
                            break;
 
             case DUPOP   : PushLf(TopLf());
                            break;
 
             case SWAPOP  : temp1 = PopLf();
                            temp2 = PopLf();
                            PushLf(temp1);
                            PushLf(temp2);
		            break;

             case CALLOP  : PushReturnStack(I);
                            OpenFrame(Operand1Of(I));
                            I = PopLf() - 1;
		            break;
 
             case RTNOP   : temp = DepthLf() - Operand1Of(I);
                            if (temp > 0) 
                            {
                             for (temp1 = 0; temp1 <= (Operand1Of(I) - 1); temp1++)
                                UpdateLf(temp1, Lf(temp + temp1));

                             PopOffLf(temp);
		            }
                            I = PopReturnStack();
                            CloseFrame (Operand1Of(I));
			    break;
 
             case GOTOOP  : I = Operand1Of(I) - 1;
		  	    break;

             case CONDOP  : temp = PopLf();
                            if (temp == 0)
                               I = Operand2Of(I) - 1;
                            else
                               I = Operand1Of(I) - 1;
                            break;
 
             case CODEOP  : PushLf(Operand1Of(I));
                            break;
 
             case LIMITOP : temp2 = PopLf();  /* U */
                            temp1 = PopLf();  /* L */
                            temp  = PopLf();  /* X */
                            if ((temp >= temp1) && (temp <= temp2))
                               PushLf(temp);
                            else 
                            {
                               fprintf(output,"<<< MACHINE ERROR >>>: ");
                               fprintf(output,"VALUE OUT OF RANGE\n");
                               DumpMemory(output);
                               FatalError();
			    }
			    break;

             case SOSOP   : if (ValidOsOp(I)) 
                            {
                                switch (Operand1Of(I)) 
                                {
                                   case TRACEX  : if (TraceSpecified)
                                                     TraceSpecified = false;
                                                  else
                                                     TraceSpecified = true;
					          break;

                                   case DUMPMEM : DumpMemory(output);
                                                  break;

                                   case OSINPUT : fscanf(input," ");
					          if (feof(input)) { 
                                                     fprintf(output,
                                                      "<<< MACHINE ERROR >>>: ");
                                                     fprintf(output,
                                                      "TRIED TO READ PAST <EOF>\n");
                                                     DumpMemory(output);
                                                     FatalError();
                                                  } 
                                                  else 
                                                  {
                                                     fscanf(input,"%d",&temp);
                                                     PushLf(temp);
					          }
					          break;
 
                                  case OSINPUTC : fscanf(input," ");
					          if (feof(input)) 
                                                  {
                                                     fprintf(output,
                                                      "<<< MACHINE ERROR >>>: ");
                                                     fprintf(output,
                                                      "TRIED TO READ PAST <EOF>\n");
                                                     DumpMemory(output);
                                                     FatalError();
                                                  } 
                                                  else 
                                                  {
                                                     fscanf(input,"%c",&tempc);
                                                     PushLf(tempc);
					          }
					          break;
 
                                  case OSOUTPUT : temp = PopLf();
                                                  fprintf(output,"%d ",temp);
					          break;
 
                                  case OSOUTPUTC: tempc = PopLf();
                                                  fprintf(output,"%c",tempc);
					          break;
 
                                  case OSOUTPUTL: fprintf(output,"\n");
						  break;
 
                                  case OSEOF    : fscanf(input," ");
					          if (feof(input))
                                                     PushLf(1);
                                                  else
                                                     PushLf(0);
					          break;
                                }
			     } 
                             else 
                             {
                                fprintf(output,"%s","<<< MACHINE ERROR >>>: ");
                                fprintf(output,"%s%1d `",
                                        "UNKNOWN OPERAND NAME: ",Operand1Of(I));
    		                fprintf(TraceFile," %d ",Operand1Of(I));
                                fprintf(output, "` \n");
                                DumpMemory(output);
                                FatalError();
                             }
                             break;
	 } /* switch (OpCodeOf(I)) */
              
      } 
      else 
      {
         fprintf(output,"%s","<<< MACHINE ERROR >>>: ");
         fprintf(output,"%s","UNKNOWN OP CODE NAME: ");
    	 fprintf(output," %d ",OpCodeOf(I));
         fprintf(output,"\n");
         FatalError();
      }
 
      I++;
 
   }
}  
コード例 #4
0
ファイル: ge_context.c プロジェクト: lmorel3/libge
void geClearColor(u32 color){
	libge_context->clear_color = color;
	glClearColor(Rf(libge_context->clear_color), Gf(libge_context->clear_color), Bf(libge_context->clear_color), Af(libge_context->clear_color));
}