Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bit plane encoder #18

Closed
wants to merge 34 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
563b585
Add files via upload
kk60503 Aug 21, 2023
12cafea
Upload source files
kk60503 Aug 21, 2023
a9a14e1
# This is a combination of 11 commits.
kk60503 Sep 19, 2023
28e460d
Add files via upload
kk60503 Sep 24, 2023
6f02bbe
Add files via upload
kk60503 Sep 24, 2023
551ca9f
Add files via upload
kk60503 Nov 12, 2023
cb24f21
Delete tests/bpe/img_files/raw/sinan.img
kk60503 Nov 14, 2023
329752e
Add files via upload
kk60503 Nov 19, 2023
af891de
Add files via upload
kk60503 Nov 19, 2023
8a13d0d
Add files via upload
kk60503 Nov 19, 2023
a820ced
Merge branch 'master' into bit_plane_encoder
kk60503 Nov 19, 2023
ee1d439
Delete olaf/bpe/testing directory
kk60503 Nov 19, 2023
f5f7811
Add files via upload
kk60503 Nov 19, 2023
6ae803f
Delete bin_check.py
kk60503 Nov 19, 2023
dda4fd8
Delete cfc.py
kk60503 Nov 19, 2023
7b96354
Delete test_bpe.py
kk60503 Nov 19, 2023
fb694ca
Delete pixel_check.py
kk60503 Nov 19, 2023
9fbd080
linting
kk60503 Nov 20, 2023
3bd3df2
Add files via upload
kk60503 Nov 20, 2023
9145a69
Add files via upload
kk60503 Nov 20, 2023
5d393f2
Add files via upload
kk60503 Nov 20, 2023
3ab97fd
Add files via upload
kk60503 Nov 20, 2023
5c9ff21
Update global.h
kk60503 Nov 20, 2023
97f4535
Update requirements.txt
kk60503 Nov 20, 2023
70f597a
Update pyproject.toml
kk60503 Nov 20, 2023
f2f72e2
Add files via upload
kk60503 Nov 20, 2023
b53eadd
Update requirements.txt
kk60503 Nov 20, 2023
255322f
Add files via upload
kk60503 Nov 20, 2023
bb7b542
Add files via upload
kk60503 Nov 20, 2023
8d0dced
Create __init__.py
kk60503 Nov 20, 2023
b54f573
Update test_bpe.py
kk60503 Nov 20, 2023
abb1a1e
Add files via upload
kk60503 Nov 20, 2023
0238c92
Update test_bpe.py
kk60503 Dec 3, 2023
471c009
Update test_bpe.py
kk60503 Dec 3, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,6 @@ dmypy.json

# Vagrant
.vagrant/

.DS_Store

73 changes: 73 additions & 0 deletions olaf/bpe/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Makefile for binding bpe C code program to Python
# The linker flags: -Wl,-ld_classic -undefined dynamic_lookup
# are for pybind11 usage with Mac M1+ OS. Please comment out as needed.
# There are also CXX flags to ignore certain warnings stemming from the source
# code that may be commented out.


PYLIB_SRC = .

BPE_SRC = source

LINK_TARGET = bpe.so

INC_DIRS = \
-I$(PYLIB_SRC) \
-I$(BPE_SRC)

BUILD_DIR = build

PYLIB_SOURCES = $(PYLIB_SRC)/bpe.cpp \
$(PYLIB_SRC)/main_pybind.cpp

BPE_SOURCES = \
$(BPE_SRC)/AC_BitPlaneCoding.c \
$(BPE_SRC)/StagesCodingGaggles.c \
$(BPE_SRC)/header.c \
$(BPE_SRC)/AdjustOutput.c \
$(BPE_SRC)/bitsIO.c \
$(BPE_SRC)/lifting_97M.c \
$(BPE_SRC)/BPEBlockCoding.c \
$(BPE_SRC)/bpe_decoder.c \
$(BPE_SRC)/lifting_97f.c \
$(BPE_SRC)/CoeffGroup.c \
$(BPE_SRC)/bpe_encoder.c \
$(BPE_SRC)/DC_EnDeCoding.c \
$(BPE_SRC)/errorhandle.c \
$(BPE_SRC)/ricecoding.c \
$(BPE_SRC)/PatternCoding.c \
$(BPE_SRC)/waveletbpe.c \

CXXFLAGS = \
-std=c++11 -g -Wall $(INC_DIRS) -fPIC \
$(shell python3-config --includes --cflags) \
$(shell python3 -m pybind11 --includes) \
-Wno-deprecated -Wno-unused-but-set-variable \
-Wno-unused-parameter -Wno-sign-compare
# above -Wno flags are to ignore certain warnings during testing

LDFLAGS = -lm $(shell python3-config --ldflags)
ifeq ($(shell uname),Darwin)
LDFLAGS += -Wl,-ld_classic -undefined dynamic_lookup
endif

