Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
17
Task/Bitmap-Histogram/Ada/bitmap-histogram-1.adb
Normal file
17
Task/Bitmap-Histogram/Ada/bitmap-histogram-1.adb
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
type Pixel_Count is mod 2**64;
|
||||
type Histogram is array (Luminance) of Pixel_Count;
|
||||
|
||||
function Get_Histogram (Picture : Grayscale_Image) return Histogram is
|
||||
Result : Histogram := (others => 0);
|
||||
begin
|
||||
for I in Picture'Range (1) loop
|
||||
for J in Picture'Range (2) loop
|
||||
declare
|
||||
Count : Pixel_Count renames Result (Picture (I, J));
|
||||
begin
|
||||
Count := Count + 1;
|
||||
end;
|
||||
end loop;
|
||||
end loop;
|
||||
return Result;
|
||||
end Get_Histogram;
|
||||
17
Task/Bitmap-Histogram/Ada/bitmap-histogram-2.adb
Normal file
17
Task/Bitmap-Histogram/Ada/bitmap-histogram-2.adb
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
function Median (H : Histogram) return Luminance is
|
||||
From : Luminance := Luminance'First;
|
||||
To : Luminance := Luminance'Last;
|
||||
Left : Pixel_Count := H (From);
|
||||
Right : Pixel_Count := H (To);
|
||||
begin
|
||||
while From /= To loop
|
||||
if Left < Right then
|
||||
From := From + 1;
|
||||
Left := Left + H (From);
|
||||
else
|
||||
To := To - 1;
|
||||
Right := Right + H (To);
|
||||
end if;
|
||||
end loop;
|
||||
return From;
|
||||
end Median;
|
||||
22
Task/Bitmap-Histogram/Ada/bitmap-histogram-3.adb
Normal file
22
Task/Bitmap-Histogram/Ada/bitmap-histogram-3.adb
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
F1, F2 : File_Type;
|
||||
begin
|
||||
Open (F1, In_File, "city.ppm");
|
||||
declare
|
||||
X : Image := Get_PPM (F1);
|
||||
Y : Grayscale_Image := Grayscale (X);
|
||||
T : Luminance := Median (Get_Histogram (Y));
|
||||
begin
|
||||
Close (F1);
|
||||
Create (F2, Out_File, "city_art.ppm");
|
||||
for I in Y'Range (1) loop
|
||||
for J in Y'Range (2) loop
|
||||
if Y (I, J) < T then
|
||||
X (I, J) := Black;
|
||||
else
|
||||
X (I, J) := White;
|
||||
end if;
|
||||
end loop;
|
||||
end loop;
|
||||
Put_PPM (F2, X);
|
||||
end;
|
||||
Close (F2);
|
||||
|
|
@ -20,31 +20,31 @@ FBSLTILE(FBSLFORM("B/w"), FBSLLOADIMAGE(blackwhite)) ' ditto, black-and-white
|
|||
RESIZE(FBSLFORM, 0, 0, 136, 162): CENTER(FBSLFORM): SHOW(FBSLFORM)
|
||||
|
||||
BEGIN EVENTS ' main message loop
|
||||
IF CBMSG = WM_CLOSE THEN DESTROY(ME) ' click any [X] button to quit
|
||||
IF CBMSG = WM_CLOSE THEN DESTROY(ME) ' click any [X] button to quit
|
||||
END EVENTS
|
||||
|
||||
SUB ToGrayScale()
|
||||
FOR ptr = head TO tail STEP 3
|
||||
b = PEEK(ptr + 0, 1) ' Windows stores colors in BGR order
|
||||
g = PEEK(ptr + 1, 1)
|
||||
r = PEEK(ptr + 2, 1)
|
||||
l = 0.2126 * r + 0.7152 * g + 0.0722 * b ' derive luminance
|
||||
POKE(ptr + 0, CHR(l))(ptr + 1, CHR)(ptr + 2, CHR) ' set pixel to shade of gray
|
||||
m = m + l
|
||||
NEXT
|
||||
FILEPUT(FILEOPEN(grayscale, BINARY_NEW), FILEGET): FILECLOSE(FILEOPEN) ' save grayscale image
|
||||
FOR ptr = head TO tail STEP 3
|
||||
b = PEEK(ptr + 0, 1) ' Windows stores colors in BGR order
|
||||
g = PEEK(ptr + 1, 1)
|
||||
r = PEEK(ptr + 2, 1)
|
||||
l = 0.2126 * r + 0.7152 * g + 0.0722 * b ' derive luminance
|
||||
POKE(ptr + 0, CHR(l))(ptr + 1, CHR)(ptr + 2, CHR) ' set pixel to shade of gray
|
||||
m = m + l
|
||||
NEXT
|
||||
FILEPUT(FILEOPEN(grayscale, BINARY_NEW), FILEGET): FILECLOSE(FILEOPEN) ' save grayscale image
|
||||
END SUB
|
||||
|
||||
SUB ToBlackAndWhite()
|
||||
STATIC b = CHR(0), w = CHR(255) ' initialize once
|
||||
|
||||
m = m / (tail - head) * 3 ' derive median
|
||||
FOR ptr = head TO tail STEP 3
|
||||
IF PEEK(ptr + 0, 1) < m THEN ' set pixel black
|
||||
POKE(ptr + 0, b)(ptr + 1, b)(ptr + 2, b)
|
||||
ELSE ' set pixel white
|
||||
POKE(ptr + 0, w)(ptr + 1, w)(ptr + 2, w)
|
||||
END IF
|
||||
NEXT
|
||||
FILEPUT(FILEOPEN(blackwhite, BINARY_NEW), FILEGET): FILECLOSE(FILEOPEN) ' save b/w image
|
||||
STATIC b = CHR(0), w = CHR(255) ' initialize once
|
||||
|
||||
m = m / (tail - head) * 3 ' derive median
|
||||
FOR ptr = head TO tail STEP 3
|
||||
IF PEEK(ptr + 0, 1) < m THEN ' set pixel black
|
||||
POKE(ptr + 0, b)(ptr + 1, b)(ptr + 2, b)
|
||||
ELSE ' set pixel white
|
||||
POKE(ptr + 0, w)(ptr + 1, w)(ptr + 2, w)
|
||||
END IF
|
||||
NEXT
|
||||
FILEPUT(FILEOPEN(blackwhite, BINARY_NEW), FILEGET): FILECLOSE(FILEOPEN) ' save b/w image
|
||||
END SUB
|
||||
|
|
|
|||
|
|
@ -1,11 +1,11 @@
|
|||
define('src_name', 'input.jpg'); // source image
|
||||
define('dest_name', 'output.jpg'); // destination image
|
||||
define('src_name', 'input.jpg'); // source image
|
||||
define('dest_name', 'output.jpg'); // destination image
|
||||
|
||||
$img = imagecreatefromjpeg(src_name); // read image
|
||||
$img = imagecreatefromjpeg(src_name); // read image
|
||||
|
||||
if(empty($img)){
|
||||
echo 'Image could not be loaded!';
|
||||
exit;
|
||||
echo 'Image could not be loaded!';
|
||||
exit;
|
||||
}
|
||||
|
||||
$black = imagecolorallocate($img, 0, 0, 0);
|
||||
|
|
@ -13,42 +13,42 @@ $white = imagecolorallocate($img, 255, 255, 255);
|
|||
$width = imagesx($img);
|
||||
$height = imagesy($img);
|
||||
|
||||
$array_lum = array(); // for storage of luminosity of each pixel
|
||||
$sum_lum = 0; // total sum of luminosity
|
||||
$average_lum = 0; // average luminosity of whole image
|
||||
$array_lum = array(); // for storage of luminosity of each pixel
|
||||
$sum_lum = 0; // total sum of luminosity
|
||||
$average_lum = 0; // average luminosity of whole image
|
||||
|
||||
for($x = 0; $x < $width; $x++){
|
||||
for($y = 0; $y < $height; $y++){
|
||||
// read pixel value
|
||||
$color = imagecolorat($img, $x, $y);
|
||||
$r = ($color >> 16) & 0xFF;
|
||||
$g = ($color >> 8) & 0xFF;
|
||||
$b = $color & 0xFF;
|
||||
// save pixel luminosity in temporary array
|
||||
$array_lum[$x][$y] = ($r + $g + $b);
|
||||
// add pixel luminosity to sum
|
||||
$sum_lum += $array_lum[$x][$y];
|
||||
}
|
||||
for($x = 0; $x < $width; $x++){
|
||||
for($y = 0; $y < $height; $y++){
|
||||
// read pixel value
|
||||
$color = imagecolorat($img, $x, $y);
|
||||
$r = ($color >> 16) & 0xFF;
|
||||
$g = ($color >> 8) & 0xFF;
|
||||
$b = $color & 0xFF;
|
||||
// save pixel luminosity in temporary array
|
||||
$array_lum[$x][$y] = ($r + $g + $b);
|
||||
// add pixel luminosity to sum
|
||||
$sum_lum += $array_lum[$x][$y];
|
||||
}
|
||||
}
|
||||
|
||||
// calculate average luminosity
|
||||
$average_lum = $sum_lum / ($width * $height);
|
||||
|
||||
for($x = 0; $x < $width; $x++){
|
||||
for($y = 0; $y < $height; $y++){
|
||||
// pixel is brighter than average -> set white
|
||||
// else -> set black
|
||||
if($array_lum[$x][$y] > $average_lum){
|
||||
imagesetpixel($img, $x, $y, $white);
|
||||
}
|
||||
else{
|
||||
imagesetpixel($img, $x, $y, $black);
|
||||
}
|
||||
}
|
||||
for($x = 0; $x < $width; $x++){
|
||||
for($y = 0; $y < $height; $y++){
|
||||
// pixel is brighter than average -> set white
|
||||
// else -> set black
|
||||
if($array_lum[$x][$y] > $average_lum){
|
||||
imagesetpixel($img, $x, $y, $white);
|
||||
}
|
||||
else{
|
||||
imagesetpixel($img, $x, $y, $black);
|
||||
}
|
||||
}
|
||||
}
|
||||
// save black and white image to dest_name
|
||||
imagejpeg($img, dest_name);
|
||||
|
||||
if(!file_exists(dest_name)){
|
||||
echo 'Image not saved! Check permission!';
|
||||
echo 'Image not saved! Check permission!';
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,8 +7,8 @@ class Bitmap {
|
|||
role PBM {
|
||||
has @.BM;
|
||||
method P4 returns Blob {
|
||||
"P4\n{self.width} {self.height}\n".encode('ascii')
|
||||
~ Blob.new: self.BM
|
||||
"P4\n{self.width} {self.height}\n".encode('ascii')
|
||||
~ Blob.new: self.BM
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
27
Task/Bitmap-Histogram/Rebol/bitmap-histogram-1.rebol
Normal file
27
Task/Bitmap-Histogram/Rebol/bitmap-histogram-1.rebol
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
Rebol [
|
||||
title: "Rosetta code: Bitmap/Histogram"
|
||||
file: %Bitmap-Histogram.r3
|
||||
url: https://rosettacode.org/wiki/Bitmap/Bitmap-Histogram
|
||||
needs: 3.21.9
|
||||
]
|
||||
|
||||
;; Load source image from file
|
||||
img: load %Lenna50.jpg
|
||||
;; Extract luminosity as vector for statistical operations
|
||||
vec: to vector! img/luminosity
|
||||
;; Compute median brightness threshold
|
||||
med: vec/median
|
||||
;; Allocate 256-bucket uint32 histogram (one bucket per brightness level)
|
||||
histogram: make vector! [uint32! 255]
|
||||
;; In a single pass: build brightness histogram and threshold each pixel
|
||||
forall vec [
|
||||
val: vec/1
|
||||
histogram/:val: histogram/:val + 1 ;; tally pixel's brightness bucket
|
||||
vec/1: either val < med [0][255] ;; binarize: black or white
|
||||
]
|
||||
;; Inspect histogram distribution
|
||||
? histogram
|
||||
;; Write thresholded grayscale data back to image
|
||||
img/gray: vec
|
||||
;; Save result as PNG and open in browser
|
||||
browse save %Lenna50-bw.png img
|
||||
8
Task/Bitmap-Histogram/Rebol/bitmap-histogram-2.rebol
Normal file
8
Task/Bitmap-Histogram/Rebol/bitmap-histogram-2.rebol
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
img: load %Lenna50.jpg
|
||||
foreach channel [red green blue] [
|
||||
vec: to vector! img/:channel
|
||||
med: vec/median
|
||||
forall vec [vec/1: either vec/1 < med [0][255]]
|
||||
img/:channel: vec
|
||||
]
|
||||
browse save %Lenna50-rgb.png img
|
||||
|
|
@ -27,6 +27,6 @@ object BitmapOps {
|
|||
val c2=Color.WHITE
|
||||
for(x <- 0 until bm.width; y <- 0 until bm.height; l=luminosity(bm.getPixel(x,y)))
|
||||
image.setPixel(x, y, if(l>threshold) c2 else c1)
|
||||
image
|
||||
image
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,10 +1,10 @@
|
|||
:HISTOGRAM:
|
||||
#30 = Buf_Free // #30 = buffer to store histogram data
|
||||
#30 = Buf_Free // #30 = buffer to store histogram data
|
||||
for (#9=0; #9<256; #9++) {
|
||||
Out_Reg(21) TC(#9) Out_Reg(Clear) // @21 = intensity value to be counted
|
||||
Buf_Switch(#15) // switch to image buffer
|
||||
#8 = Search(@21, CASE+BEGIN+ALL+NOERR) // count intensity values
|
||||
Buf_Switch(#30) // switch to histogram buffer
|
||||
Num_Ins(#8, FILL) // store count
|
||||
Out_Reg(21) TC(#9) Out_Reg(Clear) // @21 = intensity value to be counted
|
||||
Buf_Switch(#15) // switch to image buffer
|
||||
#8 = Search(@21, CASE+BEGIN+ALL+NOERR) // count intensity values
|
||||
Buf_Switch(#30) // switch to histogram buffer
|
||||
Num_Ins(#8, FILL) // store count
|
||||
}
|
||||
Return
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
fcn histogram(image){
|
||||
hist:=List.createLong(256,0); // array[256] of zero
|
||||
hist:=List.createLong(256,0); // array[256] of zero
|
||||
image.data.howza(0).pump(Void,'wrap(c){ hist[c]+=1 }); // byte by byte loop
|
||||
hist;
|
||||
}
|
||||
|
|
@ -8,7 +8,7 @@ fcn histogramMedian(hist){
|
|||
left,right:=hist[from],hist[to];
|
||||
while(from!=to){
|
||||
if(left<right){ from+=1; left +=hist[from]; }
|
||||
else { to -=1; right+=hist[to]; }
|
||||
else { to -=1; right+=hist[to]; }
|
||||
}
|
||||
from
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue