Beispiel #1
0
static void _signal(int sig)
{
    m_test_passed = false;

    _color(MCOLOR_RED);
    switch (sig) {
    case SIGFPE:
        printf("Failed! \n\n    Division by Zero\n\n");
        break;
    case SIGILL:
        printf("Failed! \n\n    Illegal Instruction\n\n");
        break;
    case SIGSEGV:
        printf("Failed! \n\n    Segmetation Fault\n\n");
        break;
    default:
        break;
    }
    _color(MCOLOR_RESET);

    puts("    | Stopping Execution.");
    fflush(stdout);
    exit(1);
}
Beispiel #2
0
 void Image::drawRectangle(JSONValue & region, int color[3])
 {
     try
     {
         cv::Scalar _color(color[0],color[1],color[2]);
         cv::Rect rectangle;    
         rectangle.x = region[0].GetInt();
         rectangle.y = region[1].GetInt();
         rectangle.width = region[2].GetInt() - region[0].GetInt();
         rectangle.height = region[3].GetInt() - region[1].GetInt();
         cv::rectangle(m_image, rectangle, _color, 1);
     }
     catch(cv::Exception & ex)
     {
         throw OpenCVException(ex.msg.c_str());
     }
 }
Beispiel #3
0
bool _imageFile::writeBitmap( const _bitmap& source , _optValue<_mimeType> mimeType2write , _optValue<_imageFileCompression> compression )
{
    if( !source.isValid() )
        return false;

    bool result = false;
    _mimeType mime = mimeType2write.isValid() ? (_mimeType)mimeType2write : this->getRealMime();

    // If file doesn't exist already, create it!
    this->create();

    switch( mime )
    {
    case _mime::image_bmp:
        GenericBMPEncoder::encode( this->getFileName().c_str() , source.getWidth() , source.getHeight() , source.getBitmap() );
        break;
    case _mime::image_png:
    {
        YsRawPngEncoder* encoder = new YsRawPngEncoder();
        if( compression.isValid() && (_imageFileCompression)compression == _imageFileCompression::none )
            encoder->SetDontCompress(1);
        _u32 numberPixels = source.getWidth()*source.getHeight();
        _u8* buffer = new _u8[numberPixels*4];
        _u8* tempBuffer = buffer;
        _pixelArray data = source.getBitmap();
        do {
            _color col = _color(*data++);
            tempBuffer[0] = col.getR()<<3;
            tempBuffer[1] = col.getG()<<3;
            tempBuffer[2] = col.getB()<<3;
            tempBuffer[3] = col.getAlpha()<<7; // Expand 1bit Alpha to 8bits
            tempBuffer+=4;
        } while( --numberPixels > 0 );
        result = encoder->EncodeToFile( this->getFileName().c_str() , source.getWidth() , source.getHeight() , 8 /*8bit per Channel*/, 6 /*True-Color with Alpha*/ , buffer );
        delete[] buffer;
        delete encoder;
        break;
    }
    case _mime::image_gif:
    {
        // Create palette with 256 entries
        _colorPalette palette = _colorPalette::fromBitmap( source );
        palette.downsample( 256 );

        // Open File for writing
        FILE* file = fopen( this->getFileName().c_str() , "wb+" );


        // Get Colors in the palette
        _vector<_color> colors = palette.getColors();

        _s16 transparentIndex = palette.hasTransparentColor() ? colors.size() - 1 : -1;


        // Predefines...
        _u16 numColors = colors.size();
        _u32 numPixels = source.getWidth()*source.getHeight();
        _u8* paletteBuffer = new _u8[numColors*3];
        _u8* imageBuffer = new _u8[numPixels];


        // Convert palette to _u8[3]
        for( int i = 0 ; i < numColors ; i++ ) {
            _color col = colors[i];
            paletteBuffer[i*3+0] = col.getR()<<3;
            paletteBuffer[i*3+1] = col.getG()<<3;
            paletteBuffer[i*3+2] = col.getB()<<3;
        }


        // Convert image to indexed format
        _pixelArray data = source.getBitmap();
        for( _u32 i = 0 ; i < numPixels; i++ )
            imageBuffer[i] = palette.getClosestColor(*data++);


        // Save gif!
        result = gif_write( file , imageBuffer , source.getWidth() , source.getHeight() , paletteBuffer , numColors , transparentIndex );

        delete[] paletteBuffer;
        delete[] imageBuffer;

        fclose( file );
        break;
    }
    case _mime::image_jpeg:
    {
        _u32 numberPixels = source.getWidth()*source.getHeight();
        _u8* buffer = new _u8[numberPixels*3];
        _u8* tempBuffer = buffer;
        _pixelArray data = source.getBitmap();
        do {
            _color col = _color(*data++);
            tempBuffer[0] = col.getR()<<3;
            tempBuffer[1] = col.getG()<<3;
            tempBuffer[2] = col.getB()<<3;
            tempBuffer += 3;
        } while( --numberPixels > 0 );
        int quality = compression.isValid() ? (int)(_imageFileCompression)compression : 90; // Set Quality
        result = jo_write_jpg( this->getFileName().c_str() , buffer , source.getWidth() , source.getHeight() , 3 , quality );
        delete[] buffer;
    }
    default:
        break;
    }

    return result;
}
Beispiel #4
0
    // Hash key of the FEN
    Key Zob::compute_fen_key (const   char *fen, bool c960) const
    {
        if (!fen)   return U64 (0);

        Key fen_key = U64 (0);
        File king[CLR_NO] = {F_NO};

#undef skip_whitespace
#undef get_next

#define skip_whitespace()  while (isspace (unsigned char (*fen))) ++fen
#define get_next()         ch = unsigned char (*fen++)

        unsigned char ch;
        for (Rank r = R_8; r >= R_1; --r)
        {
            File f = F_A;
            while (f <= F_H)
            {
                ch = *fen++;
                if (!ch) return U64 (0);

                if      (isdigit (ch))
                {
                    // empty square(s)
                    if ('1' > ch || ch > '8') return U64 (0);

                    uint8_t empty = (ch - '0');
                    f += empty;

                    if (f > F_NO) return U64 (0);
                }
                else if (isalpha (ch))
                {
                    uint32_t idx = CharPiece.find (ch);
                    if (idx != string::npos)
                    {
                        Piece p = Piece (idx);
                        if (EMPTY == p) return U64 (0);
                        if (KING == _ptype (p))  king[_color (p)] = f;

                        fen_key ^= _.psq_k[_color (p)][_ptype (p)][(f | r)];
                    }
                    ++f;
                }
                else
                {
                    return U64 (0);
                }
            }
            if (r > R_1)
            {
                ch = *fen++;
                if ('/' != ch) return U64 (0);
            }
        }
        skip_whitespace ();
        char active = get_next ();
        if ('w' == active) fen_key ^= _.mover_side;

        skip_whitespace ();
        // 3. Castling availability
        // Compatible with 3 standards:
        // * Normal FEN standard,
        // * Shredder-FEN that uses the letters of the columns on which the rooks began the game instead of KQkq
        // * X-FEN standard that, in case of Chess960, if an inner rook is associated with the castling right, the castling
        // tag is replaced by the file letter of the involved rook, as for the Shredder-FEN.
        get_next ();
        if ('-' != ch)
        {
            if (c960)
            {
                do
                {
                    Color c = isupper (ch) ? WHITE : BLACK;
                    char sym = tolower (ch);
                    if ('a' <= sym && sym <= 'h')
                    {
                        fen_key ^= _.castle_right[c][(king[c] < to_file (sym)) ? CS_K : CS_Q];
                    }
                    else
                    {
                        return U64 (0);
                    }

                    get_next ();
                }
                while (ch && !isspace (ch));
            }
            else
            {
                do
                {
                    //switch (ch)
                    //{
                    //case 'K': fen_key ^= _.castle_right[WHITE][CS_K]; break;
                    //case 'Q': fen_key ^= _.castle_right[WHITE][CS_Q]; break;
                    //case 'k': fen_key ^= _.castle_right[BLACK][CS_K]; break;
                    //case 'q': fen_key ^= _.castle_right[BLACK][CS_Q]; break;
                    //default:  return U64(0); break;
                    //}

                    Color c = isupper (ch) ? WHITE : BLACK;
                    switch (toupper (ch))
                    {
                    case 'K': fen_key ^= _.castle_right[c][CS_K]; break;
                    case 'Q': fen_key ^= _.castle_right[c][CS_Q]; break;
                    default:  return U64 (0); break;
                    }

                    get_next ();
                }
                while (ch && !isspace (ch));
            }
        }

        skip_whitespace ();
        get_next ();
        if ('-' != ch)
        {
            unsigned char ep_f = tolower (ch);
            if (!isalpha (ep_f)) return U64 (0);
            if ('a' > ep_f || ep_f > 'h') return U64 (0);

            unsigned char ep_r = get_next ();
            if (!isdigit (ep_r)) return U64 (0);
            if (('w' == active && '6' != ep_r) ||
                ('b' == active && '3' != ep_r)) return U64 (0);

            fen_key ^= _.en_passant[to_file (ep_f)];
        }

#undef skip_whitespace
#undef get_next

        return fen_key;
    }
	bool Physics::BoxBoxCollision(PhysicsComponent *Collider1, PhysicsComponent *Collider2)
	{
		Matrix4x4 worldMatrixInverse = Collider2->m_worldTransform.inverse();

			Vector3 localPosOfBox1 = worldMatrixInverse *  Collider1->m_originPos;
			Vector3 localPosOfBox2 = worldMatrixInverse * Collider2->m_originPos;

			Vector3 box1_Min = localPosOfBox1 + Vector3(-Collider1->m_boxWidth/2, 0, -Collider1->m_boxLength/2);
			Vector3 box1_Max = localPosOfBox1 + Vector3(Collider1->m_boxWidth/2, Collider1->m_boxHeight, Collider1->m_boxLength/2);

			Matrix4x4 worldMatrixInverseOfCollider1 = Collider1->m_worldTransform.inverse();
			Vector3 localPosOfBox1Collider1 = worldMatrixInverseOfCollider1 * Collider1->m_originPos;

			//Drawing for Box1
			Vector3 point0 = Collider1->m_worldTransform * (localPosOfBox1Collider1 + Vector3(Collider1->m_boxWidth/2, 0, Collider1->m_boxLength/2));
			Vector3 point1 = Collider1->m_worldTransform * (localPosOfBox1Collider1 + Vector3(-Collider1->m_boxWidth/2, 0, Collider1->m_boxLength/2));
			Vector3 point2 = Collider1->m_worldTransform * (localPosOfBox1Collider1 + Vector3(-Collider1->m_boxWidth/2, 0, -Collider1->m_boxLength/2));
			Vector3 point3 = Collider1->m_worldTransform * (localPosOfBox1Collider1 + Vector3(Collider1->m_boxWidth/2, 0, -Collider1->m_boxLength/2));

			Vector3 point4 = Collider1->m_worldTransform * (localPosOfBox1Collider1 + Vector3(Collider1->m_boxWidth/2, Collider1->m_boxHeight, Collider1->m_boxLength/2));
			Vector3 point5 = Collider1->m_worldTransform * (localPosOfBox1Collider1 + Vector3(-Collider1->m_boxWidth/2, Collider1->m_boxHeight, Collider1->m_boxLength/2));
			Vector3 point6 = Collider1->m_worldTransform * (localPosOfBox1Collider1 + Vector3(-Collider1->m_boxWidth/2, Collider1->m_boxHeight, -Collider1->m_boxLength/2));
			Vector3 point7 = Collider1->m_worldTransform * (localPosOfBox1Collider1 + Vector3(Collider1->m_boxWidth/2, Collider1->m_boxHeight, -Collider1->m_boxLength/2));

			Vector3 color(0.25f, 1.75f, 0.10f);

			DebugLines::drawDebugLinesForBox(point0,point1,point2,point3,point4,point5,point6,point7,color);			

			//Drawing for Box2

			Vector3 box2_Min = localPosOfBox2 + Vector3(-Collider2->m_boxWidth/2, 0, -Collider2->m_boxLength/2);
			Vector3 box2_Max = localPosOfBox2 + Vector3(Collider2->m_boxWidth/2, Collider2->m_boxHeight, Collider2->m_boxLength/2);

			Vector3 _point0 = Collider2->m_worldTransform * (localPosOfBox2 + Vector3(Collider2->m_boxWidth/2, 0, Collider2->m_boxLength/2));
			Vector3 _point1 = Collider2->m_worldTransform * (localPosOfBox2 + Vector3(-Collider2->m_boxWidth/2, 0, Collider2->m_boxLength/2));
			Vector3 _point2 = Collider2->m_worldTransform * (localPosOfBox2 + Vector3(-Collider2->m_boxWidth/2, 0, -Collider2->m_boxLength/2));
			Vector3 _point3 = Collider2->m_worldTransform * (localPosOfBox2 + Vector3(Collider2->m_boxWidth/2, 0, -Collider2->m_boxLength/2));

			Vector3 _point4 = Collider2->m_worldTransform * (localPosOfBox2 + Vector3(Collider2->m_boxWidth/2, Collider2->m_boxHeight, Collider2->m_boxLength/2));
			Vector3 _point5 = Collider2->m_worldTransform * (localPosOfBox2 + Vector3(-Collider2->m_boxWidth/2, Collider2->m_boxHeight, Collider2->m_boxLength/2));
			Vector3 _point6 = Collider2->m_worldTransform * (localPosOfBox2 + Vector3(-Collider2->m_boxWidth/2, Collider2->m_boxHeight, -Collider2->m_boxLength/2));
			Vector3 _point7 = Collider2->m_worldTransform * (localPosOfBox2 + Vector3(Collider2->m_boxWidth/2, Collider2->m_boxHeight, -Collider2->m_boxLength/2));
		
			Vector3 _color(0.25f, 1.75f, 0.10f);

			DebugLines::drawDebugLinesForBox(_point0,_point1,_point2,_point3,_point4,_point5,_point6,_point7,_color);
			
			bool collisionCheck1, collisionCheck2, collisionCheck3, collisionCheck4, collisionCheck5, collisionCheck6;

			collisionCheck1 = collisionCheck2 = collisionCheck3 = collisionCheck4 = collisionCheck5 = collisionCheck6 = true;
			
			if(box1_Min.m_x > box2_Max.m_x)
			{
				collisionCheck1 = false;
			}
			if(box1_Max.m_x < box2_Min.m_x)
			{
				collisionCheck2 = false;
			}
			if(box1_Min.m_y > box2_Max.m_y)
			{
				collisionCheck3 = false;
			}
			if(box1_Max.m_y < box2_Min.m_y)
			{
				collisionCheck4 = false;
			}
			if(box1_Min.m_z > box2_Max.m_z)
			{
				collisionCheck3 = false;
			}
			if(box1_Max.m_z < box2_Min.m_z)
			{
				collisionCheck4 = false;
			}

			if(collisionCheck1 && collisionCheck2 && collisionCheck3 && collisionCheck4 && collisionCheck5 && collisionCheck6)
			{
				//OutputDebugStringA("Box-Box collision detected\n");
				return true;
			}

			return false;

	}
