Skip to content

Commit

Permalink
Merge pull request #58 from ChicoState/gray_button
Browse files Browse the repository at this point in the history
Grayscale button now functions as expected
  • Loading branch information
infinimineralex authored May 10, 2024
2 parents f11f9ea + 694819f commit 63e00aa
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 23 deletions.
22 changes: 17 additions & 5 deletions Charcoal/Charcoal/Charcoal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,21 @@ void Charcoal::deleteBooks(const JSObject& thisobject, const JSArgs& args)
return;
}

void Charcoal::grayscaleName(const JSObject& thisobject, const JSArgs& args) {
MessageBoxA(NULL, "Started Grayscaling book", "Started Grayscaling book", 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);
MessageBoxA(NULL, "Finished Grayscaling book", "Finished Grayscaling book", MB_OK);
return;
}


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

std::string bookList = ebooks.printall();
Expand Down Expand Up @@ -152,10 +167,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 @@ -213,7 +225,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);
global["AddBookFolder"] = BindJSCallback(&Charcoal::getFolderPath);

Expand Down
13 changes: 5 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,9 @@ book Epub::add(PWSTR path)
return curr;
}

void Epub::grayscaleEpub(PWSTR path) {
std::string npath = wstrtostr(path);
void Epub::grayscaleEpub(std::string 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 +127,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);
};

43 changes: 39 additions & 4 deletions Charcoal/Charcoal/Library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,16 @@ std::string Library::getFileExtension(const std::string& filePath)
int ID = 0;
std::string Library::add(PWSTR path)
{
std::string f = getFileExtension(wstrtostr(path));
std::string npath = wstrtostr(path);
std::string f = getFileExtension(npath);
if (f == "epub")
{
Epub e;
book curr = e.add(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 @@ -123,15 +127,46 @@ std::vector<std::string> Library::getBookTitles() {
return titles;
}

int Library::remove(int ID) // this actually removes the book from the library.
int Library::remove(int book_ID) // this actually removes the book from the library.
{
for (int i = 0; i < collection.size(); i++)
{
if (collection[i].ID == ID)
if (collection[i].ID == book_ID)
{
collection.erase(collection.begin() + i);
return 1;
}
}
return -1;
}

int Library::grayscale(int book_ID)
{
for (int i = 0; i < collection.size(); i++)
{
if (collection[i].ID == book_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 @@ -300,7 +300,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

0 comments on commit 63e00aa

Please sign in to comment.