Wednesday, February 28, 2007

example code for BMP image display!

something I tried out for 256 colour bmp, with no attempt at cleanup [It is not hard after all!]:
Algorithm is here: The .bmp file format


#include "stdafx.h"
#include "ImageDisplay.h"
#include <fstream>
#include <map>
#include <CommDlg.h>
using namespace std;

....
ifstream image(fileName);
...
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
int dummy;
long dummyLong;
//read header
image.read((char*)&dummy, 2);
long size;
//read file size
image.read((char*)&size, 4);
long bitmapOffset;
image.read((char*)&dummyLong, 4);
image.read((char*)&bitmapOffset, 4);
long bitMapHeaderSize;
image.read((char*)&bitMapHeaderSize, 4);
long width;
image.read((char*)&width, 4);
long height;
image.read((char*)&height, 4);
image.read((char*)&dummy, 2);
int bitsPerPixel;
image.read((char*)&bitsPerPixel, 2);
bitsPerPixel = *((char*)&bitsPerPixel);
int test;
image.read((char*)&test, 2);
int paletteOffset;
int paletteSize;
paletteSize = 4* (1 << bitsPerPixel);
paletteOffset = bitmapOffset - paletteSize;
currentPalette = new unsigned char[paletteSize];
image.seekg(paletteOffset);
int l;
for(l = 0; l < paletteSize;++l) {
image.read((char*)&(currentPalette[l]), 1);
}
image.seekg(bitmapOffset);
for(i = 0; i < height; ++i) {
for(j = 0; j < width; ++j) {
unsigned char charVal = 0;
image.read((char*)&charVal, 1);
if(0 == charVal) {
continue;
}
int val = charVal* 4;
SetPixel(hdc, j, height-i,/*RGB(r, g, b)*/RGB(currentPalette[val+2], currentPalette[val+1], currentPalette[val]));
}
}
EndPaint(hWnd, &ps);
...

Monday, February 26, 2007

Sunday, February 25, 2007

cool talk about image processing

Nice one!
Recent Advances in Digital Processing of Images and Audio by Microsoft Distinguished Engineer Malvar!
http://mitworld.mit.edu/stream/404/

Friday, February 23, 2007

solution to Interesting puzzle about baby girls!

Scheme solution to Interesting puzzle about baby girls!
;Written by Fakrudeen Ali Ahmed
;Date January 12, 2007
(define (try n tries)
(define (populate girls boys people tries)
(if (or (= tries 0) (= people 0)) (list girls boys)
(let ((moregirls (countgirls people))) (begin ;(display moregirls)(newline)
(populate (+ girls moregirls) (+ boys (- people moregirls)) (- people moregirls) (- tries 1))))
)
)
(populate 0 0 n tries)
)

(define (countgirls n)
(define (loop k count)
(if (> k n) count
(loop (+ k 1) (+ count (random 2)))
)
)
(loop 1 0)
)

(define (run file n limit repeat)(with-output-to-file file (lambda()(loop (lambda()(begin (display (try n limit))(newline))) repeat))))

Wednesday, February 21, 2007

Fastest way to fibonacci number!

;Written by Fakrudeen Ali Ahmed
;December 30, 2006

(define matrix-upperleft (lambda(x)(car x)) )
(define matrix-upperright (lambda(x)(cadr x)))
(define matrix-lowerleft (lambda(x)(caddr x)))
(define matrix-lowerright (lambda(x)(cadddr x)))

(define (mmul matrix1 matrix2)
(list
; upperLeft*right.upperLeft + upperRight*right.lowerLeft,
(+ (* (matrix-upperleft matrix1) (matrix-upperleft matrix2))
(* (matrix-upperright matrix1) (matrix-upperright matrix2)))
; upperLeft*right.upperRight + upperRight*right.lowerRight,
(+ (* (matrix-upperleft matrix1) (matrix-upperright matrix2))
(* (matrix-upperright matrix1) (matrix-lowerright matrix2)))
; lowerLeft*right.upperLeft + lowerRight*right.lowerLeft,
(+ (* (matrix-lowerleft matrix1) (matrix-upperleft matrix2))
(* (matrix-lowerright matrix1) (matrix-lowerleft matrix2)))
; lowerLeft*right.upperRight + lowerRight*right.lowerRight);
(+ (* (matrix-lowerleft matrix1) (matrix-upperright matrix2))
(* (matrix-lowerright matrix1) (matrix-lowerright matrix2)))
))
(define b '(1 1 1 0))
(define i '(1 0 0 1))

(define (power-res n power value residue)
(list
(mmul value value)
(if (= 0 (fix:and n power)) residue (mmul residue value))
))

(define (loop n power list)
(if (< (/ n 2) power) (mmul (car list) (cadr list)) (loop n (fix:lsh power 1) (power-res n power (car list) (cadr list))) )) (define (fib n) (cadr (loop n 1 (list b i))))

Sunday, February 11, 2007

Amazing equations!

When I was a school student these were the equations I was totally confused and amazed about:

c = 1 /√(μϵ) ---------- 1

I was confused what does speed of "normal" visible light has to do with current or magnet!

E = mc^2 ------------ 2

I was amazed this time, what the heck speed of light has to do with Energy! I didn't believe this 'c' is the same as speed of light the first time!

Amazing!

Saturday, February 10, 2007

MIT world: distributed intelligence

http://mitworld.mit.edu/ - Great talks about varied topics. Science, Economics, poverty etc. Very good to spend time meaningfully!
I just now taking a talk about:
The World Turned Upside Down: The Impact of the Return of India and China to their Historical Global Weight