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

Grayscale button now functions as expected #58

Merged
merged 2 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
22 changes: 17 additions & 5 deletions Charcoal/Charcoal/Charcoal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,21 @@ void Charcoal::deleteBooks(const JSObject& thisobject, const JSArgs& args)
return;
}

void Charcoal::grayscaleName(const JSObject& thisobject, const JSArgs& args) {
MessageBoxA(NULL, "Grayscale Books Hook", "Grayscale Books Hook", MB_OK);
// Get the first argument
JSValueRef arg = args[0];
JSContextRef ctx = JSGlobalContextCreate(NULL);
// Convert it to a number
int ID = JSValueToNumber(ctx, arg, 0);

// Call the grayscale method
ebooks.grayscale(ID);

return;
}


JSValue Charcoal::printAllBooks(const JSObject& thisObject, const JSArgs& args) {

std::string bookList = ebooks.printall();
Expand Down Expand Up @@ -149,10 +164,7 @@ JSValueRef OnButtonClick(JSContextRef ctx, JSObjectRef function,
return JSValueMakeNull(ctx);
}

void Charcoal::grayscaleName(const JSObject& thisObject, const JSArgs& args) { //at the moment just gets metadata, Thomas will puts grayscale hooks here.
//std::string added = ebooks.getStringData();
//MessageBoxA(NULL, added.c_str(), "Book Data", MB_OK);
}



void Charcoal::OpenFile(const JSObject& thisObject, const JSArgs& args)
Expand Down Expand Up @@ -210,7 +222,7 @@ void Charcoal::OnDOMReady(ultralight::View* caller,
SetJSContext(context->ctx());
JSObject global = JSGlobalObject();
global["AddBook"] = BindJSCallback(&Charcoal::OpenFile);
global["nameToGrayscale"] = BindJSCallback(&Charcoal::grayscaleName);
global["grayscaleName"] = BindJSCallback(&Charcoal::grayscaleName);
global["deleteBook"] = BindJSCallback(&Charcoal::deleteBooks);

auto scoped_context = context;
Expand Down
14 changes: 6 additions & 8 deletions Charcoal/Charcoal/Epub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
#include <opencv2/imgproc/imgproc.hpp>
#define _CRT_SECURE_NO_WARNINGS
#pragma once
book Epub::add(PWSTR path)
book Epub::add(std::string npath)
{
book curr;
std::string npath = wstrtostr(path);
using namespace libzippp;
ZipArchive zf(npath);
zf.open(ZipArchive::ReadOnly);
Expand Down Expand Up @@ -98,6 +97,7 @@ book Epub::add(PWSTR path)
}
meta = meta->NextSiblingElement();
}

collection.push_back(curr);
//adding book to the collection
}
Expand All @@ -108,10 +108,10 @@ book Epub::add(PWSTR path)
return curr;
}

void Epub::grayscaleEpub(PWSTR path) {
std::string npath = wstrtostr(path);
void Epub::grayscaleEpub(std::string path) {
//std::string npath = wstrtostr(path);
using namespace libzippp;
ZipArchive zipArchive(npath);
ZipArchive zipArchive(path);
zipArchive.open(ZipArchive::Write);
std::vector<ZipEntry> entries = zipArchive.getEntries();
std::vector<ZipEntry>::iterator it;
Expand All @@ -128,9 +128,7 @@ void Epub::grayscaleEpub(PWSTR path) {
// Read the image file from the epub archive
int width, height, channels;
unsigned char* image = stbi_load_from_memory((unsigned char*)entry.readAsBinary(), entry.getSize(), &width, &height, &channels, 0);

// Grayscale the image
//grayscaleImage(image, width, height);


std::string base_filename = name.substr(name.find_last_of("/\\") + 1);
if(fileExt == "jpeg" || fileExt == "jpg")
Expand Down
5 changes: 2 additions & 3 deletions Charcoal/Charcoal/Epub.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
class Epub : public Library
{
public:
book add(PWSTR path);
void grayscaleEpub(PWSTR Path);
book add(std::string path);
void grayscaleEpub(std::string path);
private:
void grayscaleImage(unsigned char* imageData, int width, int height);
};

35 changes: 34 additions & 1 deletion Charcoal/Charcoal/Library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,10 @@ std::string Library::add(PWSTR path)
if (f == "epub")
{
Epub e;
book curr = e.add(path);
std::string npath = wstrtostr(path);
book curr = e.add(npath);
curr.ID = ID;
curr.path = npath;
++ID;
collection.push_back(curr);
titles.push_back(curr.title);
Expand Down Expand Up @@ -134,4 +136,35 @@ int Library::remove(int ID) // this actually removes the book from the library.
}
}
return -1;
}

int Library::grayscale(int ID)
{
for (int i = 0; i < collection.size(); i++)
{
if (collection[i].ID == ID)
{
std::string path = collection[i].path;
std::string f = getFileExtension((path));
if (f == "epub")
{
Epub e;
e.grayscaleEpub(path);
return 1;
}
else if (f == "AZW3" || f == "azw3")
{

}
else if (f == "pdf" || f == "PDF")
{

}
else
{

}
}
}
return -1;
}
4 changes: 2 additions & 2 deletions Charcoal/Charcoal/Library.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class book {
std::string language;
std::string description;

PWSTR path;
std::string path;


};
Expand All @@ -29,10 +29,10 @@ class Library {
std::string getFileExtension(const std::string& filePath);
int remove(int ID);
std::vector<std::string> getBookTitles();
int grayscale(int ID);
protected:
std::vector<book> collection;
std::vector<std::string> titles;
void push(book b);

};

Expand Down
11 changes: 10 additions & 1 deletion Charcoal/x64/Debug/assets/app.html
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,16 @@ <h2 id="result">
<button class="button-28" onclick="gscalebutton()">Grayscale</button>
<script>
function gscalebutton() {
nameToGrayscale();
var array = []
var checkboxes = document.querySelectorAll('input[type=checkbox]:checked')
for (var i = 0; i < checkboxes.length; i++) {
array.push(checkboxes[i].id)
alert(checkboxes[i].id);
var toGray = parseInt(checkboxes[i].id); //sets string ID to int so it can be passed back.
grayscaleName(toGray);
}

OnButtonClick();
}
</script>

Expand Down
Loading