Friday, November 12, 2010

Get pixel from UIImage to do Image Processing:


This is a sample for converting image to grayscale. In this example, we can know how to get pixel from UIImage to do some Image Processing.




- (UIImage *)convertToOpaque:(UIImage *)image toEndPoint:(CGPoint)point
{
    int width = image.size.width;
    int height = image.size.height;

    // the pixels will be painted to this array
    uint32_t *pixels = (uint32_t *) malloc(width * height * sizeof(uint32_t));

    // clear the pixels so any transparency is preserved
    memset(pixels, 0, width * height * sizeof(uint32_t));
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    // create a context with RGBA pixels
    CGContextRef context = CGBitmapContextCreate(pixels, width, height, 8, width * sizeof(uint32_t), colorSpace, 
                                                 kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedLast);

    // paint the bitmap to our context which will fill in the pixels array
    CGContextDrawImage(context, CGRectMake(0, 0, width, height), [image CGImage]);

    // Here you can get rgba Pixel and you can do any thing with this rgba 
    //alpha at rgbaPixel[0], Red at rgbaPixel[1], Green at rgbaPixel[2], Blue at rgbaPixel[3]

  for (int y=0; y<height; y++) {
    for (int x=0; x<width; x++) {
        uint8_t *rgbaPixel = (uint8_t *) &pixels[y * width + x];
                  /*****************************
                   Do something here
                   For example:
                   int average = (rgbaPixel[1] + rgbaPixel[2] + rgbaPixel[3]) / 3;
                   rgbaPixel[1] = average;
                   rgbaPixel[2] = average;
                   rgbaPixel[3] = average;
                   *****************************/
    }
  }

  // create a new CGImageRef from our context with the modified pixels
    CGImageRef rImage = CGBitmapContextCreateImage(context);

    // we're done with the context, color space, and pixels
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);
    free(pixels);

    // make a new UIImage to return
    UIImage *resultUIImage = [UIImage imageWithCGImage:rImage];

    // we're done with image now too
    CGImageRelease(rImage);
    return resultUIImage;
}


By Dragon

Wednesday, November 10, 2010

Simple Guide for Git

This is a simple guide for git to tell you how to use git to manage your source code. Here, we assume that your git server and your local computer are different. We will teach you how to initialize your git server, put your source to the server, download the source from the server, and update your changes to the server step by step. The git commands used here, however, are all very easy and basic, but they can give you a clear picture of how gits work.

