Example #1
0
/**
 * im_buildlut:
 * @input: input mask
 * @output: output image
 *
 * This operation builds a lookup table from a set of points. Intermediate
 * values are generated by piecewise linear interpolation. 
 *
 * For example, consider this 2 x 2 matrix of (x, y) coordinates:
 *
 *   <tgroup cols='2' align='left' colsep='1' rowsep='1'>
 *     <tbody>
 *       <row>
 *         <entry>0</entry>
 *         <entry>0</entry>
 *       </row>
 *       <row>
 *         <entry>255</entry>
 *         <entry>100</entry>
 *       </row>
 *     </tbody>
 *   </tgroup>
 * 
 * We then generate:
 *
 *   <tgroup cols='2' align='left' colsep='1' rowsep='1'>
 *     <thead>
 *       <row>
 *         <entry>Index</entry>
 *         <entry>Value</entry>
 *       </row>
 *     </thead>
 *     <tbody>
 *       <row>
 *         <entry>0</entry>
 *         <entry>0</entry>
 *       </row>
 *       <row>
 *         <entry>1</entry>
 *         <entry>0.4</entry>
 *       </row>
 *       <row>
 *         <entry>...</entry>
 *         <entry>etc. by linear interpolation</entry>
 *       </row>
 *       <row>
 *         <entry>255</entry>
 *         <entry>100</entry>
 *       </row>
 *     </tbody>
 *   </tgroup>
 *
 * This is then written as the output image, with the left column giving the
 * index in the image to place the value.
 * 
 * The (x, y) points don't need to be sorted: we do that. You can have 
 * several Ys, each becomes a band in the output LUT. You don't need to
 * start at zero, any integer will do, including negatives.
 *
 * See also: im_identity(), im_invertlut().
 *
 * Returns: 0 on success, -1 on error
 */
int
im_buildlut( DOUBLEMASK *input, IMAGE *output )
{
	State state;

	if( !input || input->xsize < 2 || input->ysize < 1 ) {
		im_error( "im_buildlut", "%s", _( "bad input matrix size" ) );
		return( -1 );
	}

	if( build_state( &state, input ) ||
		buildlut( &state ) ) {
		free_state( &state );
                return( -1 );
	}

        im_initdesc( output,
                state.lut_size, 1, input->xsize - 1, 
		IM_BBITS_DOUBLE, IM_BANDFMT_DOUBLE,
                IM_CODING_NONE, IM_TYPE_HISTOGRAM, 1.0, 1.0, 0, 0 );
        if( im_setupout( output ) ||
		im_writeline( 0, output, (PEL *) state.buf ) ) {
		free_state( &state );
		return( -1 );
	}

	free_state( &state );

	return( 0 );
}
void r300TranslateFragmentShader(r300ContextPtr r300,
				 struct r300_fragment_program *fp)
{
	struct r300_fragment_program_external_state state;

	build_state(r300, fp, &state);
	if (_mesa_memcmp(&fp->state, &state, sizeof(state))) {
		/* TODO: cache compiled programs */
		fp->translated = GL_FALSE;
		_mesa_memcpy(&fp->state, &state, sizeof(state));
	}