OBJS = $(PYLIB_SOURCES:%.cpp=$(BUILD_DIR)/%.o) $(BPE_SOURCES:%.c=$(BUILD_DIR)/%.o)

.PHONY: all clean

$(BUILD_DIR)/%.o: %.c
@mkdir -p $(BUILD_DIR)/$(BPE_SRC)
$(CXX) -c $< -o $@ $(CXXFLAGS)

$(BUILD_DIR)/%.o: %.cpp
@mkdir -p $(BUILD_DIR)/$(BPE_SRC)
$(CXX) -c $< -o $@ $(CXXFLAGS)

$(LINK_TARGET): $(OBJS)
@mkdir -p $(BUILD_DIR)
$(CXX) -shared -o $@ $^ $(LDFLAGS)

all: $(LINK_TARGET)

clean:
rm -rf $(BUILD_DIR) $(LINK_TARGET)
23 changes: 23 additions & 0 deletions olaf/bpe/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Kyle Klein for OreSat - September 2023
oresat.org - [email protected]
[email protected]


This program has been adapted from the University of Nebraska-Lincoln's
C language implementation of the CCSDS 122.0-B-2 Recommended Standard for
Image Data Compression. It has also been made to include code that allows
binding to Python using pybind.

Information on the original source program and its calling arguments for image
parameters can be found at the following website:
http://hyperspectral.unl.edu

Please note header comments for each file, as we have attempted to adapt the
source code to more modern capabilities while maintaining portability.

It should be possible to use the Makefile in the source directory to build the
bpe program without incorporating the extra code used to bind it to Python.

Note: The source program has a command line option to indicate if pixels are
signed or unsigned. This option has been omitted from the python
program; all operations assume the pixels are unsigned by default.
15 changes: 15 additions & 0 deletions olaf/bpe/bpe.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <Python.h>
#include <pybind11/pybind11.h>
#include "source/global.h"

namespace py = pybind11;

PYBIND11_MODULE(bpe, m)
{
//docstring
m.doc() = "pybind11 bpe plugin";

m.def("encode_basearg", &encode_basearg);
m.def("encode_fullarg", &encode_fullarg);
m.def("decode", &decode);
}
165 changes: 165 additions & 0 deletions olaf/bpe/main_pybind.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
// implementation file for pybind

#include "source/global.h"
#include <iostream>


// Original program information
void Usage()
{
fprintf(stderr, "/***** Bit Plane Encoder Using Wavelet Transform ***/\n");
fprintf(stderr, "bpe [-e]/[-d] [Input_file_name] [-o Output_file_name] "
"[-r BitsPerPixel]\n");
fprintf(stderr, "\nParameters: \n");
fprintf(stderr, "[-e]: encoding filename; \n");
fprintf(stderr, "[-d]: decoding filename; \n");
fprintf(stderr, "[-o]: provide ouput file name. \n");
fprintf(stderr, "[-w]: the number of pixels of each row. \n");
fprintf(stderr, "[-h]: the number of pixels of each column. \n");
fprintf(stderr, "[-r]: bits per pixel for encoding. "
"(by default it is 0 and encoded to the last bitplane\n");
fprintf(stderr, "[-b]: the number of bits of each pixel. By default it is 8.\n");
fprintf(stderr, "[-f]: byte order of a pixel, if it consists of more than one bytes.\n"
"0 means little endian, 1 means big endian. Default value is 0.\n");
fprintf(stderr, "[-t]: wavelet transform. 1 is integer 9-7 DWT "
"and 0 is floating 9-7 DWT. By default it is integer DWT\n");
fprintf(stderr, "[-s]: the number of blocks in each segment. By default it is 256.\n");
fprintf(stderr, "eg 1: bpe -e sensin.img -o codes -r 1.0 -w 256 -h 256 -s 256 \n");
fprintf(stderr, "eg 2: bpe -d codes -o ss.img \n");
fprintf(stderr, "********** Author: Hongqiang Wang *****************\n");
fprintf(stderr, "********** Department of Electrical Engineering ****\n");
fprintf(stderr, "********** University of Nebraska -Lincoln ********\n");
fprintf(stderr, "********** March 9, 2008 *************************\n");
fprintf(stderr, "/****************************************************\n");
fprintf(stderr, "/***** Altered Sept 2023 for OreSat/PSAS **********\n");
fprintf(stderr, "/****************************************************\n");
return;
}
kk60503 marked this conversation as resolved.
Show resolved Hide resolved

