Bitmap/Histogram: Difference between revisions

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
Line 15:
In the following solution the input file [https://gitlab.com/amarok8bit/action-rosetta-code/-/blob/master/source/lena30g.PPM lena30g.PPM] is loaded from H6 drive. Altirra emulator automatically converts CR/LF character from ASCII into 155 character in ATASCII charset used by Atari 8-bit computer when one from H6-H10 hard drive under DOS 2.5 is used.
{{libheader|Action! Bitmap tools}}
<langsyntaxhighlight lang=Action!>INCLUDE "H6:LOADPPM5.ACT"
 
DEFINE HISTSIZE="256"
Line 123:
DO UNTIL CH#$FF OD
CH=$FF
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Bitmap_Histogram.png Screenshot from Atari 8-bit computer]
Line 129:
=={{header|Ada}}==
Histogram of an image:
<langsyntaxhighlight lang=ada>type Pixel_Count is mod 2**64;
type Histogram is array (Luminance) of Pixel_Count;
Line 145:
end loop;
return Result;
end Get_Histogram;</langsyntaxhighlight>
Median of a histogram:
<langsyntaxhighlight lang=ada>function Median (H : Histogram) return Luminance is
From : Luminance := Luminance'First;
To : Luminance := Luminance'Last;
Line 163:
end loop;
return From;
end Median;</langsyntaxhighlight>
Conversion of an image to black and white art:
<langsyntaxhighlight lang=ada> F1, F2 : File_Type;
begin
Open (F1, In_File, "city.ppm");
Line 186:
Put_PPM (F2, X);
end;
Close (F2);</langsyntaxhighlight>
 
=={{header|BBC BASIC}}==
Line 192:
[[Image:greyscale_bbc.jpg|right]]
[[Image:histogram_bbc.gif|right]]
<langsyntaxhighlight lang=bbcbasic> INSTALL @lib$+"SORTLIB"
Sort% = FN_sortinit(0,0)
Line 250:
col% = TINT(x%*2,y%*2)
SWAP ?^col%,?(^col%+2)
= col%</langsyntaxhighlight>
 
=={{header|C}}==
 
<langsyntaxhighlight lang=c>typedef unsigned int histogram_t;
typedef histogram_t *histogram;
 
Line 260:
 
histogram get_histogram(grayimage im);
luminance histogram_median(histogram h);</langsyntaxhighlight>
 
<langsyntaxhighlight lang=c>histogram get_histogram(grayimage im)
{
histogram t;
Line 281:
}
return t;
}</langsyntaxhighlight>
 
The given <tt>histogram</tt> must be freed with a simple <tt>free(histogram)</tt>.
Line 287:
{{trans|Ada}}
 
<langsyntaxhighlight lang=c>luminance histogram_median(histogram h)
{
luminance From, To;
Line 305:
}
return From;
}</langsyntaxhighlight>
 
An example of usage is the following code.
 
<langsyntaxhighlight lang=c>#include <stdio.h>
#include <stdlib.h>
#include "imglib.h"
Line 358:
free_img((image)g_img);
free_img(color_img);
}</langsyntaxhighlight>
 
Which reads from the file specified from the command line and outputs to the standard out the PPM B/W version of the input image. The input image can be of any format handled by ImageMagick (see [[Read image file through a pipe]])
Line 364:
=={{header|Common Lisp}}==
{{libheader|opticl}}
<langsyntaxhighlight lang=lisp>(defpackage #:histogram
(:use #:cl
#:opticl))
Line 409:
(let* ((image (read-jpeg-file "lenna.jpg"))
(bw-image (gray->black&white-image (color->gray-image image))))
(write-pbm-file "lenna-bw.pbm" bw-image)))</langsyntaxhighlight>
 
=={{header|D}}==
{{trans|Ada}}
It uses the grayscale_image from the Grayscale image Task. The loaded frog image is from the Color quantization Task.
<langsyntaxhighlight lang=d>import grayscale_image;
 
Color findSingleChannelMedian(Color)(in Image!Color img)
Line 460:
img.binarizeInPlace(img.findSingleChannelMedian())
.savePGM("quantum_frog_bin.pgm");
}</langsyntaxhighlight>
 
=={{header|FBSL}}==
Line 467:
'''24-bpp P.O.T.-size BMP solution:'''
[[File:FBSLHistogram.PNG|right]]
<langsyntaxhighlight lang=qbasic>#DEFINE WM_CLOSE 16
 