	if (!fp->translated) {
		struct r300_fragment_program_compiler compiler;

		compiler.r300 = r300;
		compiler.fp = fp;
		compiler.code = &fp->code;
		compiler.program = _mesa_clone_program(r300->radeon.glCtx, &fp->mesa_program.Base);

		if (RADEON_DEBUG & DEBUG_PIXEL) {
			_mesa_printf("Fragment Program: Initial program:\n");
			_mesa_print_program(compiler.program);
		}

		insert_WPOS_trailer(&compiler);

		struct radeon_program_transformation transformations[] = {
			{ &transform_TEX, &compiler },
			{ &radeonTransformALU, 0 },
			{ &radeonTransformTrigSimple, 0 }
		};
		radeonLocalTransform(
			r300->radeon.glCtx,
			compiler.program,
			3, transformations);

		if (RADEON_DEBUG & DEBUG_PIXEL) {
			_mesa_printf("Fragment Program: After native rewrite:\n");
			_mesa_print_program(compiler.program);
		}

		struct radeon_nqssadce_descr nqssadce = {
			.Init = &nqssadce_init,
			.IsNativeSwizzle = &r300FPIsNativeSwizzle,
			.BuildSwizzle = &r300FPBuildSwizzle,
			.RewriteDepthOut = GL_TRUE
		};
		radeonNqssaDce(r300->radeon.glCtx, compiler.program, &nqssadce);

		if (RADEON_DEBUG & DEBUG_PIXEL) {
			_mesa_printf("Compiler: after NqSSA-DCE:\n");
			_mesa_print_program(compiler.program);
		}

		if (!r300FragmentProgramEmit(&compiler))
			fp->error = GL_TRUE;

		/* Subtle: Rescue any parameters that have been added during transformations */
		_mesa_free_parameter_list(fp->mesa_program.Base.Parameters);
		fp->mesa_program.Base.Parameters = compiler.program->Parameters;
		compiler.program->Parameters = 0;

		_mesa_reference_program(r300->radeon.glCtx, &compiler.program, NULL);

		if (!fp->error)
			fp->translated = GL_TRUE;
		if (fp->error || (RADEON_DEBUG & DEBUG_PIXEL))
			r300FragmentProgramDump(fp, &fp->code);
		r300UpdateStateParameters(r300->radeon.glCtx, _NEW_PROGRAM);
	}