// Parameter Error Check
BOOL ParameterValidCheck(StructCodingPara *PtrCoding)
{
if((PtrCoding->PtrHeader->Header.Part2.SegByteLimit_27Bits != 0) &&( PtrCoding->BitsPerPixel <= 0 ))
return FALSE;

if ((PtrCoding->ImageWidth < IMAGE_WIDTH_MIN) || (PtrCoding->ImageWidth > IMAGE_WIDTH_MAX))
return FALSE;

if (PtrCoding->ImageRows < IMAGE_ROWS_MIN)
return FALSE;

if (PtrCoding->PtrHeader->Header.Part3.S_20Bits < SEGMENT_S_MIN)
return FALSE;

if((PtrCoding->BitsPerPixel < 0) || (PtrCoding->BitsPerPixel >16))
return FALSE;

return TRUE;
}

// need to verify appropriate means of setting default params
void encode_basearg(char* c_infile, char * c_outfile, const UINT32 w,
const UINT32 h, const float bpp = 0, const UINT32 bit_depth = 8)
{
const UINT32 blocks_per_seg = 256;
const BOOL is_big_endian = 0;
const BOOL is_lossless = 1;

encode_fullarg(c_infile, c_outfile, w, h, bpp, bit_depth, blocks_per_seg,
is_big_endian, is_lossless);
}

void encode_fullarg(char * c_infile, char* c_outfile, const UINT32 w,
const UINT32 h, const float bpp = 0, const UINT32 bit_depth = 8,
const UINT32 blocks_per_seg = 256, const BOOL is_big_endian = 1,
const BOOL is_lossless = 1)
{
long TotalPixels = 0;
char StringBuffer[100] = {""};
StructCodingPara *PtrCoding = NULL;
time_t t0, t1; /* time_t defined in <time.h> and <sys/types.h> as long */
clock_t c0, c1; /* clock_t defined in <time.h> and <sys/types.h> as int */

PtrCoding = (StructCodingPara*)calloc(sizeof(StructCodingPara), 1);
HeaderInilization(PtrCoding);

strcpy(PtrCoding->InputFile, c_infile);
strcpy(PtrCoding->CodingOutputFile, c_outfile);
PtrCoding->ImageRows = h;
PtrCoding->ImageWidth = w;
PtrCoding->BitsPerPixel = bpp;
PtrCoding->PtrHeader->Header.Part4.PixelBitDepth_4Bits = bit_depth % 16;
//store information to a text file.
strcpy(StringBuffer, PtrCoding->CodingOutputFile);
strcat(StringBuffer, "En.txt");

if((PtrCoding->BitsPerPixel != 0) && PtrCoding->PtrHeader->Header.Part2.SegByteLimit_27Bits == 0)
{
PtrCoding->PtrHeader->Header.Part2.SegByteLimit_27Bits =
(PtrCoding->BitsPerPixel * PtrCoding->PtrHeader->Header.Part3.S_20Bits * 64/8);
}
//check validility of the input parameters.
if (ParameterValidCheck(PtrCoding) == FALSE)
{
ErrorMsg(BPE_INVALID_CODING_PARAMETERS);
}
// record the encoding time.
t0 = time(NULL);
c0 = clock();
EncoderEngine(PtrCoding);
c1 = clock();
t1 = time(NULL);
TotalPixels = PtrCoding->ImageRows * PtrCoding->ImageWidth;

fclose(PtrCoding->Bits->F_Bits);
free(PtrCoding->Bits);
free(PtrCoding->PtrHeader);
free(PtrCoding);

return;
}

//////// need to include bpp cases for embedded decoding?
void decode(char* c_infile, char* c_outfile)
{
long TotalPixels = 0;
char StringBuffer[100] = {""};
StructCodingPara *PtrCoding = NULL;
time_t t0, t1; /* time_t defined in <time.h> and <sys/types.h> as long */
clock_t c0, c1; /* clock_t defined in <time.h> and <sys/types.h> as int */

PtrCoding = (StructCodingPara *) calloc(sizeof(StructCodingPara), 1);
HeaderInilization(PtrCoding);

strcpy(PtrCoding->InputFile, c_infile);
strcpy(PtrCoding->CodingOutputFile, c_outfile);

short TotalBitsPerpixel = 0;
//store information to a text file.
strcpy(StringBuffer, PtrCoding->CodingOutputFile);
strcat(StringBuffer, ".txt");

if(PtrCoding->BitsPerPixel < 0)
{
ErrorMsg(BPE_INVALID_CODING_PARAMETERS);
}
t0 = time(NULL);
c0 = clock();
DecoderEngine(PtrCoding);
c1 = clock();
t1 = time(NULL);

TotalBitsPerpixel = PtrCoding->PtrHeader->Header.Part4.PixelBitDepth_4Bits;
if(TotalBitsPerpixel == 0)
{
TotalBitsPerpixel = 16;
}
TotalPixels = PtrCoding->ImageRows * PtrCoding->ImageWidth;

fclose(PtrCoding->Bits->F_Bits);
free(PtrCoding->Bits);
free(PtrCoding->PtrHeader);
free(PtrCoding);

return;
}
Loading
Loading