Server: 192.168.0.100
User on the server: git
Project: spsw
User on the client: Wayne


  • On the server
    1. Login the git server
      ssh git@192.168.0.100
    2. Create a directory for the project "spsw". A repository on server should be bare, and by convention, a bare repository is named with ".git" suffix.
      mkdir spsw.git
      cd spsw.git
    3. Initialize a bare repository.
      git init --bare
  • On the Client
    1. Setup you personal information.
      git config --global user.name "Wayne"
      git config --global user.email "wayne@gmail.com"
    2. Create a non-bare repository. There are two ways to do this.
      1. By cloning the source from the server. (This assumes that the server has the source code, but so far we haven't put our source to the server - the server doesn't has the source code)
        1. git clone git@192.168.0.100:/home/git/spsw.git
      2. By creating a new project and then putting to the server.
        1. Create a directory for the project spsw. (".git" suffix is not needed because it's a non-bare repository)
          mkdir spsw
          cd spsw
        2. Initialize a non-bare repositry.
          git init
        3. Copy your source here
        4. Add all the files. ("git add ." is the choice for this case)
          git add .    # Add new and modified without deleted
          git add -u   # Add modified and updated without new
          git add -A  # git add . && git add -u
        5. Show what you've added or changed if you want.
          git diff
        6. Commit the changes.
          git commit -a -m "changes description"
        7. Add the git server source and name it "origin"
          git remote add origin git@192.168.0.100:/home/git/spsw.git
        8. Put the source code in current branch to the server "origin" and branch "master".
          git push origin master
    3. Upate the source from the server.
      git fetch    # download the source to branch "origin/master"
      git merge   # merge "origin/master" to the current branch.
      git pull      # git fetch && git merge
    4. Some useful commands operating branches
      1. Create a new branch
        git branch new_branch
      2. Swith to new_branch
        git checkout new_branch
      3. Delete new_branch
        git branch -D new_branch
      4. show the list of all the branches
        git branch -a
        git show-branch
    5. Other useful commands
      1. View all you've commit
        git log
      2. Show a commit detail
        git show commit_number
      3. Give a commit a tag like a name
        git tag -m "version 1.0" 1.0 commit_number
        git show 1.0
  • Reference
    1. http://progit.org/book/zh/ch1-1.html
    2. http://zh-tw.whygitisbetterthanx.com/#cheap-local-branching
    3. http://gitref.org/
    4. http://tw.myblog.yahoo.com/stevegigijoe/article?mid=173&next=172&l=f&fid=8
  • Books
    1. Version Control with Git
    2. Pro Git
Wayne

Cross-Compile Tesseract 3.00 for iPhone SDK 3GS

The shell script below can be used to cross-compile Tesseract 3.00 for iPhone 3GS. The original script is from:

Wayne

#!/bin/sh
# build_fat.sh
#
# Created by Robert Carlsen on 15.07.2009. Updated 24.9.2010
# build an arm / i386 lib of standard linux project
#
# initially configured for tesseract-ocr v2.0.4
# updated for tesseract prerelease v3

export DEVROOT=/Developer/Platforms/iPhoneOS.platform/Developer
export SDKROOT=$DEVROOT/SDKs/iPhoneOS3.2.sdk
export CFLAGS="-arch armv6 -pipe -no-cpp-precomp -isysroot $SDKROOT -miphoneos-version-min=2.2 -I$SDKROOT/usr/lib/gcc/arm-apple-darwin9/4.0.1/include -I$SDKROOT/usr/include/"export CPPFLAGS="$CFLAGS"
export CXXFLAGS="$CFLAGS"
export LDFLAGS="-L$SDKROOT/usr/lib/"
export LDFLAGS="-L$SDKROOT/usr/lib/ -Wl,-dylib_install_name,@executable_path/"
export LD="$DEVROOT/usr/bin/ld"
export CPP="$DEVROOT/usr/bin/arm-apple-darwin10-cpp-4.2.1"
export CXX="$DEVROOT/usr/bin/arm-apple-darwin10-g++-4.0.1"
export CC="$DEVROOT/usr/bin/arm-apple-darwin10-gcc-4.0.1"
./configure --host=arm-apple-darwin
make -j3

ar r libtesseract_full.a `find . -name "libtesseract_*.o"`
ranlib libtesseract_full.a

Friday, October 8, 2010

Using NSThread implement Timer


用內建的NSTimer時,會因view中加入以UIScrollView為base的物件時,而導致托拉View時產生Timer hold住的情形。此時我們需要自已利用 NSThread + NSThread 來實作我們自已的Timer以解決上述問題。其例子如下:



-(void)startTimer{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[NSThread detachNewThreadSelector:@selector(launchTimer) toTarget:self withObject:nil];
[pool release];

-(void)launchTimer{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[NSThread setThreadPriority:1.0];
continuePlaying = YES;
while (continuePlaying) { // loop until cancelled
[self performSelectorOnMainThread:@selector(updateTimer) withObject:nil waitUntilDone:NO];
[NSThread sleepForTimeInterval:1.0];
}
[pool release];
}
Dragon

Thursday, October 7, 2010

dragon_test

Hello everybody, I'm dragon.

Hello World!

Hello World!

This blog is where we, SPSW Team, share and discuss what we're thinking and doing with anyone who is interested in. Hope this blog will be growing up very, very fast!

Wayne