DIM colored = ".\LenaClr.bmp", grayscale = ".\LenaGry.bmp", blackwhite = ".\LenaBnw.bmp"
Line 516:
NEXT
FILEPUT(FILEOPEN(blackwhite, BINARY_NEW), FILEGET): FILECLOSE(FILEOPEN) ' save b/w image
END SUB</langsyntaxhighlight>
 
=={{header|Forth}}==
<langsyntaxhighlight lang=forth>: histogram ( array gmp -- )
over 256 cells erase
dup bdim * over bdata + swap bdata
do 1 over i c@ cells + +! loop drop ;</langsyntaxhighlight>
 
=={{header|Fortran}}==
Line 529:
'''Note''': ''luminance'' range is hard-encoded and is from 0 to 255. This could be enhanced.
 
<langsyntaxhighlight lang=fortran>module RCImageProcess
use RCImageBasic
implicit none
Line 568:
end function histogram_median
end module RCImageProcess</langsyntaxhighlight>
 
Example:
 
<langsyntaxhighlight lang=fortran>program BasicImageTests
use RCImageBasic
use RCImageIO
Line 613:
call free_img(gray)
 
end program BasicImageTests</langsyntaxhighlight>
 
=={{header|Go}}==
Histogram and Threshold functions are be added to the Grmap type for this task:
<langsyntaxhighlight lang=go>package raster
 
import "math"
Line 640:
}
}
}</langsyntaxhighlight>
Demonstration program computes the median:
<langsyntaxhighlight lang=go>package main
 
// Files required to build supporting package raster are found in:
Line 686:
fmt.Println(err)
}
}</langsyntaxhighlight>
 
=={{header|Haskell}}==
First, an implementation of a black-and-white instance of <tt>Color</tt>. For simplicty, we use ASCII PBM for output instead of the raw format.
<langsyntaxhighlight lang=haskell>module Bitmap.BW(module Bitmap.BW) where
 
import Bitmap
Line 721:
toBWImage' darkestWhite = mapImage $ f . luminance
where f x | x < darkestWhite = black
| otherwise = white</langsyntaxhighlight>
 
Every instance of <tt>Color</tt> has a <tt>luminance</tt> method, so we don't need to convert an image to <tt>Gray</tt> to calculate its histogram.
<langsyntaxhighlight lang=haskell>import Bitmap
import Bitmap.RGB
import Bitmap.BW
Line 750:
if left < right
then (n + 1, left + l, right, ls, rL)
else (n, left, right + r, lL, rs)</langsyntaxhighlight>
 
=={{header|J}}==
Line 756:
 