	update_params(r300, fp);
}
Example #3
0
int
main ( int argc , char** argv )
{
  /*
   * Do initialization steps here.
   */

  /*
   * Set up list of player id's.
   */
  char* players [2] = { "r" , "b" };

  /*
   * Attempt to initialize the game state to the default.
   *
   * Add more error checking?
   */
  State* state = build_state ( );
  FILE* init_file = fopen ( INIT_FILENAME , "r" );
  bool initialized = init_file && state_load ( state , init_file );
  fclose ( init_file );
  if ( ! initialized )
    return 1;


  /*
   * The top level of program logic resides nested within this loop,
   * which runs until one of the players sends the termination signal.
   *
   * Note that this is an infinite loop. The actual control logic for
   * this loop is a single goto statment in the verb interpreter, set
   * to jump outside the loop if and only if the VERB_TERM is
   * encountered. DO NOT BE ALARMED. THIS IS A LEGITIMATE USE OF THE
   * GOTO STATEMENT!
   */
  while ( true )
    {
      /*
       * The logic inside this loop effectively handles a single
       * round, from the starting position to reseting the game.
       */


      /*
       * Initialize the player indexer and player.
       */
      unsigned short indexer = 0;
      char* player = players[0];


      /*
       * While a game is still in progress (i.e. while both players
       * have legal moves available), this loop alternates between
       * the red and black players, starting with the red player.
       *
       * The control statement for this loop determines whether the
       * player has any vaild moves. If the player does not have any
       * valid moves, the control logic of the interpreter loop is
       * modified to force the losing player to fix the situation.
       * This could change in the future, so with minor changes,
       * control could just be made to fall through to a reset
       * after this loop, it just depends on what happens during
       * development.
       */
      while ( state_has_moves ( state , player ) )
        {
          /*
           * The logic inside this loop effectively handles a single
           * turn, allowing an unlimited number of commands which are
           * not moves to be performed, followed by a single move.
           *
           * Notice that both players must be trusted for this system
           * to work as-is, otherwise any player could modify the
           * state of the game to their advantage. This is a standing
           * security issue, but one which can be ignored at the
           * moment for the academic purposes of the project.
           */

          /*
           * Initialize the keep_turn flag.
           */
          bool keep_turn = true;

          /*
           * While the player has not yet made a move, it is still
           * the player's turn. Thus, this loop terminates only after
           * a move has been made, processing any other commands
           * the player gives during the current turn. Control stays
           * with the player anyways if he has no moves, as he has
           * lost and the losing player is responsible for resetting
           * the board.
           */
          while ( keep_turn || ( ! state_has_moves ( state , player ) ) )
            {
              /*
               * This logic effectively handles a single command from
               * the current player. This involves reading the command
               * from the player's input pipe, interpreting and
               * executing the command, and returning the result of
               * the command to the player as appropriate.
               */

              /*
               * Initialize the command buffer.
               */
              //char* cmd = malloc ( sizeof ( char ) * CMD_BUFFER_SIZE );
              char* cmd = NULL;
              size_t cmdlen = 0;

              /*
               * Read the next command from stdin.
               *
               * Might try and modify this to avoid the buffer.
               */
              getline ( &cmd , &cmdlen , stdin );

              /*
               * Extract the verb of the command, this is always the
               * first word of the command, in a HTTP-like format.
               */
              char* verb = strtok ( cmd , " \n" );


              /*
               * Use an if-else chain statement to select the proper
               * code to execute for each verb.
               */
              if ( ! verb )
                {
                  /*
                   * If the entry was just a newline, ignore it.
                   */
                }
                
              else if ( ! strcmp ( verb , VERB_TERM ) )
                {
                  /*
                   * If the term signal is given, then jump to the
                   * term_exit branch.
                   */

                  goto term_exit;
                }

              else if ( ! strcmp ( verb , VERB_LOAD ) )
                {
                  cmd_load ( cmd , state );
                }

              else if ( ! strcmp ( verb , VERB_SAVE ) )
                {
                  cmd_save ( cmd , state );
                }

              else if ( ! strcmp ( verb , VERB_TGET ) )
                {
                  cmd_tget ( cmd , state );
                }

              else if ( ! strcmp ( verb , VERB_TSET ) )
                {
                  cmd_tset ( cmd , state );
                }

              else if ( ! strcmp ( verb , VERB_MOVE ) )
                {
                  /*
                   * This code executes if the verb is the move command.
                   * It then parses the remainder of the command to
                   * determine its validity. If the command is well-
                   * structured and the move is legal, the move is made
                   * and the moved flag set to true. Otherwise, a 400
                   * BAD SYNTAX error will be returned. If the move
                   * requested is well-formed but illegal, a 403 MOVE
                   * FORBIDDEN error is returned. Assuming no errors
                   * occur, a 200 OK message is returned.
                   */

                  cmd_move ( cmd , state );

                  keep_turn = false;
                }

              else
                {
                  /*
                   * If we don't recognize the given verb, we do the
                   * safe thing and return the verb nonexistant error.
                   */
                  //puts ( "PCIP/1.0 404 NOTVERB" );
                  puts ( "NULL" );
                }


              /*
               * Deallocate the command buffer.
               */
              free ( cmd );
           }
          
          /*
           * Increment the player indexer with wraparound. Set the
           * pipes pointing to the current player's input and
           * output.
           *
           * Trust me, I have a reason for putting it here.
           */
          indexer = ( indexer + 1 ) % PLAYER_COUNT;
          player = players[indexer];
          state->player = *player;

          /*
           * Announce the turn change.
           */
          printf ( "PCIP/1.0 201 NEW TURN\n%c\n" , *player );
       }


      /*
       * This is the interpreter loop's aforementioned implicit fall
       * through case for processing end of game conditions.
       *
       * Given the termination condition of the previous loop, the
       * player input and output pipes now point to the loosing
       * player, who is now responsible for resetting the game.
       */
    }


  /*
   * If the term command is given, the program jumps to this point,
   * just before deallocation of top-level dynamic resources occurs.
   * Note that breaking out of a switch statement and a triple nested 
   * do-while loop *is* a legitimate use of goto.
   */
term_exit:


  /*
   * Clean up.
   */

  /*
   * Deallocate the game state.
   */
  free_state ( state );


  /*
   * If we got this far without an incident, the program should have
   * run successfully, so we return the corresponding status code.
   */
  return EXIT_SUCCESS;
};