Beispiel #6
0
static void _put_summary()
{
    puts("");
    puts("  +---------------------------------------------------+");
    puts("  |                      Summary                      |");
    puts("  +---------++------------+-------------+-------------+");

    printf("  | Suites  ||");
    _color(MCOLOR_YELLOW);  printf(" Total %4d ",  m_num_suite);
    _color(MCOLOR_RESET); putchar('|');
    _color(MCOLOR_GREEN);   printf(" Passed %4d ", m_num_suite_pass);
    _color(MCOLOR_RESET); putchar('|');
    _color(MCOLOR_RED);     printf(" Failed %4d ", m_num_suite_fail);
    _color(MCOLOR_RESET); puts("|");

    printf("  | Tests   ||");
    _color(MCOLOR_YELLOW);  printf(" Total %4d ",  m_num_test);
    _color(MCOLOR_RESET); putchar('|');
    _color(MCOLOR_GREEN);   printf(" Passed %4d ", m_num_test_pass);
    _color(MCOLOR_RESET); putchar('|');
    _color(MCOLOR_RED);     printf(" Failed %4d ", m_num_test_fail);
    _color(MCOLOR_RESET); puts("|");

    printf("  | Asserts ||");
    _color(MCOLOR_YELLOW);  printf(" Total %4d ",  m_num_assert);
    _color(MCOLOR_RESET); putchar('|');
    _color(MCOLOR_GREEN);   printf(" Passed %4d ", m_num_assert_pass);
    _color(MCOLOR_RESET); putchar('|');
    _color(MCOLOR_RED);     printf(" Failed %4d ", m_num_assert_fail);
    _color(MCOLOR_RESET); puts("|");

    puts("  +---------++------------+-------------+-------------+");
    puts("");
}
Beispiel #7
0
int mtest_run()
{
    bool suite_passed;
    clock_t start, end;
    double duration;

    signal(SIGFPE,  _signal);
    signal(SIGILL,  _signal);
    signal(SIGSEGV, _signal);

    puts("");
    puts("    +-----------------------------------------+");
    puts("    | mtest, micro test tool                  |");
    puts("    |                                         |");
    puts("    | stolen from github.com/orangeduck/ptest |");
    puts("    +-----------------------------------------+");

    start = clock();

    suite_passed = true;

    for (int i = 0; i < m_num_test; i++) {
        mtest_t test = m_tests[i];

        suite_passed = true;

        if (i == 0 || strcmp(m_tests[i].suitename, m_tests[i-1].suitename)) {
            printf("\n\n  ====== Suite %s ======\n\n", m_tests[i].suitename);

            m_test_passed = true;

            if (i != 0) {
                if (suite_passed) m_num_suite_pass++;
                else m_num_suite_fail++;
            }
        }

        printf("    | %s ... ", test.funcname);
        fflush(stdout);

        test.func();

        suite_passed = suite_passed && m_test_passed;

        if (m_test_passed) {
            m_num_test_pass++;

            _color(MCOLOR_GREEN);
            puts("Passed!");
            _color(MCOLOR_RESET);
        } else {
            m_num_test_fail++;

            _color(MCOLOR_RED);
            printf("Failed! \n\n%s\n", m_assert_error);
            _color(MCOLOR_RESET);
        }
    }

    if (suite_passed) m_num_suite_pass++;
    else m_num_suite_fail++;

    end = clock();

    _put_summary();

    duration = (double)(end - start) / CLOCKS_PER_SEC;

    printf("    Total Running Time: %0.3fs\n\n", duration);

    if (m_num_suite_fail > 0) return 1;
    else return 0;
}