Using <code>toGray</code> from [[Grayscale image#J|Grayscale image]].
<langsyntaxhighlight lang=j>getImgHist=: ([: /:~ ~. ,. #/.~)@,
medianHist=: {."1 {~ [: (+/\ I. -:@(+/)) {:"1
toBW=: 255 * medianHist@getImgHist < toGray</langsyntaxhighlight>
 
'''Example Usage:'''
Line 764:
Use [http://rosettacode.org/mw/images/b/b6/Lenna100.jpg Lenna100.jpg] for testing (read using the [[j:Addons/media/platimg|media/platimg]] addon and convert to ppm file).
 
<langsyntaxhighlight lang=j> require 'media/platimg'
'Lenna100.ppm' writeppm~ 256#.inv readimg 'Lenna100.jpg'
786447</langsyntaxhighlight>
 
Read ppm file, convert to black and white and write to a new ppm file using <code>writeppm</code>, <code>readppm</code> and <code>toColor</code> from the [[read ppm file#J | read]]/[[write ppm file#J|write ppm file]], and [[grayscale image#J|grayscale image]] solutions.
<langsyntaxhighlight lang=j> 'Lenna100BW.ppm' writeppm~ toColor toBW readppm 'Lenna100.ppm'
786447</langsyntaxhighlight>
 
=={{header|Java}}==
<langsyntaxhighlight lang=Java>import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
Line 835:
return median;
}
}</langsyntaxhighlight>
 
=={{header|Julia}}==
{{works with|Julia|0.6}}
 
<langsyntaxhighlight lang=julia>using Images, FileIO
 
ima = load("data/lenna50.jpg")
Line 848:
imb[imb .≤ medcol] = Gray(0.0)
imb[imb .> medcol] = Gray(1.0)
save("data/lennaGray.jpg", imb)</langsyntaxhighlight>
 
=={{header|Kotlin}}==
Uses the image from the [[Percentage difference between images]] task as an example.
<langsyntaxhighlight lang=scala>// version 1.2.10
 
import java.io.File
Line 912:
val bwFile = File("Lenna_bw.jpg")
ImageIO.write(image, "jpg", bwFile)
}</langsyntaxhighlight>
 
=={{header|Lua}}==
Line 920:
[[Basic bitmap storage#Lua]],
[[Grayscale image#Lua]].
<langsyntaxhighlight lang=lua>function Histogram( image )
local size_x, size_y = #image, #image[1]
Line 971:
 
bitmap = ConvertToColorImage( gray_im )
Write_PPM( "outputimage.ppm", bitmap )</langsyntaxhighlight>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang =Mathematica>ImageLevels[img]</langsyntaxhighlight>
 
=={{header|Nim}}==
<langsyntaxhighlight lang=Nim>import bitmap
import grayscale_image
 
Line 1,028:
 
# Save image as a PPM file.
image.writePPM("house_bw.ppm")</langsyntaxhighlight>
 
=={{header|OCaml}}==
{{Trans|C}}
 
<langsyntaxhighlight lang=ocaml>type histogram = int array
 
let get_histogram ~img:gray_channel =
Line 1,046:
done;
(t: histogram)
;;</langsyntaxhighlight>
 
<langsyntaxhighlight lang=ocaml>let histogram_median (h : histogram) =
 
let from = 0 and to_ = 255 in
Line 1,062:
in
aux from to_ left right
;;</langsyntaxhighlight>
 
main:
<langsyntaxhighlight lang=ocaml>let () =
let img = read_ppm ~filename:"/tmp/foo.ppm" in
 
Line 1,088:
 
output_ppm ~oc:stdout ~img:res;
;;</langsyntaxhighlight>
 
=={{header|Octave}}==
'''Using package''' [http://octave.sourceforge.net/image/index.html Image]
<langsyntaxhighlight lang=octave>function h = imagehistogram(imago)
if ( isgray(imago) )
for j = 0:255
Line 1,143:
ibw( img > m ) = 255;
ibw( img <= m ) = 0;
jpgwrite("lennamed.jpg", ibw, 100);</langsyntaxhighlight>
 
=={{header|Phix}}==
Requires read_ppm() from [[Bitmap/Read_a_PPM_file#Phix|Read_a_PPM_file]], write_ppm() from [[Bitmap/Write_a_PPM_file#Phix|Write_a_PPM_file]]. <br>
Uses demo\rosetta\lena.ppm, included in the distribution, results may be verified with demo\rosetta\viewppm.exw
<langsyntaxhighlight lang=Phix>-- demo\rosetta\Bitmap_Histogram.exw (runnable version)
include ppm.e -- black, white, read_ppm(), write_ppm() (covers above requirements)
 
Line 1,186:
sequence img = read_ppm("Lena.ppm")
img = to_bw(img)
write_ppm("LenaBW.ppm",img)</langsyntaxhighlight>
 
=={{header|PHP}}==
 
<langsyntaxhighlight lang=PHP>
define('src_name', 'input.jpg'); // source image
define('dest_name', 'output.jpg'); // destination image
Line 1,245:
echo 'Image not saved! Check permission!';
}
</syntaxhighlight>
</lang>
Example: <br>
<div>
Line 1,262:
=={{header|PicoLisp}}==
{{trans|Forth}}
<langsyntaxhighlight lang=PicoLisp>(de histogram (Pgm)
(let H (need 256 0)
(for L Pgm
(for G L
(inc (nth H (inc G))) ) )
H ) )</langsyntaxhighlight>
 
=={{header|PureBasic}}==
Also requires PureBasic solutions for [[Bitmap/Read_a_PPM_file#PureBasic|Read a PPM file]], [[Grayscale_image#PureBasic|Grayscale image]], and [[Bitmap/Write_a_PPM_file#PureBasic|Write a PPM file]].
<langsyntaxhighlight lang=PureBasic>Procedure getHistogram(image, Array histogram(1))
Protected w = ImageWidth(image) - 1
Protected h = ImageHeight(image) - 1
Line 1,333:
outputFile = Left(sourceFile, Len(sourceFile) - Len(GetExtensionPart(sourceFile))) + "_bw." + GetExtensionPart(sourceFile)
SaveImageAsPPM(image, outputFile, 1)
EndIf</langsyntaxhighlight>
 
=={{header|Python}}==
Makes use of the Pillow library (PIL) you can install it using pip. The code is probably not the fastest or the image I used (1960x1960) is just too big.
<langsyntaxhighlight lang=python>from PIL import Image
 
# Open the image
Line 1,378:
 
bw_image.show()
bm_image.show()</langsyntaxhighlight>
 
=={{header|Racket}}==
<langsyntaxhighlight lang=racket> #lang racket
(require racket/draw math/statistics racket/require
(filtered-in
Line 1,442:
(send (bitmap->monochrome 1/4 bm) save-file "histogram-racket-0.25.png" 'png)
(send (bitmap->monochrome 1/2 bm) save-file "histogram-racket-0.50.png" 'png) ; median
(send (bitmap->monochrome 3/4 bm xs ws) save-file "histogram-racket-0.75.png" 'png)))</langsyntaxhighlight>
 
{{out}}
Line 1,457:
Uses pieces from [[Bitmap#Raku| Bitmap]], [[Bitmap/Write_a_PPM_file#Raku| Write a PPM file]] and [[Grayscale_image#Raku| Grayscale image]] tasks. Included here to make a complete, runnable program.
 
<syntaxhighlight lang=raku perl6line>class Pixel { has UInt ($.R, $.G, $.B) }
class Bitmap {
has UInt ($.width, $.height);
Line 1,508:
histogram($b);
 
'./Lenna-bw.pbm'.IO.open(:bin, :w).write: $b.P4;</langsyntaxhighlight>
 
See [https://github.com/thundergnat/rc/blob/master/img/Lenna.png Lenna], and [https://github.com/thundergnat/rc/blob/master/img/Lenna-bw.png Lenna-bw] images. (converted to .png as .ppm format is not widely supported).
 
=={{header|Ruby}}==
<langsyntaxhighlight lang=ruby>class Pixmap
def histogram
histogram = Hash.new(0)
Line 1,553:
end
 
Pixmap.open('file.ppm').save_as_blackandwhite('file_bw.ppm')</langsyntaxhighlight>
 
=={{header|Rust}}==
<langsyntaxhighlight lang=rust>extern crate image;
use image::{DynamicImage, GenericImageView, ImageBuffer, Rgba};
 
Line 1,624:
img.save("lena-mono.png").expect("could not save result image");
}
</syntaxhighlight>
</lang>
 
=={{header|Scala}}==
Line 1,632:
* [[Read_ppm_file#Scala|Read a PPM File]] image loading
 
<langsyntaxhighlight lang=scala>object BitmapOps {
def histogram(bm:RgbBitmap)={
val hist=new Array[Int](255)
Line 1,663:
image
}
}</langsyntaxhighlight>
 
Usage:
<langsyntaxhighlight lang=scala>val img=Pixmap.load("image.ppm").get
val hist=BitmapOps.histogram(img)
val mid=BitmapOps.histogram_median(hist);
Line 1,676:
icon=new ImageIcon(BitmapOps.monochrom(img, mid).image)
}
}</langsyntaxhighlight>
 
=={{header|Tcl}}==
{{libheader|Tk}}
Uses [[read ppm file#Tcl|readPPM]], [[grayscale image#Tcl|grayscale]] and [[write ppm file#Tcl|output_ppm]] from other pages.
<langsyntaxhighlight lang=tcl>package require Tcl 8.5
package require Tk
 
Line 1,730:
}
}
}</langsyntaxhighlight>
 
=={{header|Vedit macro language}}==
Line 1,736:
On return, #30 points to buffer containing histogram data.
The histogram data is given as ASCII decimal values, one value per line.
<langsyntaxhighlight lang=vedit>:HISTOGRAM:
#30 = Buf_Free // #30 = buffer to store histogram data
for (#9=0; #9<256; #9++) {
Line 1,745:
Num_Ins(#8, FILL) // store count
}
Return</langsyntaxhighlight>
 
=={{header|Wren}}==
{{libheader|DOME}}
<langsyntaxhighlight lang=ecmascript>import "dome" for Window
import "graphics" for Canvas, Color, ImageData
 
Line 1,833:
}
 
var Game = ImageHistogram.new("Lenna100.jpg", "Lenna100_B&W.png")</langsyntaxhighlight>
 
=={{header|zkl}}==
{{trans|C}}
Uses the PPM class from http://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#zkl
<langsyntaxhighlight lang=zkl>fcn histogram(image){
hist:=List.createLong(256,0); // array[256] of zero
image.data.howza(0).pump(Void,'wrap(c){ hist[c]+=1 }); // byte by byte loop
Line 1,851:
}
from
}</langsyntaxhighlight>
<langsyntaxhighlight lang=zkl>img:=PPM.readPPMFile("lenaGrey.ppm"); // a grey scale image
median:=histogramMedian(histogram(img));
median.println();
Line 1,861:
img.data.pump(bw.data.clear(),'wrap(c){ if(c>median) 0xff else 0 });
 
bw.write(File("foo.ppm","wb"));</langsyntaxhighlight>
{{out}}<pre>101</pre>
See the BBC Basic entry or:
10,327

edits